今天使用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. hdu2586 How far away ?(lca模版题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树还有两个点然后求这两个点的最短距离. 题解:val[a]+val[b]-2*va ...

  2. CSU 1804: 有向无环图 拓扑排序 图论

    1804: 有向无环图 Submit Page   Summary   Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 716    ...

  3. 百度之星 资格赛 1003 度度熊与邪恶大魔王 dp(背包)

    度度熊与邪恶大魔王  Accepts: 1141  Submissions: 6840  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 3 ...

  4. gulp的介绍和手动安装

    gulp, 前端自动化工具, 文件操作, 项目上线之前,将碎片文件合并,将ES6转成ES5,文件压缩,快速搭建服务器... gulp基于node环境 gulp就是node的一个非内置的小模块 gulp ...

  5. Redis哨兵(Sentinel)模式快速入门

    更多内容,欢迎关注微信公众号:全菜工程师小辉.公众号回复关键词,领取免费学习资料. 当主服务器宕机后,需要手动把一台从服务器切换为主服务器,这就需要人工干预,费事费力,还会造成一段时间内服务不可用. ...

  6. 深入Go的错误处理机制使用

    开篇词 程序运行过程中不可避免的发生各种错误,要想让自己的程序保持较高的健壮性,那么异常,错误处理是需要考虑周全的,每个编程语言提供了一套自己的异常错误处理机制,在Go中,你知道了吗?接下来我们一起看 ...

  7. JavaScript数组和伪数组

    伪数组和数组 记住一句话: 伪数组是一个Object,数组是Array. 对象和数组之间的关系 JavaScript的内置函数继承与 Object.prototype. 可以认为new Array() ...

  8. C语言实现二级指针表示字符串数组

    头文件: #include<stdlib.h> #include<stdio.h> #include<string.h> 函数原型: char ** createB ...

  9. 增强学习Q-learning分析与演示(入门)

    一些说明.参阅 https://github.com/MorvanZhou/Reinforcement-learning-with-tensorflow/blob/master/contents/1_ ...

  10. 关于spring boot多张表建立外健的讨论

    现在有四张表:student(学生表).blogs(博客表).comment(评论表).reply(回复表) 现在说一下这四张表: student(学生表):学生的信息记录表 blogs(博客表):学 ...