dave
dave

Reputation: 4102

MongoDB with Authentication

I am trying to get MongoDB running on my localhost (Windows) with authentication.

To do so, I first have to add a user, right? I did so by starting the daemon using this command:

C:\[…]\mongod.exe -f C:\[…]\mongo.config

mongo.config contains the following:

# Basic database configuration
dbpath = C:\[…]\db\
bind_ip = 127.0.0.1
port = 20571

# Security
noauth = true

# Administration & Monitoring
nohttpinterface = true

After that I connected via this command:

C:\[…]\mongo.exe --port 20571 127.0.0.1

There I added a user:

> use admin
switched to db admin
> db.addUser('test', 'test')
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
    "user" : "test",
    "readOnly" : false,
    "pwd" : "a6de521abefc2fed4f5876855a3484f5",
    "_id" : ObjectId("50db155e157524b3d2195278")
}

To check if everything worked I did the following:

> db.system.users.find()
{ "_id" : ObjectId("50db155e157524b3d2195278"), "user" : "test", "readOnly" : false, "pwd" : "a6de521abefc2fed4f5876855a3484f5" }

Which seemed OK to me.

After that I changed "noauth = true" to "auth = true" in the mongo.config file and restarted the daemon.

Now I expected to be able to connect with user and password:

C:\[…]\mongo.exe --port 20571 -u test -p test 127.0.0.1

Which denied access to me with this message:

MongoDB shell version: 2.0.4
connecting to: 127.0.0.1:20571/127.0.0.1
Wed Dec 26 16:24:36 uncaught exception: error { "$err" : "bad or malformed command request?", "code" : 13530 }
exception: login failed

So here's my question: Why does the login fail?

I can still connect without providing user and password, but can't access any data because "unauthorized db:admin lock type:-1 client:127.0.0.1". Which is actually what I expected.

Upvotes: 1

Views: 9959

Answers (2)

dave
dave

Reputation: 4102

As Andrei Sfat told me in the comments on the question I made 2 major errors.

First, I thought I could pass the IP to the Client as a simple argument. But you have to use --host for that. Instead, the parameter I thought was the IP address actually should be the db name. So the correct command to connect to a Server is as follows:

C:\[…]\mongo.exe --port 20571 -u test -p test --host 127.0.0.1 admin

Second, users are per database. As I only added the user "test" to the db "admin", it only works there.

Upvotes: 2

coderLMN
coderLMN

Reputation: 3076

Obviously the auth = true configuration wasn't load successfully. Did you forget the -f paramter when you restart the mongod.exe?

C:\[…]\mongod.exe -f C:\[…]\mongo.config

Upvotes: 0

Related Questions