1

I have created a superuser tom in mongodb v2.6.11 with username & password and now i want to add this at ubuntu startup service

on searching came to know that i have to edit below file

this is how my /etc/init/mongod.conf looks

pre-start script
    mkdir -p /var/lib/mongodb/
    mkdir -p /var/log/mongodb/
end script

start on runlevel [2345]
stop on runlevel [06]

script
  ENABLE_MONGOD="yes"
  CONF=/etc/mongod.conf
  DAEMON=/usr/bin/mongod
  DAEMONUSER=${DAEMONUSER:-mongodb}

  if [ -f /etc/default/mongod ]; then . /etc/default/mongod; fi

  # Handle NUMA access to CPUs (SERVER-3574)
  # This verifies the existence of numactl as well as testing that the command works
  NUMACTL_ARGS="--interleave=all"
  if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
  then/etc/init/mongod.conf
    NUMACTL="$(which numactl) -- $NUMACTL_ARGS"
    DAEMON_OPTS=${DAEMON_OPTS:-"--config $CONF"}
  else
    NUMACTL=""
    DAEMON_OPTS="-- "${DAEMON_OPTS:-"--config $CONF"}
  fi

  if [ "x$ENABLE_MONGOD" = "xyes" ]
  then
    exec start-stop-daemon --start --chuid $DAEMONUSER --exec $NUMACTL $DAEMON $DAEMON_OPTS
  fi
end script

ATTEMPT 1 :by using mongo command line

sudo mongod --auth --port 27017 --dbpath /var/lib/mongodb

but don't know what to change in /etc/init/mongod.conf ?


ATTEMPT 2 :by using /etc/mongod.conf

  1. Did Changes In /etc/mongod.conf

auth = true

