Caused by: 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=XXX.XXX.XXX.XXX:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='admin', source='GWPayCenter', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server XXX.XXX.XXX.XXX:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
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.getWriteConnectionSource(ClusterBinding.java:68)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:144)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:71)
at com.mongodb.Mongo.execute(Mongo.java:819)
at com.mongodb.Mongo$2.execute(Mongo.java:802)
at com.mongodb.MongoCollectionImpl.createIndexes(MongoCollectionImpl.java:491)
at com.mongodb.MongoCollectionImpl.createIndex(MongoCollectionImpl.java:458)
at org.springframework.data.mongodb.core.DefaultIndexOperations.lambda$ensureIndex$0(DefaultIndexOperations.java:111)
at org.springframework.data.mongodb.core.DefaultIndexOperations.execute(DefaultIndexOperations.java:192)
问题描述
java 连接mongodb报超时,相同用户用NoSQLBooster for MongoDB连接成功,排除用户名密码问题、mongo server未启动问题,管理员账号登录,也不该有权限不足问题
解决方法
账号权限问题,改用目标库对应的账号即可
查询指定账号所对应的库,连接库不在结果中会连接失败
db.getCollection("system.users").find({"user":'devuser'})
.projection({})
.sort({_id:-1})
.limit(100)