I have successfully enabled authorization on MongoDB and I have created an account on the admin database and then I created an account for my database called test. The following connection string to connect to my test database works successfully: mongo --host 192.168.17.52 --port 27017 -u user1 -p password --authenticationDatabase test
Only problem I have now is, I cannot execute commands such as: show dbs. I get the following error when I try to do so:
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0, lsid: { id: UUID(\"a1d5bc0d-bc58-485e-b232-270758a89455\") }, $db: \"admin\" }"
I have been on many online sources to help fix this issue but no luck, is there a way to resolve this issue? Seems like my user can't access the admin database, is there a way to grant this access to my user so I can run the necessary commands like show dbs?
Any help is much appreciated! :)
In order to run show dbs command and if the user has access to multiple databases, first the user should be created on the admin database (this is because listDatabases action is a cluster wide operation). Also the user should be given access to this operation. In order to do that, a new role should be created with the action. Below are the steps for the same:
//login as admin with --authenticationDatabase "admin" (assumption is that admin user is with root privileges) and then run the below:
use admin;
db.runCommand({ createRole: "listDatabases", privileges: [{ resource: { cluster : true }, actions: ["listDatabases"]} ], roles: [] });
db.createUser({user:"testUser", pwd:"passwd", roles:[{role:"read", db:"db1"},{role:"read", db:"db2"},{ role: "listDatabases", db: "admin" }]});
//exit as admin user and login as testUser: note the --authenticationDatabase "admin"
mongo -u "testUser" -p --authenticationDatabase "admin"
after logging in run the command below and it should list all the databases:
show dbs;
The below will work fine even though user is not given access to admin database:
use admin;
But then the below will give error:
show collections;
The problem is related the database you are using with the --authenticationDatabase parameter.
You are connecting to mongo with the user of your test database who has no privileges to execute listDatabase commands.
Let's do this using the admin db as auth db
mongo --host 192.168.17.52 --port 27017 -u user1 -p password --authenticationDatabase admin
and then run the command
show dbs
Related
I created a default admin user with the following command in mongoshell:
use admin
db.createUser(
{
user: "root",
pwd: "root",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
Then I authenticated using the shell and my credentianls as following:
mongo --port 27017 -u "root" -p "root" --authenticationDatabase "admin"
In the shell I am able to see the users and version collections using:
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> use admin
switched to db admin
> show tables
system.users
system.version
>
Now when I try to access these tables from MongoDB Compass Community I first authenticate:
Now the admin database is showing up empty?
How can I access the users collection and the other collections from the graphic interface just as I did from the shell?
The reason why you cannot see the user collection and other collections is the userAdminAnyDatabase built-in role. By default It does not need the necessary rights to read those collections.
Try root or dbAdminAnyDatabase depends on your needs.
Cheers,
Resource:
https://docs.mongodb.com/manual/reference/built-in-roles/
In short : Use "root" in role instead of "userAdminAnyDatabase" and try again.
If use was set this config in "C:\Program Files\MongoDB\Server\4.4\bin\mongod.cfg"
security:
authorization: enabled
You need to comment these 2 line by insert # at start of lines.
Then restart service of "MongoDB Server (MongoDB)".
Open Cmd type these command line by line
mongo
use admin
db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])
Then uncomment security and authorization in mongod.cfg and restart service "MongoDB Server (MongoDB)" again.
You can see how to create or update user role in this answer
Thanks Dan Dascalescu.
https://stackoverflow.com/a/29472184/4418749
I know this is a basic security measure, but I can't make it works with my mongodb installation. Each time I run the command :
mongorestore dump_folder -u "username" -p "password"
It returns:
error reading database: not authorized on DATABASE_NAME to execute command { listCollections: 1, cursor: { batchSize: 0 } }
My question is : What is the permission that I need to enable and how ?
For the time being, I just turn off the Mongo's security in /etc/mongod.conf and start it back after I restore/dump my databases.
Additional Note:
This is how I prepared my databases and its user
I log in with my admin account, create a database, named DATABASE001
And create a user:
use DATABASE001
db.createUser({
user: "USER001",
pwd: "mypassword",
roles:[ { role: "readWrite", db: "DATABASE001" },"dbAdmin" ]
})
For roles, I add readWrite to DATABASE001 and also dbAdmin
I want this user (USER001) be able to backup or restore his database (DATABASE001).
I tested it by login with this command. It successfully logged in
mongo localhost -u USER001 -p mypassword --authenticationDatabase DATABASE001
I want to dump this database content with:
mongodump -u USER001 -p mypassword --authenticationDatabase DATABASE001
it throws me an error:
Failed: error getting database names: not authorized on admin to execute command { listDatabases: 1 }
What is this listDatabases ? how can I add it to my roles?
$ mongorestore --username adminUser --password pass123 --authenticationDatabase admin --db exampleDB dump/exampleDB
I have mongod server up and running as a service on ubuntu 14.04, i started mongo shell and created a user.
db.createUser( {
user: "testAdmin",
pwd: "12345678",
roles: [ { role: "userAdmin", db: "students" } ]
} )
after that I changed edit the mongod.conf and set auth=true and restarted the mongod server as a service.
After all this, when I want to start the mongo shell with user testAdmin .
mongo -u testAdmin -p 12345678 --authenticationDatabase students
But I am not able to login and it is giving me error.
MongoDB shell version: 3.2.10
connecting to: 127.0.0.1:25018/test
2016-10-13T05:56:27.500+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:25018, reason: errno:111 Connection refused
2016-10-13T05:56:27.500+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:25018, connection attempt failed :
First when I am specifying database students why it is trying to connect to test? Second I know that userAdmin role in mongodb is not for read/write in database with userAdmin I can only manage user roles. But to manage other users i still need to login in mongo shell.
It is working when I turn off the auth in mongod.conf, so other things are fine, only authentication is not working.
First when I am specifying database students why it is trying to connect to test?
You are not specifying the database (students) you want to connect to here.
The --authenticationDatabase is used to specify the database where the user had been created and which is used for the authentication process.
You should also specify students as the db option you want to connect to. If you omit it, the shell automatically tries to connect to test database:
mongo students -u testAdmin -p 12345678 --authenticationDatabase students
Note the addition of students just after mongo.
This is assuming the user has been created in the students database (and not admin or other). Note that in that case the db declaration within role is redundant:
roles: [ { role: "userAdmin", db: "students" } ]
could have also been specified as:
roles: [ "userAdmin" ]
I'm rather new with MongoDB and I'm trying to add a admin account into my MongoDB whithout any users at this point.
Using this command: mongo --port 27017 -u manager -p 123456 --authenticationDatabase admin
I receive this error:
Kind regards.
Let me explain the reason why its not working. mongo db refers to users collection to validate the login user.
By default mongodb doesnt provide any user login like manager, using which your were trying to login
Thus the below command is not working
mongo --port 27017 -u manager -p 123456 --authenticationDatabase admin
To make the below command to work. You need to add user "manager" to mongodb user collection.
To create user run the below command:
db.addUser( { user: "manager",
pwd: "123456",
roles: [ "userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase"
] } )
Once you have the user created, below command work.
mongo --port 27017 -u manager -p 123456 --authenticationDatabase admin
I am having an issue with mongo db 3.0.4 that randomly started happening today. The authentication stopped working randomly without any user action on our part. Our setup is easy. One application, one database. I take the application out of the equation and I still get the same result. Here are the steps I am taking. Reinstalled mongo and pointed the logpath and dbpath to a new folder to start from scratch.
Create the config file.
logpath=c:\data\logs\mongod.log
dbpath=c:\data\db
Log in to mongo shell and run this command.
db.createUser({user: "admin",pwd: "admin", roles: [ { role: "userAdminAnyDatabase", db: "admin" }]})
Add the auth=true in the config file.
auth=true
Restart the mongodb service.
Start up mongo shell with
mongo -u admin -p admin --authenticationDatabase admin
Then I get the Error 18 Authentication failed.
I am really lost for words. Any help would be greatly appreciated.
Thanks
i think user doesn't have efficient privileges. try with this role.
use admin
{user:"admin",pwd:"admin",roles:["root"]}
Before create the user, you need to change the db and create it in admin db.
Disable auth
Enter to mongo shell
Change db to admin ('use admin')
now create the user.
Enable the auth again (with auth or mongodb-keyfile)
Enter mongo shell with:
mongo -u admin -p admin --authenticationDatabase admin