我一直在努力从我的Java客户端访问我的openshift Web应用程序中的MongoDB集合,并且每次都失败了。我可以连接,但无法以任何方式查询集合。
以下是当前的错误消息:
JBWEB000070:异常
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000 ms后超时。群集状态的客户端视图是{type = UNKNOWN,servers = [{address = 127.12.158.2:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSecurityException:Exception authenticating},由{com.mongodb引起.MongoCommandException:命令失败,错误18:' auth failed'在服务器127.12.158.2:27017上。完整的回答是{"代码" :18," ok" :0.0," errmsg" :" auth失败" }}}]
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
JBWEB000071:根本原因
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=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]
com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:167)
com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
com.mongodb.Mongo.execute(Mongo.java:760)
com.mongodb.Mongo$2.execute(Mongo.java:747)
com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:135)
com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:36)
org.jboss.tools.example.springmvc.mongodb.model.util.MongodbUtil.getCataloguesWithoutJoins(MongodbUtil.java:184)
我用来获取在堆栈跟踪中提到的整个MondodbUtil类中使用的mongoClient的代码是:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
mongoClient = new MongoClient(new MongoClientURI(
"mongodb://"+System.getenv("OPENSHIFT_MONGODB_DB_USER")+":"+
System.getenv("OPENSHIFT_MONGODB_DB_PASSWORD")+"@"+
System.getenv("OPENSHIFT_MONGODB_DB_HOST")+":"+
System.getenv("OPENSHIFT_MONGODB_DB_PORT")+"/jbosseap"));
}
return mongoClient;
}
我也尝试过:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"),
Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
MongoCredential.createMongoCRCredential(
"OPENSHIFT_MONGODB_DB_USER",
"jbosseap",
"OPENSHIFT_MONGODB_DB_PASSWORD".toCharArray()
)
);
mongoClient = new MongoClient( seeds, credentials );
}
return mongoClient;
}
And also:
public static MongoClient getMongoClient() {
// If mongoClient not yet set up
if (mongoClient == null) {
mongoClient = new MongoClient(
new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"),
Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
}
return mongoClient;
}
I have run out of ideas now, so seeking your wisdom!
答案 0 :(得分:1)
在执行程序之前,你是否保持mongod进程运行? 在执行程序之前,请按照以下步骤操作:
Ctrl+Shift+Enter
以管理员身份运行命令提示符cd c:/Mongo/mongodb/bin
(将其替换为实际的bin路径)c:/data/db
(此路径可能因您使用的命令提示符而异)mongod
2017-02-26T11:33:37.233 + 0530 I FTDC [initandlisten]使用目录“E:/data/db/diagnostic.data”初始化全时诊断数据“2017-02-26T11:33:37.359+ 0530 I NETWORK [thread1]等待端口27017上的连接
我遇到了类似的问题,这就是解决问题的方法。
答案 1 :(得分:0)
我已经解决了这个问题。
使用环境变量OPENSHIFT_MONGODB_DB_USER
不起作用,因为它设置为空白。如果在代码中硬编码为 admin ,则问题就会消失。
有点令人困惑(至少对我来说)环境变量OPENSHIFT_MONGODB_DB_PASSWORD
已设置。