how to set mogodb in upstart service with authentication - mongodb

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
Did Changes In /etc/mongod.conf
auth = true
port = 27017
$sudo mongod --config /etc/mongod.conf
/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
But on closing terminal mongodb also closes
ATTEMPT 3
Did Changes In /etc/mongod.conf
auth = true
port = 27017
RESTART COMPUTER
/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
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 ?

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

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

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

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/

Related

How do you restart MongoDB server and closing the terminal on Cloud 9

I am developing in which I am using MongoDB as my database and I use the the following commands to install on the terminal on cloud 9.
$ sudo apt-get install -y mongodb-org
$ mkdir data
$ echo 'mongod --bind_ip=$IP --dbpath=data --nojournal --rest "$#"' > mongod
$ chmod a+x mongod
$ ./mongod
This worked fine and I closed the terminal after I finished. Then I got back to it and I just typed: $ ./mongod in order to run the program but it's giving me the following error:
2017-12-24T07:02:40.787+0000 ** WARNING: --rest is specified without --httpinterface,
2017-12-24T07:02:40.787+0000 ** enabling http interface
warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
2017-12-24T07:02:40.790+0000 [initandlisten] MongoDB starting : pid=15545 port=27017 dbpath=data 64-bit host=traorefly-real-5786569
2017-12-24T07:02:40.790+0000 [initandlisten] db version v2.6.12
2017-12-24T07:02:40.790+0000 [initandlisten] git version: d73c92b1c85703828b55c2916a5dd4ad46535f6a
2017-12-24T07:02:40.790+0000 [initandlisten] build info: Linux build5.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
2017-12-24T07:02:40.790+0000 [initandlisten] allocator: tcmalloc
2017-12-24T07:02:40.790+0000 [initandlisten] options: { net: { bindIp: "0.0.0.0", http: { RESTInterfaceEnabled: true, enabled: true } }, storage: { dbPath: "data", journal: { enabled: false } } }
2017-12-24T07:02:40.811+0000 [initandlisten] ERROR: listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2017-12-24T07:02:40.811+0000 [initandlisten] ERROR: addr already in use
2017-12-24T07:02:40.811+0000 [initandlisten] ERROR: listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:28017
2017-12-24T07:02:40.811+0000 [initandlisten] ERROR: addr already in use
2017-12-24T07:02:40.814+0000 [initandlisten] now exiting
2017-12-24T07:02:40.814+0000 [initandlisten] dbexit:
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: going to close listening sockets...
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: going to flush diaglog...
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: going to close sockets...
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: waiting for fs preallocator...
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: closing all files...
2017-12-24T07:02:40.814+0000 [initandlisten] closeAllFiles() finished
2017-12-24T07:02:40.814+0000 [initandlisten] shutdown: removing fs lock...
2017-12-24T07:02:40.814+0000 [initandlisten] dbexit: really exiting now
Some people said to delete *mongod.lock that's created inside the data file and then use --repair an then run it but it still doesn't work.
You're starting an instance of mongod while one is already running in the background. You could use ps -A to look for the mongod process, and subsequently use kill <process id> to kill that instance.
If the port is blocked by any other process, you can find that out with: lsof -i :27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 30284 ubuntu 5u IPv4 447749095 0t0 TCP *:27017 (LISTEN)

Fatal Assertion in MongoDB