port = 27017

  1. $sudo mongod --config /etc/mongod.conf

  2. /var/log/mongodb/mongod.log after firing command

    2016-01-20T23:58:41.675+0530 ***** SERVER RESTARTED ***** 2016-01-20T23:58:41.677+0530 [initandlisten] MongoDB starting : pid=3168 port=27017 dbpath=/var/lib/mongodb 64-bit host=vijay 2016-01-20T23:58:41.677+0530 [initandlisten] db version v2.6.11 2016-01-20T23:58:41.677+0530 [initandlisten] git version: d00c1735675c457f75a12d530bee85421f0c5548 2016-01-20T23:58:41.677+0530 [initandlisten] build info: Linux build4.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49 2016-01-20T23:58:41.677+0530 [initandlisten] allocator: tcmalloc 2016-01-20T23:58:41.677+0530 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } } 2016-01-20T23:58:41.812+0530 [initandlisten] journal dir=/var/lib/mongodb/journal 2016-01-20T23:58:41.812+0530 [initandlisten] recover : no journal files present, no recovery needed 2016-01-20T23:58:41.934+0530 [initandlisten] waiting for connections on port 27017 2016-01-20T23:58:47.746+0530 [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends 2016-01-20T23:58:47.746+0530 [signalProcessingThread] now exiting 2016-01-20T23:58:47.746+0530 [signalProcessingThread] dbexit: 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: going to close listening sockets... 2016-01-20T23:58:47.746+0530 [signalProcessingThread] closing listening socket: 10 2016-01-20T23:58:47.746+0530 [signalProcessingThread] closing listening socket: 13 2016-01-20T23:58:47.746+0530 [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: going to flush diaglog... 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: going to close sockets... 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: waiting for fs preallocator... 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: lock for final commit... 2016-01-20T23:58:47.746+0530 [signalProcessingThread] shutdown: final commit... 2016-01-20T23:58:47.838+0530 [signalProcessingThread] shutdown: closing all files... 2016-01-20T23:58:47.843+0530 [signalProcessingThread] closeAllFiles() finished 2016-01-20T23:58:47.843+0530 [signalProcessingThread] journalCleanup... 2016-01-20T23:58:47.843+0530 [signalProcessingThread] removeJournalFiles 2016-01-20T23:58:47.949+0530 [signalProcessingThread] shutdown: removing fs lock... 2016-01-20T23:58:47.949+0530 [signalProcessingThread] dbexit: really exiting now

  3. But on closing terminal mongodb also closes

ATTEMPT 3

  1. Did Changes In /etc/mongod.conf

auth = true

port = 27017

  1. RESTART COMPUTER

  2. /var/log/mongodb/mongod.log after RESTARTING COMPUTER

    2016-01-21T00:40:13.011+0530 ***** SERVER RESTARTED ***** 2016-01-21T00:40:13.014+0530 [initandlisten] MongoDB starting : pid=1012 port=27017 dbpath=/var/lib/mongodb 64-bit host=vijay 2016-01-21T00:40:13.014+0530 [initandlisten] db version v2.6.11 2016-01-21T00:40:13.014+0530 [initandlisten] git version: d00c1735675c457f75a12d530bee85421f0c5548 2016-01-21T00:40:13.014+0530 [initandlisten] build info: Linux build4.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49 2016-01-21T00:40:13.014+0530 [initandlisten] allocator: tcmalloc 2016-01-21T00:40:13.014+0530 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, security: { authorization: "enabled" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } } 2016-01-21T00:40:13.439+0530 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /var/lib/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating 2016-01-21T00:40:13.439+0530 [initandlisten] dbexit: 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: going to close listening sockets... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: going to flush diaglog... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: going to close sockets... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: waiting for fs preallocator... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: lock for final commit... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: final commit... 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: closing all files... 2016-01-21T00:40:13.439+0530 [initandlisten] closeAllFiles() finished 2016-01-21T00:40:13.439+0530 [initandlisten] shutdown: removing fs lock... 2016-01-21T00:40:13.439+0530 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor 2016-01-21T00:40:13.440+0530 [initandlisten] dbexit: really exiting now

  3. Observation : Unable to create/open lock file: /var/lib/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating I found this Error in log .Don't know if it causing mongodb to terminate.if it is causing mongo to STOP at computer start then how to set permisssion ?

Mr punch
  • 1,756
  • 4
  • 16
  • 23

4 Answers4

2

Alternatively possible to use supervisor:

apt install supervisor

Setting for process mongodb:

create file myupstartservice.conf at location /etc/supervisor/conf.d and put below code

[program:mongo]
command=/usr/bin/mongod --auth --config /etc/mongod.conf
autostart=true
autorestart=true
user=root
priority=100

then checkservice

service supervisor restart

some reference

Mr punch
  • 1,756
  • 4
  • 16
  • 23
alex10
  • 2,726
  • 3
  • 22
  • 35
  • Why the heck should one use supervisor on top of upstart on top of init? – Markus W Mahlberg Jan 18 '16 at 20:23
  • It is much easier to configure, which greatly simplifies life. – alex10 Jan 18 '16 at 20:32
  • And adds a moving part totally unnecessary, see [my answer](http://stackoverflow.com/a/34863984/1296707). – Markus W Mahlberg Jan 18 '16 at 20:55
  • Oh, and btw: you should never, ever run MongoDB as root. – Markus W Mahlberg Jan 18 '16 at 21:03
  • well, this is just an example ;) – alex10 Jan 18 '16 at 21:07
  • Alex, forgive me, but to get it running somehow isn't the best idea. An example? Every attacker able to gain access to the MongoDB instance in question might acquire root rights at the machine if OP used the configuration you suggested. Do not assume that suggestions will be reflected. Often enough, it is just c&p. With mongod running as root, there is no 5% security margin of disk space: Your mongodb might fill the FS, making it impossible to do further administrative tasks, forcing you to run from a live system. Will it run as root via supervisor? Yes. Is it a good idea? Not so much. – Markus W Mahlberg Jan 18 '16 at 21:17
  • ^ blah blah blah. I compiled mongo from source because new versions aren't available for Raspberry pi. Because of this, I had to make my own initialization setup. Might as well use supervisor or systemD because why use upstart? – Felipe Jan 14 '18 at 01:20
2

There is absolutely no need to fiddle with the startup files (which you really should not do unless you exactly know what you are doing) or install additional software for managing MongoDB.

I'd rather suggest to read MongoDB's extensive documentation of the config file options.

The only thing you need to do is to set

auth=true

for legacy config files or

security:
  authorization: enabled

for YAML config files in /etc/mongod.conf

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • @plutopunch Well, then I have to tell all my customers... And the other DBAs... And MongoDB Inc.... ;) the parameter is _exactly_ there for that purpose, please add the log output by [editing your question](http://stackoverflow.com/posts/34861198/edit). – Markus W Mahlberg Jan 19 '16 at 11:19
  • Instead of star int it manually, simply use `service MongoDB restart` to apply config changes. – Markus W Mahlberg Jan 21 '16 at 18:54
1

Thanks @Markus W mahalberg extending your answer in detailed steps

Correct Mongodb Way to set mongo in startup with authentication


Steps :

1 : sudo apt-get install mongodb-org - in new terminal

2 : sudo mongod --port 27017 --dbpath /var/lib/mongodb

3 : mongo --port 27017 - in new terminal

4 : use admin

5 : Lets setup superuser

db.createUser(
{
    user: "tom",
    pwd: "jerry",
    roles: [
              { role: "userAdminAnyDatabase", db: "admin" },
              { role: "readWriteAnyDatabase", db: "admin" },
              { role: "dbAdminAnyDatabase", db: "admin" },
              { role: "clusterAdmin", db: "admin" }
           ]
})

6 : Lets configure for Startup service for that Do Changes In /etc/mongod.conf and set

auth = true

port = 27017

7 : sudo /etc/init.d/mongod stop OR sudo service mongod stop - in new terminal

8 : sudo /etc/init.d/mongod start OR sudo service mongod start

9 : restart your pc and before restarting your pc please empty this file /var/log/mongodb/mongod.log and save

Now lets check TWO things Authentication & mongodb running at startup

1 :Check Authentication setup

mongo --port 27017 -u "tom" -p "jerry" --authenticationDatabase "admin" - in new terminal

Note : this step is most important step .

it will give Output on terminal like

MongoDB shell version: 2.6.11
connecting to: 127.0.0.1:27017/test
>

2 :Check if mongodb is running at startup

Run below command without running mongod as mongod should have started in startup

mongo --port 27017 -u "tom" -p "jerry" --authenticationDatabase "admin" - in new terminal

If it dosent start then open this file '/var/log/mongodb/mongod.log' .

if it contains Error log :

Unable to create/open lock file: /var/lib/mongodb/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

Then apply these permissions and restart and again check

sudo chown -R mongodb:mongodb /var/lib/mongodb/.
sudo chown -R mongodb:mongodb /var/log/mongodb/mongod.log
sudo /etc/init.d/mongod stop` OR `sudo service mongod stop    
sudo /etc/init.d/mongod start` OR `sudo service mongod start 

If it does not contains above error log then you should have successfully started mongodb at startup

Mr punch
  • 1,756
  • 4
  • 16
  • 23
0

1 : Got to -> $cd /etc/init/

2 :Through vi Or nano editor Create file myservice.conf with below code

description "service to start mongodb at startup"
author "plutopunch :)"

start on started mountall
stop on shutdown

respawn
respawn limit 99 5

script
     export HOME="/home/admin"

     exec sudo mongod --port 27017 --auth --dbpath /var/lib/mongodb
end script

post-start script
end script

3 : restart pc

http://upstart.ubuntu.com/cookbook/

Mr punch
  • 1,756
  • 4
  • 16
  • 23