com.mongodb.MongoSocketOpenException: Exception opening socket

本地连接虚拟机上的MongoDB失败,报错内容如下:

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.43.31:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
	at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
	at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)
	at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)
	at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:158)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:133)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:128)
	at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:118)
	at com.mongodb.operation.DropCollectionOperation.execute(DropCollectionOperation.java:54)
	at com.mongodb.operation.DropCollectionOperation.execute(DropCollectionOperation.java:39)
	at com.mongodb.Mongo.execute(Mongo.java:781)
	at com.mongodb.Mongo$2.execute(Mongo.java:764)
	at com.mongodb.DBCollection.drop(DBCollection.java:1969)
	at com.mongodb.casbah.MongoCollectionBase$class.dropCollection(MongoCollection.scala:147)
	at com.mongodb.casbah.MongoCollection.dropCollection(MongoCollection.scala:1106)

解决方法:

将配置文件 mongodb.conf 中的 bind_ip=127.0.0.1 改为 bind_ip=0.0.0.0

  • 127.0.0.1 表示只允许本地连接,不接受远程连接mongodb

你可能感兴趣的:(MongoDB)