Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.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

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
来自mongo java客户端的MongoDB身份验证失败_Java_Mongodb - Fatal编程技术网

来自mongo java客户端的MongoDB身份验证失败

来自mongo java客户端的MongoDB身份验证失败,java,mongodb,Java,Mongodb,我尝试使用mongo java客户端版本3.2连接到mongo db版本3.2,得到了以下异常,知道这里出了什么问题吗 com.mongodb.MongoSecurityException: Exception authenticating at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48) at com.mongodb.connection.InternalStreamC

我尝试使用mongo java客户端版本3.2连接到mongo db版本3.2,得到了以下异常,知道这里出了什么问题吗

com.mongodb.MongoSecurityException: Exception authenticating
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server 10.100.5.41:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:46)
... 5 more
下面是我用来连接的代码

MongoClient mongoClient = null;

    try {
        MongoCredential credential = MongoCredential.createMongoCRCredential("admin", "mydatabase", "admin123".toCharArray());
        mongoClient = new MongoClient(new ServerAddress("10.100.5.41", 27017), Arrays.asList(credential));

        MongoDatabase database = mongoClient.getDatabase("mydatabase");
        System.out.println(database.getName());
        MongoCollection collection = database.getCollection("user");

        MongoCursor cursor = collection.find().iterator();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

经过数小时的调试,db管理员告诉我们,他已经更改了用户名和密码。有时我想知道为什么我会成为一名软件工程师。

您是否在mydatabase或admin(默认设置)中添加了用户?或者换句话说,在createMongoCRCredential中尝试使用admin作为数据库。@Zeus代码似乎正确。我也在做同样的事情,连接到我的服务器没有问题。在身份验证过程中,检查服务器上的日志是否存在错误。请有人关闭这个问题,经过数小时的调试,db管理员告诉我们,他已经更改了用户名和密码。有时我想知道为什么我会成为一名软件工程师。@Zeus用你刚才说的回答你的问题并接受it@neomega太好了,谢谢你的提示。