Nickpick
Nickpick

Reputation: 6597

Mongodb authentication not working from config file

I am trying to enable authentication in my mongodb version v3.4.3 from the config file but it doesn't work.

The following works: sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb

I can access via authentication

The following works as well

sudo service mongod start but only when in the config file (which is /etc/mongod.conf) I don't put authorization: enabled

When I add authorization: enabled (as described in the documentation for that version), I can no longer launch mongod. It gives an error saying that my database folder does not exist, so I assume the config file is corrupt and it doesn't read anymore that the database folder is in /var/lib/mongodb.

Any suggestions are appreciated.

Here the full config file (standard config file, except that I added authorization)

ubuntu@ip-172-31-28-105:~$ cat /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

#processManagement:

#security:
    authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Upvotes: 2

Views: 4538

Answers (2)

Nickpick
Nickpick

Reputation: 6597

There were a few problems:

First of all, the config file always has to be stated when running mongod. Otherwise it takes /data/db as database folder. So the way to run it is via mongod --config /etc/mongod.conf, and the standard configuration contains dbPath: /var/lib/mongodb as database folder.

Second, the config file needs to look like the below. The # in front of the security needs to be removed.

security:
  authorization: enabled

And thirdly, in order for start service mongod start to work, it is necessary to adjust the permission in the mongodb database folder, so the mongodb user has permission to write to them (this is not in the documentation, but probably self-explanatory with anybody who has enough experience). This needs to be done with chown, as the script will use mongdb as user (and no longer sudo).

And fourth, I'm using ubuntu 16, so sudo service mongod start won't work. It needs to be done via sudo systemctl start mongodb. This is also not correctly mentioned in the mongodb documentation.

Upvotes: 4

Parth Vyas
Parth Vyas

Reputation: 507

The mongodb config file is a YAML file. Also, you cannot use tabs in the config file of mongodb. Always use spaces. In the config file, you have commented out the security parameter. Uncomment it and it may work

Change the config file to

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

#processManagement:

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

#Enterprise-Only Options:

#auditLog:

#snmp:

Upvotes: 3

Related Questions