Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mongodb

mlab

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

like image 377
Crypt Junior Avatar asked Jan 18 '16 21:01

Crypt Junior


1 Answers

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

use krimson
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "write", db: "krimson" }
    ]
 )
like image 125
Alex Avatar answered Oct 05 '22 22:10

Alex