Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show dbs gives "Not Authorized to execute command" error

Tags:

I've spent some time trying to figure out what is wrong but as I could not find out, I decided to ask here.

I am running MongoDB(Windows 64-bit 2008 R2+) version 3.2.3 on Windows 8, the paths are :

C:\MongoDB\bin for the installation

C:\data\db for the data folder

I've installed following this video and this tutorial from the official documentation.

The first issue might be, as I don't really know if it is an issue at all, the connection between the client(mongo.exe) and the server(mongod.exe).

I lauched mongod.exe via the command line (with administrator rights), everything went fine, I got the message :

waiting for connections on port 27017 

but when I launch mongo.exe via a new instance of the command line, the server(mongod.exe) doesn't print a message saying there is a new connection (it was the case in the tutorials I watched)

On the other side, mongo.exe prints

connecting to : test 

I don't know if everything is correct at this point but I still tried some basics commands like :

show dbs returns not authorized on admin to execute command

Basically, all the commands I tried had the same error message, even with "fresh" db I just created with use 'dbName'

Some answers online said I have to create a user with proper roles, I tried this one. Still the same error message not authorized to execute command

My question is the following :

Is is normal that mongod.exe doesn't show a new connection when I launch mongo.exe ? If it is correct then what can I do to make even the basic commands work ?

Additional Informations :

I tried to uninstall/re-install few times, with the "Custom mode" and the "Complete mode" in the Windows installer but it always lead to the same problem.

I also tried to create a MongoDB Service following the official documentation but I'm not really sure if it was a good idea. (I can't add more links but it is in a section in the second link I shared.

Edit section :

I decided to try it on another computer which I have not touched for years, running on Windows 7 64-bit.

I copied the MongoDB installation folder at the root of this computer, created \data\db folder and launched mongod.exe.

Then I launched mongo.exe and this time, mongod.exe printed a message saying there is a new open connection which it doesn't on my actual computer. I think the problem is here because I was able to start the basic tutorial from the official documentation and perform simple commands like create a new db, insert, find, show dbs, etc. Everything that I am not able to do on my actual computer.

So I think the problem is coming from the connection between mongod.exe and mongo.exe

Do you have any idea how I could solve this problem as I have tried uninstalling few times.

like image 275
Anjou Avatar asked Mar 09 '16 02:03

Anjou


2 Answers

You should have started the mongod instance with access control, i.e., the --auth command line option, such as:

$ mongod --auth 

Let's start the mongo shell, and create an administrator in the admin database:

$ mongo > use admin > db.createUser(   {     user: "myUserAdmin",     pwd: "abc123",     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]   } ) 

Now if you run command "db.stats()", or "show users", you will get error "not authorized on admin to execute command..."

> db.stats() {         "ok" : 0,         "errmsg" : "not authorized on admin to execute command { dbstats: 1.0, scale: undefined }",         "code" : 13,         "codeName" : "Unauthorized" } 

The reason is that you still have not granted role "read" or "readWrite" to user myUserAdmin. You can do it as below:

> db.auth("myUserAdmin", "abc123") > db.grantRolesToUser("myUserAdmin", [ { role: "read", db: "admin" } ]) 

Now You can verify it (Command "show users" now works):

> show users {         "_id" : "admin.myUserAdmin",         "user" : "myUserAdmin",         "db" : "admin",         "roles" : [                 {                         "role" : "read",                         "db" : "admin"                 },                 {                         "role" : "userAdminAnyDatabase",                         "db" : "admin"                 }         ] } 

Now if you run "db.stats()", you'll also be OK:

> db.stats() {         "db" : "admin",         "collections" : 2,         "views" : 0,         "objects" : 3,         "avgObjSize" : 151,         "dataSize" : 453,         "storageSize" : 65536,         "numExtents" : 0,         "indexes" : 3,         "indexSize" : 81920,         "ok" : 1 } 

This user and role mechanism can be applied to any other databases in MongoDB as well, in addition to the admin database.

(MongoDB version 3.4.3)

like image 97
Yuci Avatar answered Sep 22 '22 05:09

Yuci


Create a user like this:

db.createUser(       {         user: "myUserAdmin",         pwd: "abc123",         roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]       }     ) 

Then connect it following this:

mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin" 

Check the manual :

https://docs.mongodb.org/manual/tutorial/enable-authentication/

like image 24
Istiak Morsalin Avatar answered Sep 22 '22 05:09

Istiak Morsalin