【问题标题】:MongoDB - Command failed with error code 13 `not authorized by ***** to execute this command`MongoDB - 命令失败,错误代码 13`***** 未授权执行此命令`
【发布时间】:2021-01-09 04:43:33
【问题描述】:

因此,出于某种奇怪的原因,我的用户无权在 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 }

连接代码

    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");

}

我已尝试重新创建用户并在凭据中获得声誉,以查看这是否是问题所在,而事实并非如此。 我目前正在使用 MongoLab。所以我想知道这是否可能是问题之一,或者不是什么,或者我需要在 MongoLab 中手动分配用户管理员角色的特定方法

【问题讨论】:

    标签: java mongodb mlab


    【解决方案1】:

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

    use krimson
    db.grantRolesToUser(
        "reportsUser",
        [
          { role: "write", db: "krimson" }
        ]
     )
    

    【讨论】:

    • 我完全是 MongoDB 的菜鸟,我应该把它放在哪里或执行它?
    • @Messa 所以我会连接 Mongo Shell 并执行命令?我正在使用 MongoLab?
    • 你可以运行mongo ds037395.mongolab.com:37395
    • 和@CryptJunior有同样的错误,但是授予这个角色并没有解决问题。
    • 对不起,“还有一个”评论,但有人设法解决了这个问题吗?同样奇怪的问题在这里:-(
    【解决方案2】:

    我遇到了同样的错误。

    所以尝试了下面的代码,它对我有用。

    MongoCredential credential = MongoCredential.createCredential("xyz", "admin", "password".toCharArray());
            MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 1235), Arrays.asList(credential));
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的错误并通过设置 authSource=admin 解决了它

      示例网址字符串

      mongodb://root:example@localhost:27017/learningdb?authSource=admin&retryWrites=true&w=majority
      

      用户已经拥有完全权限,并且 Spring boot 能够在启动时连接到 mongoDB。问题是在从 mongo 存储库访问数据时,它无法对用户进行身份验证。提供 authSource 解决了这个问题。

      【讨论】:

      • “root:example”代表什么?
      猜你喜欢
      • 2021-09-07
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多