0

I've created an user on mongo db this is the comnand and the output

db.createUser({user:"appuser",pwd:"12345",roles: [ { role: "readWrite", db: "mydb" }]})
Successfully added user: {
    "user" : "appuser",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "mydb"
        }
    ]
}

but when I try to enter with this command

mongo --port 27017 -u "appuser" --authenticationDatabase "mydb" -p

I get this error

MongoDB shell version v4.4.4
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=mydb&compressors=disabled&gssapiServiceName=mongodb
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

I created a root user and setted this option in /etc/mongod.conf

security:
  authorization: enabled

is something wrong?

af_159623
  • 197
  • 10

1 Answers1

0

From the official document of mongodb here

The database where you create the user (in this example, test) is that user's authentication database.

The database where we create the user will be used in the command to connect to MongoDB. We need to run use [nameofdatabase] to select the database before creating the user.

So, if you want to use mydb as the authentication database, you need to run these commands :

use mydb;
db.createUser({user:"appuser",pwd:"12345",roles: [ { role: "readWrite", db: "mydb" }]});

Then you can connect to your MongoDB with : mongo --port 27017 -u "appuser" --authenticationDatabase "mydb" -p

Đăng Khoa Đinh
  • 5,038
  • 3
  • 15
  • 33