MongoDB - Command failed with error code 13 `not authorized by ***** to execute this command`
Asked Answered
I

4

15

So for some odd reason, my user isn't get authorized to write anything in the krimson database. The database connection is successfully but the access to grant the user to write into the database isn't working as expected.

Full Error

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 }

Connect Code

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

}

I've tried to recreate the user and repute in the credentials to see if that was the problem and it wasn't. I'm using MongoLab at the moment. So I'm wondering if that could be one of the problems or what not or is there a specific way I need to assign a user admin role in MongoLab manually

Inviolable answered 18/1, 2016 at 21:43 Comment(0)
R
4

You need to grant write access to the user's account in the mongo shell:

use krimson
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "write", db: "krimson" }
    ]
 )
Rozier answered 18/1, 2016 at 21:49 Comment(6)
I'm a complete noobie at MongoDB where would I exactly put this or execute this?Inviolable
@Messa So I would connect with the Mongo Shell and execute the command? I'm using MongoLab?Inviolable
You can run mongo ds037395.mongolab.com:37395Rozier
Had the same mistake as @CryptJunior, but granting this role didn't solved the problem.Potiche
Sorry for the "one more" comment, but did someone managed to get this problem solved? Same odd problem in here :-(Amitie
Also, I am pulling my hair around 2 days. In my case , i am using reactive mongodb with spring boot 2.1.1. If someone has resolved this problem, please help.Zosima
C
4

I was facing the same error.

So Tried below code and it works for me.

MongoCredential credential = MongoCredential.createCredential("xyz", "admin", "password".toCharArray());
        MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 1235), Arrays.asList(credential));
Consalve answered 28/6, 2019 at 4:24 Comment(0)
W
3

I got the same error and solved it by setting the authSource=admin

sample url string

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

The user already has full authority and Spring boot was able to connect to mongoDB while startup. The issue was while accessing the data from mongo repository, it was not able to authenticate the user. Giving the authSource resolved the issue.

Wandis answered 20/9, 2020 at 19:4 Comment(2)
What does "root:example" stand for?Kondon
In my case, I just replaced "root:example" by my username and password.Markswoman
S
-3

Try changing the application.propertys to application.yml

Sarmiento answered 14/12, 2023 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.