How to configure MongoDB to be able to connect using username and password?

176 views Asked by At

I've a VMWare VM with Ubuntu 16.04.5 and just installed MongoDb 3.6 (not the latest, but it matches production environment)

Inside remote shell, I can run mongo and so my mongod service is working.

This is parts of /etc/mongod.conf file about auth

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, 192.168.1.50

security:
  suthorization: enabled

I created this user into the db admin

> use admin

switched to db admin

> show users

{
        "_id" : "admin.realtebo",
        "user" : "realtebo",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}

Then I restarted mongod service.

The problem

I cannot no more access using mongo shell client, it refuses my connection

mongo -u realtebo -p passsword_i_setup_for_user_realtebo

MongoDB shell version v3.6.9
connecting to: mongodb://127.0.0.1:27017

2019-01-16T09:06:51.416+0100 W NETWORK  [thread1] 
  Failed to connect to 127.0.0.1:27017, 
  in(checking socket for error after poll), reason: Connection refused

2019-01-16T09:06:51.416+0100 E QUERY    [thread1] 
  Error: couldn't connect to server 127.0.0.1:27017, 
  connection attempt failed :
    connect@src/mongo/shell/mongo.js:257:13
    @(connect):1:6

exception: connect failed

Important notes

  • Sure, removing authorization: true and restarting, I can access again, so the daeom in running well, but I'd like to learn the right way: using user and password is the minimum

  • I verified that this exact user/password combo is working developing a little nodejs app that works using these credentials, and fails without using them, so password is right

  • I tried to remove auth and verified that "Studio 3T" (a mongodb client) on Windows host can succesfully access MongoDb on the guest, so there are no port/firewalls/other problems with the VM

Othe tries

Failed specifying the auth db, admin as you can see some lines above here.

mongo -u realtebo -p passsword_i_setup_for_user_realtebo --authenticationDatabase admin

Simple questions, I hope

What's the right way to create something like a superuser with user and password in mongodb? And then which 'method' must I use from win client and from linux shell client to access it?

I know that I then must create another user, a readWrite I suppose, for a single and specific db, but this is not the actual question. For now I need to be able to login as a super user to manage it

2

There are 2 answers

1
Benoit On BEST ANSWER

It's not an authentication problem, actually the server does not start at all ("Connection refused") because of an error in the /etc/mongod.conf file, should be:

security:
  authorization: enabled

Also it's mandatory to add the option --authenticationDatabase admin when starting a client.

0
realtebo On

It was a so simple stupid error.

  1. I fixed the following typo, note the s instead of a in suthorization

    security:
      suthorization: enabled
    
  2. Restarted service

  3. From linux shell I can succesfully connect using

    mongo -u realtebo -p passsword_i_setup_for_user_realtebo --authenticationDatabase admin
    
  4. From windows client I setup auth method on legacy and entered username and password

Now all works