22

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.

Community
  • 1
  • 1
Anjou
  • 341
  • 2
  • 3
  • 16
  • Your permissions for the current user are clearly not correct. This is a new install. Just install fresh without authorization enabled and get the hang of things first. Then **carefully** follow tutorials ( I would recommend the main site documentation over people blog articles ) to set up the authorization correctly. – Blakes Seven Mar 09 '16 at 02:40
  • Have you have users created yet? if not, remove --auth from your config file, restart and try again. – genericuser Mar 09 '16 at 02:58
  • @genericuser As it is a fresh installation, I don't think I have users created. For the configuration file, I don't have one since I used the .msi and apparently you have to create one manually if needed. – Anjou Mar 09 '16 at 03:42
  • @BlakesSeven So I tried to re-install MongoDB following the official documentation (https://docs.mongodb.org/v3.2/tutorial/install-mongodb-on-windows/) but I'm still facing the same problem. Is it possible it has something to do with the connection between mongod.exe and mongo.exe ? Thanks – Anjou Mar 09 '16 at 03:47
  • If you're using **MongoDB Compass**, check out [my solution here](https://stackoverflow.com/a/54971006/10000326) – Mr. Dang Mar 03 '19 at 16:33

8 Answers8

34

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)

Yuci
  • 27,235
  • 10
  • 114
  • 113
  • this gives me [js] Error: Could not find user myUserAdmin@test – Stepan Yakovenko Jun 04 '19 at 03:53
  • @StepanYakovenko, I just tried on version 4.0.9, the recipe still works fine. Wonder at which step you encountered the error you mentioned? – Yuci Jun 04 '19 at 09:43
  • Unfortunately I had to delete entire database to redo this, so I can't really reproduce this now :( – Stepan Yakovenko Jun 05 '19 at 16:30
  • 1
    @StepanYakovenko The reason you might have got that error because you would have forgotten to run `use admin` command because I got the same error and upon checking I forgot to run that command after connecting. – Vignesh S Aug 25 '19 at 10:24
16

one more, after you create user by following cmd-1, please assign read/write/root role to the user by cmd-2. then restart mongodb by cmd "mongod --auth".

The benefit of assign role to the user is you can do read/write operation by mongo shell or python/java and so on, otherwise you will meet "pymongo.errors.OperationFailure: not authorized" when you try to read/write your db.

cmd-1:

use admin
db.createUser({
  user: "newUsername",
  pwd: "password",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})

cmd-2:

db.grantRolesToUser('newUsername',[{ role: "root", db: "admin" }])
Haisheng Yu
  • 191
  • 1
  • 4
  • Thanks - this answer did resolve my issue - executing 3.4.10 community edition. Simply adding read role did not resolve the authorization issue. For others: I recommend reading the following [page](https://docs.mongodb.com/manual/core/security-built-in-roles/) to understand why this change is needed. – LMA1980 Nov 27 '17 at 22:00
14

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/

Istiak Morsalin
  • 10,621
  • 9
  • 33
  • 65
  • 1
    If I understand correctly, MongoDB provides an exception when creating the first user for the admin database. So after a fresh install, I launched mongod.exe then mongo.exe, I switched to the admin db with ''use admin'' and I tried to create an user with the code you provided. However, I still have the error message saying : not authorized on admin to execute command. Is it possible that my client is not connected to the server ? As I closed mongod.exe with Ctrl+C but I was still able to type command on mongo.exe. Shouldn't it say something like "disconnected" ? Thank you – Anjou Mar 09 '16 at 03:32
  • It should obviously say not connected. You should not be able to run mongo without running mongod. – Istiak Morsalin Mar 09 '16 at 06:13
  • Yes, there was an issue with the connection as a process was listening on the default port even with mongod/mongo shell closed. I changed the default port to one free and everything is now working. Thank you for your help – Anjou Mar 09 '16 at 06:15
  • Please Put a tick mark at the answer also. You are welcome. @Anjou – Istiak Morsalin Mar 09 '16 at 06:35
  • Accept the answer as right if you think it was worked for you, May be that is a courtesy of this community . – Istiak Morsalin Mar 09 '16 at 06:51
  • @JasonBourne I have one question to this old question. If I create admin user like above that's mean that ebery new user will be admin? – Mat.Now Mar 10 '17 at 10:35
  • @V.R yes. But you can change role if you want – Istiak Morsalin Mar 10 '17 at 13:18
8

Copy of answer OP posted in question:

Solution

After the update from the previous edit, I looked a bit about the connection between client and server and I found out that even when mongod.exe was not running, there was still something listening on port 27017 with netstat -a

So I tried to launch the server with a random port using

[dir]mongod.exe --port 2000

Then the shell with

[dir]mongo.exe --port 2000

And this time, the server printed a message saying there is a new connection. I typed few commands and everything was working perfectly fine, I started the basic tutorial from the documentation to check if it was ok and for now it is.

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Thanks spent too much time on this. On windows 10 message queuing seems to be enabled mqsvc.exe which was using port 27017. – dan Jun 01 '17 at 20:10
1

There are two things,

1) You can run the mongodb instance without username and password first.

2) Then you can add the user to the system database of the mongodb which is default one using the query below.

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

Thanks.

Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54
Amit Shah
  • 1,380
  • 1
  • 10
  • 19
1

Try this...

use admin;
db.createUser({
   user: 'admin',
   pwd: 'SecurePass',
   roles: [ { role: 'root', db: 'admin' } ]
});
db.auth("admin", "SecurePass");
db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])

db.auth() before db.grantRolesToUser()

solve my problem :)

if it doesn't work try to change the role to 'readWrite'

0

for me it worked by adding

1) "You can run the mongodb instance without username and password first.---OK

2) "Then you can add the user to the system database of the mongodb which is default one using the query below".---OK

db.createUser({
  user: "myUserAdmin",
  pwd: "abc123",
  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ],
  mechanisms:[ "SCRAM-SHA-1" ] // I added this line

})
hoque
  • 5,735
  • 1
  • 19
  • 29
-1

It was Docker running in the background in my case. If you have Docker installed, you may wanna close it and try again.

Mehmet N. Yarar
  • 576
  • 9
  • 17