I have been uninstalling and reinstalling Mongodb on Ubuntu 14.04 following the official documentation for few times now. However, I keep getting this:
2015-12-22T12:46:32.091+0000 I CONTROL ***** SERVER RESTARTED *****
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] MongoDB starting : pid=7039 port=27017 dbpath=/var/lib/mongodb 64-bit host=server-04
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] db version v3.0.8
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] git version: 83d8cc25e00e42856924d84e220fbe4a839e605d
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] build info: Linux ip-10-187-89-126 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] allocator: tcmalloc
2015-12-22T12:46:32.096+0000 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, syste$
2015-12-22T12:46:32.120+0000 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted
2015-12-22T12:46:32.120+0000 I - [initandlisten] Fatal Assertion 28578
2015-12-22T12:46:32.120+0000 I - [initandlisten]
***aborting after fassert() failure
I am expecting to see: [initandlisten] waiting for connections on port <port> but can't see it. What can cause it?
remove everything from /tmp directory
sudo rm -rf /tmp/mongod*.sock
change the ownership of the db path and log path
chown -R mongodb:mongodb /var/lib/mongodb/
chown -R mongodb:mongodb /var/log/monogdb/
restart your mongo
sudo service mongod start

How to create a Mongo Docker Image with default collections and data?

I need support here to build my own mongo docker image.
I have a list of scripts to create and insert data into the MongoDB that shall be called in my Dockerfile to deliver a docker image with default collections and data.
Here is how my Dockerfile looks like currently:
FROM mongo:latest
RUN mkdir -p /data/scripts
COPY . /data/scripts
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db/
RUN FILES=scripts/*-create.js
RUN for f in $FILES; do mongo mydb $f; done
RUN FILES=scripts/*-insert.js
RUN for f in $FILES; do mongo mydb $f; done
RUN mongod --shutdown
I've tried different options to start and stop mongod and always one of the two fail, the current script raise the following error:
There doesn't seem to be a server running with dbpath: /data/db
Update
After #Matt answer I could run successfully the command chain, but can't still see my database (called my-db), collections and data there.
The current Dockerfile:
FROM mongo:latest
RUN mkdir -p /data/db/scripts
COPY . /data/db
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db \
&& CREATE_FILES=/data/db/scripts/*-create.js \
&& for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
&& INSERT_FILES=/data/db/scripts/*-insert.js \
&& for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
&& mongod --shutdown
The output from the docker build command:
Sending build context to Docker daemon 10.24 kB
Step 1 : FROM mongo:latest
---> c08c92f4cb13
Step 2 : RUN mkdir -p /data/db/scripts
---> Running in a7088943bb57
---> 373c7319927d
Removing intermediate container a7088943bb57
Step 3 : COPY . /data/db
---> 8fa84884edb7
Removing intermediate container ae43e2c24fee
Step 4 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db && CREATE_FILES=/data/db/scripts/*-create.js && for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done && INSERT_FILES=/data/db/scripts/*-insert.js && for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done && mongod --shutdown
---> Running in 33970b6865ee
about to fork child process, waiting until server is ready for connections.
forked process: 10
child process started successfully, parent exiting
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
killing process with pid: 10
---> 8451e43b7749
Removing intermediate container 33970b6865ee
Successfully built 8451e43b7749
But as I said, I still can't see the database, collections and data in my database using mongo shell.
Also I connected to the running container and got the mongodb.log:
2015-11-06T16:15:14.562+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
2015-11-06T16:15:14.562+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-11-06T16:15:14.698+0000 I JOURNAL [initandlisten] preallocateIsFaster=true 2.36
2015-11-06T16:15:14.746+0000 I JOURNAL [durability] Durability thread started
2015-11-06T16:15:14.746+0000 I JOURNAL [journal writer] Journal writer thread started
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] MongoDB starting : pid=10 port=27017 dbpath=/data/db 64-bit host=9c05d483673a
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] db version v3.0.7
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] git version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] build info: Linux ip-10-183-78-195 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] allocator: tcmalloc
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] options: { processManagement: { fork: true }, storage: { dbPath: "/data/db" }, systemLog: { destination: "file", path: "/var/log/mongodb.log" } }
2015-11-06T16:15:14.748+0000 I INDEX [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE [FileAllocator] creating directory /data/db/_tmp
2015-11-06T16:15:14.804+0000 I STORAGE [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0 secs
2015-11-06T16:15:14.807+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2015-11-06T16:15:14.830+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:49641 #1 (1 connection now open)
2015-11-06T16:15:14.832+0000 I INDEX [conn1] allocating new ns file /data/db/my-db.ns, filling with zeroes...
2015-11-06T16:15:14.897+0000 I STORAGE [FileAllocator] allocating new datafile /data/db/my-db.0, filling with zeroes...
2015-11-06T16:15:14.898+0000 I STORAGE [FileAllocator] done allocating datafile /data/db/my-db.0, size: 64MB, took 0 secs
2015-11-06T16:15:14.904+0000 I NETWORK [conn1] end connection 127.0.0.1:49641 (0 connections now open)
2015-11-06T16:15:14.945+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:49642 #2 (1 connection now open)
2015-11-06T16:15:14.958+0000 I NETWORK [conn2] end connection 127.0.0.1:49642 (0 connections now open)
2015-11-06T16:15:14.982+0000 I CONTROL [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2015-11-06T16:15:14.982+0000 I CONTROL [signalProcessingThread] now exiting
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] closing listening socket: 6
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] closing listening socket: 7
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2015-11-06T16:15:14.982+0000 I STORAGE [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-11-06T16:15:14.982+0000 I STORAGE [signalProcessingThread] shutdown: final commit...
2015-11-06T16:15:15.008+0000 I JOURNAL [signalProcessingThread] journalCleanup...
2015-11-06T16:15:15.008+0000 I JOURNAL [signalProcessingThread] removeJournalFiles
2015-11-06T16:15:15.009+0000 I JOURNAL [signalProcessingThread] Terminating durability thread ...
2015-11-06T16:15:15.088+0000 I JOURNAL [journal writer] Journal writer thread stopped
2015-11-06T16:15:15.088+0000 I JOURNAL [durability] Durability thread stopped
2015-11-06T16:15:15.088+0000 I STORAGE [signalProcessingThread] shutdown: closing all files...
2015-11-06T16:15:15.090+0000 I STORAGE [signalProcessingThread] closeAllFiles() finished
2015-11-06T16:15:15.090+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2015-11-06T16:15:15.090+0000 I CONTROL [signalProcessingThread] dbexit: rc: 0
I also checked the folder /data/db content:
root#fbaf17233182:/data/db# ls -al
total 16
drwxr-xr-x 3 mongodb mongodb 4096 Nov 6 16:15 .
drwxr-xr-x 4 root root 4096 Nov 6 16:15 ..
drwxr-xr-x 2 root root 4096 Nov 5 18:55 scripts
May help:
Parent Dockerfile
Parent Dockerfile ENTRYPOINT
The problem was that information could not be saved on /db/data, so I've created a solution creating my own data directory.
# Parent Dockerfile https://github.com/docker-library/mongo/blob/982328582c74dd2f0a9c8c77b84006f291f974c3/3.0/Dockerfile
FROM mongo:latest
# Modify child mongo to use /data/db2 as dbpath (because /data/db wont persist the build)
RUN mkdir -p /data/db2 \
&& echo "dbpath = /data/db2" > /etc/mongodb.conf \
&& chown -R mongodb:mongodb /data/db2
COPY . /data/db2
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2 --smallfiles \
&& CREATE_FILES=/data/db2/scripts/*-create.js \
&& for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
&& INSERT_FILES=/data/db2/scripts/*-insert.js \
&& for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
&& mongod --dbpath /data/db2 --shutdown \
&& chown -R mongodb /data/db2
# Make the new dir a VOLUME to persists it
VOLUME /data/db2
CMD ["mongod", "--config", "/etc/mongodb.conf", "--smallfiles"]
Thanks to #yosifkit from the docker-library/mongo Github project for pointing that the volume would store the data in the resulting image. I missed that on the documentation.
During a docker image build, each build command like RUN is launched in it's own docker container and then when the command completes the data is committed as an image. If you run dockviz images --tree while doing a build you will get the idea.
In your case mongod has started and stopped long before you need it. You need to start mongo and run your scripts all in the one RUN step. You can achieve that by using a shell script that launches mongod and inserts your data.
Your Dockerfile will run:
RUN mongo_create_insert.sh
Then mongo_create_insert.sh contains all your mongo dependent steps:
#!/usr/bin/env bash
mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db/
FILES=scripts/*-create.js
for f in $FILES; do mongo mydb $f; done
FILES=scripts/*-insert.js
for f in $FILES; do mongo mydb $f; done
mongod --shutdown
As a side note, I tend to install Ansible in my base image and use that to provision Docker images in single RUN command rather than doing lots of shell RUN steps in a Dockerfile (which is just a glorified shell script in the end). You lose some of the build caching niceness but we've moved on from provisioning with shell scripts for a reason.
According to the description of the image on DockerHub, there is a much cleaner and simpler solution for this.
When a container is started for the first time it will execute files
with extensions .sh and .js that are found in
/docker-entrypoint-initdb.d. Files will be executed in alphabetical
order. .js files will be executed by mongo using the database
specified by the MONGO_INITDB_DATABASE variable, if it is present, or
test otherwise. You may also switch databases within the .js script.
First, the Dockerfile is as simple as
FROM mongo:4
COPY setup.sh /docker-entrypoint-initdb.d/
COPY scripts /
Then, in the setup.sh, add your user/collection creation script, for example
mongo=( mongo --host 127.0.0.1 --port 27017 --quiet )
mongo+=(
--username="$MONGO_INITDB_ROOT_USERNAME"
--password="$MONGO_INITDB_ROOT_PASSWORD"
--authenticationDatabase="$rootAuthDatabase"
)
CREATE_FILES=/scripts/*-create.js
for f in $CREATE_FILES; do "${mongo[#]}" "$MONGO_INITDB_DATABASE" $f; done
INSERT_FILES=/scripts/*-insert.js
for f in $INSERT_FILES; do "${mongo[#]}" "$MONGO_INITDB_DATABASE" $f; done

MongoDB centos7 unable to run with a non standard port as a service

I installed mongodb on a fresh centos 7
I run sudo service mongod start without any problem
When I change the port in /etc/mongod.conf and I restart the mongod service I get the following error
From /var/log/mongodb/mongod.log
2014-11-11T07:38:29.495+0000 [initandlisten] MongoDB starting : pid=12401 port=27022 dbpath=/var/lib/mongo 64-bit host=awscm001
2014-11-11T07:38:29.495+0000 [initandlisten] db version v2.6.5
2014-11-11T07:38:29.495+0000 [initandlisten] git version: e99d4fcb4279c0279796f237aa92fe3b64560bf6
2014-11-11T07:38:29.495+0000 [initandlisten] build info: Linux build8.nj1.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
2014-11-11T07:38:29.495+0000 [initandlisten] allocator: tcmalloc
2014-11-11T07:38:29.495+0000 [initandlisten] options: { config: "/etc/mongod.conf", net: { port: 27021 }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/var/lib/mongo" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2014-11-11T07:38:29.497+0000 [initandlisten] journal dir=/var/lib/mongo/journal
2014-11-11T07:38:29.497+0000 [initandlisten] recover : no journal files present, no recovery needed
2014-11-11T07:38:29.679+0000 [initandlisten] ERROR: listen(): bind() failed errno:13 Permission denied for socket: 0.0.0.0:27022
2014-11-11T07:38:29.680+0000 [initandlisten] now exiting
2014-11-11T07:38:29.680+0000 [initandlisten] dbexit:
2014-11-11T07:38:29.680+0000 [initandlisten] shutdown: going to close listening sockets...
2014-11-11T07:38:29.680+0000 [initandlisten] shutdown: going to flush diaglog...
I am able though to run with a different port with the following command
sudo mongod -f /etc/mongod.conf
When I run as a service user mongod runs mongodb
When I run from command line root runs mongodb
How can run the mongodb service with a different port?
By the way I succeeded on centos 6.5
Thanks
for those people having problem like me.. my fix is allow mongodb custom port on semanage
semanage port --list | grep mongo
mongod_port_t tcp 27017-27019, 28017-28019
as you can see, only 27017-27019, 28017-28019 is allowed.
in order to add a custom port for mongodb
semanage port -a -t mongod_port_t -p tcp 21010
you now have added the custom port
semanage port --list | grep mongo
mongod_port_t tcp 21010, 27017-27019, 28017-28019
make sure to restart mongodb

changing mongod dbpath raise permission error

I needed more space on my mongo database so I went to my local computer shop, bought a new hard drive, installed it. Then I copied all the files from /var/lib/mongodb to /media/didier/mongodb/mongodb/ and changed the ownership of /media/didier/mongodb/mongodb/ with the command sudo chown -R mongodb:mongodb /media/didier/mongodb/mongodb/. Then, I changed the config file at /etc/mongod.conf to point to the good data directory. Basically, I followed the instruction at Changing MongoDB data store directory.
The permissions looks good, the config file looks fine too, but when I start mongodb using sudo service mongod start I have the following exception in my logs
014-07-16T09:11:37.362-0400 [initandlisten] MongoDB starting : pid=5899 port=27017
dbpath=/media/didier/mongodb/mongodb/ 64-bit host=miaou
2014-07-16T09:11:37.362-0400 [initandlisten] db version v2.6.3
2014-07-16T09:11:37.362-0400 [initandlisten] git version: 255f67a66f9603c59380b2a389e386910bbb52cb
2014-07-16T09:11:37.362-0400 [initandlisten] build info: Linux build12.nj1.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
2014-07-16T09:11:37.362-0400 [initandlisten] allocator: tcmalloc
2014-07-16T09:11:37.362-0400 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/media/didier/mongodb/mongodb/" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2014-07-16T09:11:37.362-0400 [initandlisten] exception in initAndListen std::exception: boost::filesystem::status: Permission denied: "/media/didier/mongodb/mongodb/", terminating
2014-07-16T09:11:37.362-0400 [initandlisten] dbexit:
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: going to close listening sockets...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: going to flush diaglog...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: going to close sockets...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: waiting for fs preallocator...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: lock for final commit...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: final commit...
2014-07-16T09:11:37.362-0400 [initandlisten] shutdown: closing all files...
2014-07-16T09:11:37.362-0400 [initandlisten] closeAllFiles() finished
2014-07-16T09:11:37.362-0400 [initandlisten] dbexit: really exiting now
Any idea what goes wrong? When I simply run sudo mongod --config /etc/mongod.conf everything works fine. So I suppose it is a permission problem.
cheers!
The problem is that the hard drive is not accessible to the mongodb user. To share a hard drive between all users, you need to change the /etc/fstab by adding the following line:
UUID=a43bc179-f0e5-464b-bd77-1be6a7581ec2 /media/mongodb ext4 defaults 0 0
That will mount the hard drive to the /media/mongodb directory at boot time. See https://help.ubuntu.com/community/Fstab for more information.
In short, to use a different hard drive as data location on mongodb on a ubuntu machine one needs to do the following three steps:
make sure the mongodb user can access the new hard drive (fstab)
make sure the user mongodb has access to the data directory (chown)
change the mongo config file (/etc/mongod.conf)
I think the issue is you need to change permissions of your /media mount point to have r+x (read and execute) for 'other' groups and users; at least that fixed it for me. Doing
sudo chmod a+rx /media
will do it. You want to make sure this is also true for the whole path to the mongodb data I think, so you could do sudo chmod -R a+rx /media to make it recursive, or change the permissions for the drive, then each subfolder up to the mongodb folder:
sudo chmod a+rx /media/didier
sudo chmod a+rx /media/didier/mongodb
sudo chmod a+rx /media/didier/mongodb/mongodb