java连接mongodb报错

hkh_1012 2015-04-05 08:47:21
环境mongodb3.0.1,虚拟机centos7
使用shell可以用mongo连接mongod
Mongo mg = new Mongo("192.168.59.128",27017);
DB db = mg.getDB("mydb");
for(String s:db.getCollectionNames()){
System.out.println(s);
}

异常如下:
四月 05, 2015 8:26:27 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster created with settings {hosts=[192.168.59.128:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50}
四月 05, 2015 8:26:28 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=192.168.59.128:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
四月 05, 2015 8:26:48 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Exception in monitor thread while connecting to server 192.168.59.128:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.59.128:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:166)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:172)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:80)
at com.mongodb.Mongo.execute(Mongo.java:736)
at com.mongodb.Mongo$2.execute(Mongo.java:723)
at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at com.mongodb.OperationIterable.forEach(OperationIterable.java:66)
at com.mongodb.MappingIterable.forEach(MappingIterable.java:50)
at com.mongodb.MappingIterable.into(MappingIterable.java:60)
at com.mongodb.DB.getCollectionNames(DB.java:223)
at mongodbtest.TestMongodbCurd.main(TestMongodbCurd.java:13)
...全文
35131 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
grilmylove 2015-10-30
  • 打赏
  • 举报
回复
我也遇到了这个问题,最终解决的办法是: 我在用命令mongo localhost:9927 连接db后,发现显示为application:SECONDARY> show collections; 本以为是主节点的,居然成了副节点了,原来是因为我把三个节点同时启动的原因,所以没有保证9927是第一个启动的。 关掉再重新打开9928的节点后,9927就成了主节点了,连接到9927显示为application:SECONDARY> show collections。 这个时候再测试 已经ok了。 [success] Total time: 8 s, completed 2015-10-30 14:54:45
SexyCode 2015-04-27
  • 打赏
  • 举报
回复
意思是连接超时,那就更改连接最大时间
JPF1024 2015-04-27
  • 打赏
  • 举报
回复
也可以用127.0.0.1试试
eshujiushiwo 2015-04-07
  • 打赏
  • 举报
回复
引用 1 楼 hkh_1012 的回复:
自己已解决, http://blog.csdn.net/hkh_1012/article/details/44899561
谢谢分享
hkh_1012 2015-04-06
  • 打赏
  • 举报
回复
自己已解决, http://blog.csdn.net/hkh_1012/article/details/44899561

1,747

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