【发布时间】:2016-02-04 01:21:09
【问题描述】:
我一直在努力从我的 Java 客户端访问我的 openshift Web 应用程序中的 MongoDB 集合,并且每次都失败。我可以连接,但不能以任何方式查询集合。
这是当前的错误信息:
JBWEB000070:异常
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 com.mongodb.MongoTimeoutException:在等待与 ReadPreferenceServerSelector{readPreference=primary} 匹配的服务器时超时 30000 毫秒。集群状态的客户端视图是 {type=UNKNOWN, servers=[{address=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticationing},由 {com.mongodb 引起.MongoCommandException:命令失败,错误 18:服务器 127.12.158.2:27017 上的“身份验证失败”。完整的响应是 { "code" : 18, "ok" : 0.0, "errmsg" : "auth failed" }}}]
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!
【问题讨论】:
标签: mongodb collections authorization openshift