MongoDB - 命令失败,错误代码为13`未经*****授权执行此命令`

Cry*_*ior 6 java mongodb mlab

因此,由于一些奇怪的原因,我的用户没有被授权在krimson数据库中写任何东西.数据库连接成功,但授予用户写入数据库的访问权限未按预期工作.

完全错误

Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on krimson to execute command { findandmodify: "users", query: { _id: "_id" }, fields: {}, sort: {}, new: true, upsert: true, update: { $i
nc: { _id: 1 } } }' on server ds037395.mongolab.com:37395. The full response is { "ok" : 0.0, "errmsg" : "not authorized on krimson to execute command { findandmodify: \"users\", query: { _id: \"_id\" }, fields: {}, sort: {}, new:
 true, upsert: true, update: { $inc: { _id: 1 } } }", "code" : 13 }
Run Code Online (Sandbox Code Playgroud)

连接代码

    public static void connects(String ip, int port, String database, String username, String password) {
    try {
        client = new MongoClient(ip, port); /**Creating our connection between server & MongoDB*/
        krimson = client.getDB(database); /**Checks if the database exists or not if not then create a new one*/

        MongoCredential.createCredential(username, database, password.toCharArray()); /**Credentials used to connect to MongoDB*/

    } catch (Exception e){
        e.printStackTrace();
        System.out.println("Linking connections failed"); /**Outputs when the database cannot connect*/
        UtilServer.broadcast("&c&lThe Krimson Library did not establish a successfully connection.");
    }

    users = krimson.getCollection("users");

}
Run Code Online (Sandbox Code Playgroud)

我试图重新创建用户并在凭证中声明是否存在问题,但事实并非如此.我现在正在使用MongoLab.所以我想知道这可能是其中一个问题,或者是什么,或者是否有一种特定的方式我需要手动在MongoLab中分配用户管理员角色

Ale*_*lex 4

您需要在 mongo shell 中授予用户帐户写入权限:

use krimson
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "write", db: "krimson" }
    ]
 )
Run Code Online (Sandbox Code Playgroud)