Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 我在使用Java MongoClient访问数据库中的集合时遇到问题_Mongodb_Collections_Authorization_Openshift - Fatal编程技术网

Mongodb 我在使用Java MongoClient访问数据库中的集合时遇到问题

Mongodb 我在使用Java MongoClient访问数据库中的集合时遇到问题,mongodb,collections,authorization,openshift,Mongodb,Collections,Authorization,Openshift,我一直在努力从Java客户端访问openshift web应用程序中的MongoDB集合,但每次都失败了。我可以连接,但无法以任何方式查询集合 以下是当前的错误消息: JBWEB000070:异常 org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为com.mongodb.MongoTimeoutException:等待与ReadPreferenceServerSelector{readPreference=primar

我一直在努力从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:Command命令失败引起,错误18:'auth fails'在服务器127.12.158.2:27017上。完整响应为{“code”:18,“ok”:0.0,“errmsg”:“身份验证失败”}]

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)
我也尝试过:

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!
publicstaticmongoclient getMongoClient(){
//如果mongoClient尚未设置
if(mongoClient==null){
列表种子=新的ArrayList();
添加(新服务器地址(System.getenv(“OPENSHIFT\u MONGODB\u DB\u主机”),
parseInt(System.getenv(“OPENSHIFT\u MONGODB\u DB\u PORT”);
列表凭据=新建ArrayList();
凭据。添加(
MongoCredential.CreateMongorCredential(
“OPENSHIFT_MONGODB_DB_用户”,
“jbosseap”,
“OPENSHIFT\u MONGODB\u DB\u密码”
)
);
mongoClient=新的mongoClient(种子、凭据);
}
返回mongoClient;
}
而且:
公共静态MongoClient getMongoClient(){
//如果mongoClient尚未设置
if(mongoClient==null){
mongoClient=新的mongoClient(
新服务器地址(System.getenv(“OPENSHIFT\u MONGODB\u DB\u主机”),
parseInt(System.getenv(“OPENSHIFT\u MONGODB\u DB\u PORT”);
}
返回mongoClient;
}
我现在已经没有主意了,所以寻求你的智慧吧!

我已经解决了这个问题

使用环境变量
OPENSHIFT\u MONGODB\u DB\u USER
不起作用,因为该变量设置为空。如果在代码中硬编码为admin,则问题消失


有点令人困惑的是(至少对我来说)设置了环境变量OPENSHIFT\u MONGODB\u DB\u PASSWORD。

在执行程序之前,您是否保持mongod进程运行? 在执行程序之前,请遵循以下步骤:

  • 按Windows键并键入cmd.exe
  • 按Ctrl+Shift+Enter以管理员身份运行命令提示符
  • 在命令提示下,键入
    cd c:/Mongo/mongodb/bin
    (将其替换为实际的bin路径)
  • 确保在如下文件夹中配置了数据库路径:
    c:/data/db
    (此路径可能根据您使用的命令提示符而有所不同)
  • 在命令提示符下运行以下命令以启动mongod进程:
    mongod
  • mongod进程启动后,您将在命令提示符下收到以下消息:
  • 2017-02-26T11:33:37.233+0530 I FTDC[initandlisten]正在使用目录“E:/data/db/diagnostic.data”初始化全职诊断数据捕获2017-02-26T11:33:37.359+0530 I网络[thread1]等待端口27017上的连接

  • 现在执行你的程序
  • 我遇到了一个类似的问题,这就是如何解决的

    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!