MongoDB Service Won’t Start – Adding Admin User in Ubuntu 16.04

authenticationmongodbUbuntu

I have an ubuntu 16.04 and I have installed mongodb on it yesterday with sudo aptitude install mongodb; it worked very well at start. I decided to add authentication info to mongodb, so I opened a session to it writing mongo in the shell and I entered these commands:

use admin
db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})

It said that user has been added successfully. I entered exit to set configurations in order to use authentication while starting service. I edited vim /lib/systemd/system/mongod.service; it now contains:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --auth 

[Install]
WantedBy=multi-user.target

I entered systemd daemon-reload in the shell after that, then service mongod restart; but unfurtunately mongodb never started. When I try to connect to mongo using command mongo -u admin -p admin123 --authenticationDatabase admin, it shows me:

mongo -u admin -p admin123 --authenticationDatabase admin
MongoDB shell version: 2.6.10
connecting to: test
2017-08-08T10:37:52.751+0430 warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2017-08-08T10:37:52.751+0430 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

It's saying no server is running (connect failed); additionally I don't know why it's trying to connect to test while I'm inserting admin?! However command systemctl status mongodb shows following:

● mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Mon 2017-08-07 16:02:30 IRDT; 18h ago
     Docs: man:mongod(1)
 Main PID: 11367 (code=exited, status=0/SUCCESS)

Aug 07 15:33:14 ZiZi systemd[1]: Started An object/document-oriented database.
Aug 07 16:02:30 ZiZi systemd[1]: Stopping An object/document-oriented database...
Aug 07 16:02:30 ZiZi systemd[1]: Stopped An object/document-oriented database.

No log is written in file /var/log/mongodb/mongodb.log.

When I enter ExecStart in the shell, I can successfully start mongo:

    sudo /usr/bin/mongod --quiet --auth 
2017-08-08T10:53:27.358+0430 [initandlisten] MongoDB starting : pid=29830 port=27017 dbpath=/data/db 64-bit host=ZiZi
2017-08-08T10:53:27.359+0430 [initandlisten] db version v2.6.10
2017-08-08T10:53:27.359+0430 [initandlisten] git version: nogitversion
2017-08-08T10:53:27.359+0430 [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-08-08T10:53:27.359+0430 [initandlisten] build info: Linux lgw01-12 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 BOOST_LIB_VERSION=1_58
2017-08-08T10:53:27.359+0430 [initandlisten] allocator: tcmalloc
2017-08-08T10:53:27.359+0430 [initandlisten] options: { security: { authorization: "enabled" }, systemLog: { quiet: true } }
2017-08-08T10:53:27.360+0430 [initandlisten] journal dir=/data/db/journal
2017-08-08T10:53:27.360+0430 [initandlisten] recover : no journal files present, no recovery needed
2017-08-08T10:53:27.431+0430 [initandlisten] waiting for connections on port 27017

EDIT 1:

I have re-installed mongodb using instructions described here; it is now on its latest version:

mongod --version
    db version v3.4.7
    git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
    OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
    allocator: tcmalloc
    modules: none
    build environment:
        distmod: ubuntu1604
        distarch: x86_64
        target_arch: x86_64

I have added authentication again; my /lib/systemd/system/mongod.service is now this:

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false

# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

And this is my /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: 127.0.0.1


#processManagement:

security:
  auth: true

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

User add process was successful:

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "myUserAdmin",
...     pwd: "abc123",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
    "user" : "myUserAdmin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

I have reloaded systemctl and restarted mongod service, but mongod service still fails to start. I don't have --quiet option, but there is no log in the /var/log/mongodb/mongod.log file. This is what service mongod status returns:

service mongod status
● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2017-08-09 16:12:42 IRDT; 1min 0s ago
     Docs: https://docs.mongodb.org/manual
  Process: 2030 ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf (code=exited, status=2)
 Main PID: 2030 (code=exited, status=2)

Aug 09 16:12:42 ZiZi systemd[1]: Stopped High-performance, schema-free document-oriented database.
Aug 09 16:12:42 ZiZi systemd[1]: Started High-performance, schema-free document-oriented database.
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Unit entered failed state.
Aug 09 16:12:42 ZiZi systemd[1]: mongod.service: Failed with result 'exit-code'.

It implies that I have an INVALIDARGUMENT which I can't detect. Any help?

Best Answer

There can be multiple reasons why mongod didn't start but..

First, reason, why you don't get any lines to mongodb.log, is that --quiet -parameter. I wouldn't use it because it makes much harder to recognize what is the source of problems. So remove it and try again, this time there should be lines in log file what tell what went wrong.

Second, at least in my Ubuntu mongo installation that starting service line is ExecStart=/usr/bin/mongod --config /etc/mongod.conf what means that all configuration is done in /etc/mongod.conf -file, what is anyway best place to configure service, because that file is not overwritten during updates.

Change (in mongod.conf) that auth:true to authorization:enabled (remember keep those space characters before authorization -word).