1

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.

9
  • --host 127.0.0.1 instead of plain 127.0.0.1 or don't specify the host part, since mongo will connect to localhost by default Commented Dec 27, 2012 at 13:04
  • C:\[…]\mongo.exe --port 20571 -u test -p test --host 127.0.0.1 gets me another error: MongoDB shell version: 2.0.4 connecting to: 127.0.0.1:20571/test Thu Dec 27 14:07:06 uncaught exception: login failed exception: login failed Commented Dec 27, 2012 at 13:06
  • try doing: use admin db.auth('test','test'), additionally to the db.addUser(...) because you added the user, but not giving any auth privileges Commented Dec 27, 2012 at 13:18
  • restarted db with auth = false, executed what you suggested, restarted with auth = true, can't connect. Same error as in my comment above. Commented Dec 27, 2012 at 13:24
  • try doing: use test and then db.addUser('test',test) and restart the mongo instance and try to connect to it with --auth Commented Dec 27, 2012 at 13:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

The configuration was loaded successfully. See comments under my question. Sorry for not marking it as solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.