今天使用mongo-java-drive写连接mongo的客户端,着实被上面那个错坑了一把。回顾一下解决过程:

报错

 com.mongodb.MongoTimeoutException: Timed out after  ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake}, caused by {java.io.EOFException: SSL peer shut down incorrectly}}]
at com.mongodb.connection.BaseCluster.getDescription(BaseCluster.java:)
at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:)
at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:)
at com.mongodb.client.internal.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:)
at com.mongodb.client.internal.MongoCollectionImpl.executeInsertOne(MongoCollectionImpl.java:)
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:)
at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:)
at com.czb.chargepile.core.MongeTemplate.insert(MongeTemplate.java:)
at com.czb.chargepile.manage.X01Manager.insert(X01Manager.java:)
at manage.X01ManagerTest.main(X01ManagerTest.java:)

分析:mongodb数据库连接超时,也就是客户端连不上mongo

代码

 MongoCredential credential = MongoCredential.createCredential(user, databaseName, password);

 mongoClient = MongoClients.create(

 MongoClientSettings.builder()

 .applyToSslSettings(builder -> builder.enabled(true))

 .applyToClusterSettings(builder ->

 builder.hosts(Arrays.asList(new ServerAddress("localhost", 27017))))

 .credential(credential)

 .build());

 MongoDatabase database = mongoClient.getDatabase(databaseName);

解决

1、看到错误,首先想到的是用户名密码不对,为了确保密码无误,重新设置了mongo的密码,然而并未好使

2、检查用户所属数据库是否正确,经过检查,发现完全匹配

3、通过:https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#reference-compatibility-mongodb-java

检查mongo-java-drive版本是否和mongo版本匹配,发现完全匹配

4、最后开始怀疑上面的代码有问题,找官方文档https://mongodb.github.io/mongo-java-driver/3.8/driver/tutorials/authentication/,从这里找到了新的写法,先无脑拷贝,然后运行,发现这里写法是成功的,与自己的代码对比,发现我的代码多了一行

 .applyToSslSettings(builder -> builder.enabled(true))

回到我代码,去掉,发现成功了。又回去看官方文档,找到端倪

如果使用那个选项,传输过程会使用TLS/SSL对传输层进行加密,但是我的mongo服务是没有对应设置的,所以导致连接不上。

Timed out after 30000 ms while waiting to connect的更多相关文章

  1. (node:7584) UnhandledPromiseRejectionWarning: MongooseTimeoutError: Server selection timed out after 30000 ms

    记录一次学习node.js犯的低级错误 这里遇到一个这样的问题 express连接mongoose时报错(node:7584) UnhandledPromiseRejectionWarning: Mo ...

  2. mongo连接不上Timed out after 30000

    本地连接mongo报错,错误代码为: Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while wait ...

  3. 【异常】Reason: Executor heartbeat timed out after 140927 ms

    1 详细异常 ERROR scheduler.JobScheduler: Error running job streaming job ms. org.apache.spark.SparkExcep ...

  4. php Connection timed out after 30000 milliseconds

    function HttpRequest($url, $params, $method = 'GET', $header = array(), $bEncode = true){ $opts = ar ...

  5. java连接mongodb的一个奇葩问题及奇葩解决方式

    昨天在eclipse中编写代码,本来连接mongodb进行各项操作都是正常的,但是有一会儿突然之间就没法连接了,还一直抱错,错误如下: 信息: Cluster created with setting ...

  6. mongodb单机搭建

    参考网站:http://www.runoob.com/mongodb/mongodb-linux-install.html 1.下载 https://www.mongodb.com/download- ...

  7. Android跟蓝牙耳机建立连接有两种方式

    Android 跟蓝牙耳机建立连接有两种方式. 1. Android 主动跟蓝牙耳机连BluetoothSettings 中和蓝牙耳机配对上之后, BluetoothHeadsetService 会收 ...

  8. Android 蓝牙( Bluetooth)耳机连接分析及实现

    Android 实现了对Headset 和Handsfree 两种profile 的支持.其实现核心是BluetoothHeadsetService,在PhoneApp 创建的时候会启动它. if ( ...

  9. Mongo集群Java连接时UnknownHostException错误

    今天在 Java 连接 Mongo 集群时报了一个超时的错误,但是在本地客户端连接单节点的时候却能连上,具体报的错误如下: Caused by: com.mongodb.MongoTimeoutExc ...

随机推荐

  1. 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...

  2. JUC(1)

    cmd目录切换 D:\>cd /d D:\Program Files\Java\jdk1.8.0_201\bin 1. JUC 简介 在 Java 5.0 提供了 java.util.concu ...

  3. javascript 多个异步处理解决方法

    JS异步处理真是一个让人头疼的东西,尤其是AJAX这个玩意.刚开始为了方便我会将jquery ajax 这个属性这样设置 async: false但是这样做问题很多,会让浏览器假死,好在ajax提供了 ...

  4. Erlang 不同版本内容

    OTP 22.0 Erlang/OTP 22是一个新的主要版本,具有新的特性和改进,同时也具有不兼容性. 要更深入地了解OTP 22发行版的亮点,您可以阅读我们的博客: http://blog.erl ...

  5. C++ lambda的演化

    翻译自https://www.bfilipek.com/2019/02/lambdas-story-part1.html与https://www.bfilipek.com/2019/02/lambda ...

  6. 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)

    //导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...

  7. spring boot的多环境部署

    需求:不同的环境有不同的开关属性,比如开发系统,需要关闭短信,微信的通知功能.而演示环境,线上环境则需要打开这些配置. 那么,如何做到呢?--->在properties.application配 ...

  8. Cabloy-CMS:动静结合,解决Hexo痛点问题

    介绍 Cabloy-CMS是什么 Cabloy-CMS是基于CabloyJS全栈业务开发框架开发的"动静结合"的CMS,可以快速构建企业网站.博客.社区.商城等Web应用. 在线演 ...

  9. charles SSL代理设置

    本文参考:charles SSL代理设置 charles SSL代理设置 SSL Proxying Srtting 这里最常用的设置就是第一个ssl proxying,这里记录了需要捕获哪些ssl的信 ...

  10. 如何从请求、传输、渲染3个方面提升Web前端性能

    什么是WEB前端呢?就是用户电脑的浏览器所做的一切事情.我们来看看用户访问网站,浏览器都做了哪些事情: 输入网址 –> 解析域名 -> 请求页面 -> 解析页面并发送页面中的资源请求 ...