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

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

Related

mongodb server is not starting, mac os

When running:
mongod
I get:
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] MongoDB starting : pid=1160 port=27017 dbpath=/data/db 64-bit host=Interstellar
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] db version v3.4.4
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2k 26 Jan 2017
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] allocator: system
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] modules: none
2017-04-30T11:19:56.234+0600 I CONTROL [initandlisten] build environment:
2017-04-30T11:19:56.235+0600 I CONTROL [initandlisten] distarch: x86_64
2017-04-30T11:19:56.235+0600 I CONTROL [initandlisten] target_arch: x86_64
2017-04-30T11:19:56.235+0600 I CONTROL [initandlisten] options: {}
2017-04-30T11:19:56.235+0600 I STORAGE [initandlisten] exception in
initAndListen: 29 Data directory /data/db not found., terminating
2017-04-30T11:19:56.235+0600 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-04-30T11:19:56.235+0600 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-04-30T11:19:56.235+0600 I CONTROL [initandlisten] now exiting
2017-04-30T11:19:56.235+0600 I CONTROL [initandlisten] shutting down with code:100
I'm having trouble with this, badly need help
The problem is that if you run "mongod" the default path is /usr/data. NEVERTHELESS macOS Catalina does not accept creating a writable folder there. So after long research, in both here, and reddit, I finally got my server (mongod as well as Mongo) up and running. Try the following these steps:
mkdir -p /System/Volumes/Data/data/db
sudo chown -Rv <your_user_name> /System/Volumes/Data/data/db
sudo mongod --dbpath /System/Volumes/Data/data/db
This may run mongod and start the server. Open another terminal tab, and type Mongo.
First step
brew services start mongodb/brew/mongodb-community#4.4
Second step
cd /Users
whoami
cd to your whoami directory
mkdir data
cd data
mkdir db
mongod --dbpath ~/data/db
Third step
mongo
The problem is that there is no /data/db directory. Please issue this command:
sudo mkdir -p /data/db
If you're not worried about opening up permissions too widely, but just want to ensure that mongodb has permissions to start, you can also issue these commands. They may fix any problems you have if the above command alone doesn't solve things.
cd /
sudo chmod -R 777 data

Windows Docker mongo container doesn't work with volume mount

I have the following docker command
docker run -v //c/data:/data/db mongo
and I get the following error response from docker / mongo
MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8706cbf1b78f
db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1t 3 May 2016
allocator: tcmalloc
modules: none
build environment:
distmod: debian81
distarch: x86_64
target_arch: x86_64
options: {}
wiredtiger_open config: create,cache_size=478M,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
WiredTiger error (1) [1489982988:687653][1:0x7fec9df0ccc0], connection: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
Assertion: 28595:1: Operation not permitted src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 267
exception in initAndListen: 28595 1: Operation not permitted, terminating
shutdown: going to close listening sockets...
removing socket file: /tmp/mongodb-27017.sock
shutdown: going to flush diaglog...
now exiting
shutting down with code:100
now when I remove the volume mongo works but I need to persist my data so I need to mount the volume somehow, just not sure what i am doing wrong at this point.
I do see the files appearing in my folder but not sure why I get the 100 error
To get around this, you can employ a tool like rsync to move the db files into mapped directory while Mongo is running. The underlying bug has to do with latency between the Windows mapped volume and that bind path within the container. Offloading the work to rsync decouples the latency from Mongo's runtime requirements.
Example
Create a basic Dockerfile like this:
FROM mongo:latest
RUN apt-get update && \
apt-get install -y \
rsync
ADD init.sh /init.sh
Where init.sh is:
#!/bin/bash
migrate_db() {
while true
do
rsync -avh /data/db/* /data/mapped-db
sleep 5
done
}
migrate_db &
#Execute a command
mongod --smallfiles --logpath=/dev/null --verbose &
#Wait
wait $!
Then, when launching the container, just start with ./init.sh as your ENTRYPOINT.

how to set mogodb in upstart service with authentication

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/

Error while connecting mongodb shell [duplicate]

when i setup mongodb in my ubuntu , i try : ./mongo it show this error :
couldn't connect to server 127.0.0.1 shell/mongo.js
so what can i do ,
thanks
Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Please note the following:
You must run this command as the mongodb user. If you run it as root,
then root will own files in /var/lib/mongodb/ that are necessary to
run the mongodb daemon and therefore when the daemon trys to run
later as the mongodb user, it won't have permissions to start. In
that case you'll get this error: Unable to create / open lock file
for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission
denied, terminating.
On Ubuntu, you must specify the configuration file /etc/mongodb.conf
using the -f flag. Otherwise it will look for the data files in the
wrong place and you will see the following error: dbpath (/data/db/)
does not exist, terminating.
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
Here is all, sometimes, it takes a little while to start mongo after performing these operations.
Trying running $mongod
If you get en error such as
MongoDB shell version: 2.0.5
connecting to: test
Fri Jun 1 11:20:33 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
hisham-agil:~ hisham$ mongod
mongod --help for help and startup options
Fri Jun 1 11:24:47 [initandlisten] MongoDB starting : pid=53452 port=27017 dbpath=/data/db/ 64-bit host=hisham-agil.local
Fri Jun 1 11:24:47 [initandlisten] db version v2.0.5, pdfile version 4.5
Fri Jun 1 11:24:47 [initandlisten] git version: nogitversion
Fri Jun 1 11:24:47 [initandlisten] build info: Darwin gamma.local 11.3.0 Darwin Kernel Version 11.3.0: Thu Jan 12 18:48:32 PST 2012; root:xnu-1699.24.23~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
Fri Jun 1 11:24:47 [initandlisten] options: {}
Fri Jun 1 11:24:47 [initandlisten] exception in initAndListen: 10296 dbpath (/data/db/) does not exist, terminating
Fri Jun 1 11:24:47 dbexit:
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close listening sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to flush diaglog...
Fri Jun 1 11:24:47 [initandlisten] shutdown: going to close sockets...
Fri Jun 1 11:24:47 [initandlisten] shutdown: waiting for fs preallocator...
Fri Jun 1 11:24:47 [initandlisten] shutdown: lock for final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: final commit...
Fri Jun 1 11:24:47 [initandlisten] shutdown: closing all files...
Fri Jun 1 11:24:47 [initandlisten] closeAllFiles() finished
Fri Jun 1 11:24:47 dbexit: really exiting now
Then you've run into a basic startup error that is pretty common.
By default mongod will try to use /data/db for its database files, which in this case, does not exist.
You can't start
mongo
until you handle
mongod.
Try creating those directories and make sure they are writable by the same user that is running the mongod process.
**See similar question-- Getting an error, "Error: couldn't connect to server 127.0.0.1 shell/mongo.js" & when trying to run mongodb on mac osx lion
This is actually not an error... What happens here is that Mongo relies on a daemon in order to run the local database server, so in order to "fire up" the mongo server in your shell, you have to start the mongo service first.
For Fedora Linux (wich is the Distro I use) You have to run these commands:
1 sudo service mongod start
2 mongo
And there you have it! the server is going to run. Now, If you want Mongo service
to Start when the system boots then you have to run:
sudo chkconfig --levels 235 mongod on
And that's all! If you do that, now in the shell you just have to type mongo in order
to start the server but that's pretty much it, the problem is you have to start the SERVICE first and then the SERVER :)
P.S. The commands I posted might work on other linux distros as well, not just in fedora... In case not maybe you have to tweak some words depending on the distro you're using ;)
I got the same problem when I tried to install mongo. I got Error as,
Error
"Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84"
Solution:
First install mongod by using:
sudo apt-get install mongodb-server
Then type
mongod --dbpath /mongo/db
Then
sudo rm /var/lib/mongodb/mongod.lock
Then
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
Thank You
You need to delete the lockfile mongod.lock or /var/lib/mongodb/mongod.lock on ubuntu, then you need to run mongod.exe or service mongodb start on ubuntu first, then run mongo.exe or mongo on ubuntu.
Either your mongod is not running (check using "ps" command) or it is listening on some outside IP address and not on localhost. So first check the process list if 'mongod' is running. If yes, check with "netstat -nap" for the related port.
Of course you can start mongod on the console manually or even look into the mongod logfile
(if there is one configured...depending on how you installed mongod).
First you have to make sure that all the files and directories in your /var/lib/mongodb/ folder (or whichever folder dbpath points to) belong to the mongodb user and mongodb group.
cd /var/lib/mongodb/
sudo chown mongodb filename.*
sudo chgrp mongodb filename.*
sudo chown -R mongodb directory
sudo chgrp -R mongodb directory
(Replace filename and directory with their respective names)
Then you can remove the lock, repair the database and restart the daemon as other people already mentioned:
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start
First start your mongo server by
Users-MacBook-Pro:csv1 Admin$ mongod
all output going to: /usr/local/var/log/mongodb/mongo.log
Then open another terminal window and open shell
Users-MacBook-Pro:csv1 Admin$ mongo
Also check that your root partition has enough space to start mongod.
df -h /
You'll see smth like this on mongod launch:
Mon Aug 12 17:02:59.159 [initandlisten] recover : no journal files present, no recovery needed
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] ERROR: Insufficient free space for journal files
Mon Aug 12 17:02:59.159 [initandlisten] Please make at least 3379MB available in /var/lib/mongodb/journal or use --smallfiles
Mon Aug 12 17:02:59.159 [initandlisten]
Mon Aug 12 17:02:59.159 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Aug 12 17:02:59.159 dbexit:
Mon Aug 12 17:02:59.159 [initandlisten] shutdown: going to close listening sockets...
On Ubuntu, try this:
sudo invoke-rc.d mongodb start
It could be combination of $PATH and Permission issue.
Try following steps given below:
Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
/usr/local/Cellar/mongodb/2.4.6/
In order to update your $PATH variable, do following:
$ sudo vi /etc/paths
Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.
/usr/local/Cellar/mongodb/2.4.6/bin
Use ‘Esc : w q’ to save and exit from Vi editor.
Use echo to display your path variable:
$ echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
Now try to check the Mongo version, if you get following, then you are on the right track!
$ mongo --version
MongoDB shell version: 2.4.6
Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
$ sudo mkdir /data/db
$ sudo mkdir /data/log
$ whoami
username
$ chown -R username /data
Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.
$ sudo vi /etc/mongodb.conf
Add following:
fork = true
port = 27017
quiet = true
dbpath = /data/db
logpath = /data/log/mongod.log
logappend = true
journal = true
Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.
Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
$ sudo mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 3516
all output going to: /data/log/mongod.log
child process started successfully, parent exiting
If you get above message, then know that you have successfully started your Mongod service.
Now, to connect to it, in Terminal 2 type following:
$mongo test
MongoDB shell version: 2.4.6
connecting to: test
Server has startup warnings:
Tue Sep 3 16:55:43.527 [initandlisten]
Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!
That's all. I applied this solution, when I tried to install copy of MongoDB on my Mac for the first time. See if this help you too.
For detailed post you can go here - http://arcanebytes.com/2013/09/03/mongodb-installation-on-mac-os-x/#comment-1036112094.
I hope it helps!
Cheers,
Chinmay
I solved this problem on ubuntu 12.04 by following steps:
1) sudo rm /var/log/mongodb
2) sudo rm /var/lib/mongodb
3) I removed mongo and then installed it again
4) sudo service mongodb restart
and All is Well
For Ubuntu:
Just Open the terminal and enter the below command.
You just have to restart your mongoDB.
sudo service mongodb restart

Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied

How to I get mongo to use a mounted drive on ec2? I really do not understand. I attached a volume on ec2 formatted the drive as root and start as root and yet as root I cant access? I am running on ubuntu 12.04. No other mongo is running
I see that mongo made a 'db' dir in /data i.e. /data/db
cd /
ls -al
drwxr-xr-x 4 root root 4096 Mar 5 16:28 data
cd /data
ls -al
total 28
drwxr-xr-x 4 root root 4096 Mar 5 16:28 .
drwxr-xr-x 24 root root 4096 Mar 5 16:28 ..
drwxr-xr-x 2 root root 4096 Mar 5 16:28 db
drwx------ 2 root root 16384 Mar 5 16:20 lost+found
sudo mkfs.ext3 /dev/xvdh
sudo mkdir /data
sudo su - -c 'echo "/dev/xvdh %s auto noatime 0 0" | sudo tee -a /etc/fstab'
sudo mount /data
sudo service mongodb start
mongodb start/running, process 17169
sudo ps -ef | grep mongod
ubuntu 15763 15634 0 16:32 pts/2 00:00:00 tail -f mongodb.log
ubuntu 18049 15766 0 16:43 pts/3 00:00:00 grep --color=auto mongod
Tue Mar 5 16:33:15 [initandlisten] MongoDB starting : pid=15890 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar 5 16:33:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar 5 16:33:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar 5 16:33:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar 5 16:33:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar 5 16:33:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar 5 16:33:15 dbexit:
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar 5 16:33:15 [initandlisten] shutdown: going to close sockets...
Tue Mar 5 16:33:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar 5 16:33:15 [initandlisten] shutdown: lock for final commit...
Tue Mar 5 16:33:15 [initandlisten] shutdown: final commit...
Tue Mar 5 16:33:15 [initandlisten] shutdown: closing all files...
Tue Mar 5 16:33:15 [initandlisten] closeAllFiles() finished
Tue Mar 5 16:33:15 [initandlisten] shutdown: removing fs lock...
Tue Mar 5 16:33:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar 5 16:33:15 dbexit: really exiting now
Below is if I restart when I remove a lock file....
Tue Mar 5 16:59:15 [initandlisten] MongoDB starting : pid=21091 port=27017 dbpath=/data 64-bit host=aws-mongo-server-east-staging-20130305161917
Tue Mar 5 16:59:15 [initandlisten] db version v2.2.3, pdfile version 4.5
Tue Mar 5 16:59:15 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Tue Mar 5 16:59:15 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Tue Mar 5 16:59:15 [initandlisten] options: { bind_ip: "10.157.60.27", config: "/etc/mongodb.conf", dbpath: "/data", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", replSet: "heythat" }
Tue Mar 5 16:59:15 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
Tue Mar 5 16:59:15 dbexit:
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to close listening sockets...
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to flush diaglog...
Tue Mar 5 16:59:15 [initandlisten] shutdown: going to close sockets...
Tue Mar 5 16:59:15 [initandlisten] shutdown: waiting for fs preallocator...
Tue Mar 5 16:59:15 [initandlisten] shutdown: lock for final commit...
Tue Mar 5 16:59:15 [initandlisten] shutdown: final commit...
Tue Mar 5 16:59:15 [initandlisten] shutdown: closing all files...
Tue Mar 5 16:59:15 [initandlisten] closeAllFiles() finished
Tue Mar 5 16:59:15 [initandlisten] shutdown: removing fs lock...
Tue Mar 5 16:59:15 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
Tue Mar 5 16:59:15 dbexit: really exiting now
I use this method to solve the problem:
sudo chown -R mongodb:mongodb /data/db
I was having the same problem on a Ubuntu ec2 instance. I was following this amazon article on page 7:
http://d36cz9buwru1tt.cloudfront.net/AWS_NoSQL_MongoDB.pdf
Mongodb path in /etc/mongodb.conf was set to /var/lib/mongodb (primary install location and working). When I changed to /data/db (EBS volume) I was getting 'errno:13 Permission denied'.
First I ran sudo service mongodb stop.
Then I used ls -la to see what group & owner mongodb assigned to /var/lib/mongodb (existing path) and I changed the /data/db (new path) with chown and chgrp to match. (example: sudo chown -R mongodb:mongodb /data/db)
Then I updated the path in etc/mongodb.conf to /data/db and deleted the old mongo files in /var/lib/mongodb directory.
Then I ran sudo service mongodb start and waited about a minute. If you try to connect to 27017 immediately you won't be able to.
After a minute check /data/db (EBS volume) and mongo should have placed a journal, mongod.lock, local.ns, local.0, etc. If not try sudo service mongodb restart and check a minute later.
I just spent over a hour with this. Changing the group and deleting the old files is probably not necessary, but that's what worked for me.
This is a great video about mounting a ebs volume to ec2 instance:
http://www.youtube.com/watch?v=gBII3o3BofU
In my case (AWS EC2 instance, Ubuntu) helped:
$ sudo mkdir -p /data/db/
$ sudo chown `USERNAME` /data/db
And after that everything worked fine.
You just have to give access to your /data/db folder.
Type sudo chown -R <USERNAME> /data/db, replace <USERNAME> by your username.
You can find your username by typing whoami.
I installed mongodb with EBS on an EC2 with Ubuntu 14.04 following this tutorial:
http://docs.mongodb.org/ecosystem/platforms/amazon-ec2/
But instead of the suggested chown I did:
sudo chown -R mongodb:mongodb /data /log /journal
To fix the problem
I had similar issue, the actual reason was that there was mongod session running already from my previous attempt.
I ran
killall mongod
and everything else ran just as expected.
killall command would send a TERM signal to all processes with a real UID. So this kills all the running instances of mongod so that you could start your own.
For mac users:
Run ls -ld /data/db/
Output should be something like drwrx-xr-x 20 singh wheel 680 21 Jul 05:49 /data/db/
Where singh is the owner and wheel is the group it belongs to.
Run sudo chown -R singh:wheel /data/db
Run mongod
As of today, I tried to get my way through the to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating, and tried all the answer posted above to solve this problem, hence nothing worked out by adding
sudo chown -R mongodb:mongodb /data/db
Unless I added my current user permission to the location path by
sudo chown $USER /data/db
Hope this helps someone. Also I just installed Mongo DB on my pi. Cheers!
I had a similar issue and followed all the instructions above regarding changing owners using sudo chown etc. I still had an instance of mongodb running in the background after the changes. Running
ps auxw | grep mongo
showed me other tasks using mongo running in background that weren't closed properly. I then ran kill on all the ones running and then could start my server.
Removing the mongodb.lock file was not the issue in my case. I did so and got an error about the port being in use: [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017. I found another solution here: unable to start mongodb local server with instructions to kill the process:
Find out from netstat which process is running mongodb port (27017)
sudo netstat -tulpn | grep :27017
Output will be: tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1412/mongod
Kill the appropriate process.
sudo kill 1412 (replace 1412 with your process ID found in step 1)
And I was able to successfully start mongodb again. I believe mine was still running from an improper shut down.
For those of you experiencing this error on Windows using Task Manager end the instance of "mongod.exe" that is running. Once that is done permanently delete the mongo.lock file and run mongod.exe. It should work perfectly after that.
My mongo (3.2.9) was installed on Ubuntu, and my log file had the following lines:
2016-09-28T11:32:07.821+0100 E STORAGE [initandlisten] WiredTiger (13) [1475058727:821829][6785:0x7fa9684ecc80], file:WiredTiger.wt, connection: /var/lib/mongodb/WiredTiger.turtle: handle-open: open: Permission denied
2016-09-28T11:32:07.822+0100 I - [initandlisten] Assertion: 28595:13: Permission denied
2016-09-28T11:32:07.822+0100 I STORAGE [initandlisten] exception in initAndListen: 28595 13: Permission denied, terminating
2016-09-28T11:32:07.822+0100 I CONTROL [initandlisten] dbexit: rc: 100
So the problem was in permissions on /var/lib/mongodb folder.
sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo chmod -R 755 /var/lib/mongodb
Restart the server
Fixed it, although I do realise that may be not too secure (it's my own dev box I'm in my case), bit following the change both db and authentication worked.
In Mycase
In mongodb version 2.6.11 default databse directory is /var/lib/mongodb/
$ sudo chown -R id -u /var/lib/mongodb/
$ sudo chown -R id -u /var/lib/mongodb/mongod.lock
$ sudo /etc/init.d/mongod stop
$ sudo /etc/init.d/mongod start
I got the same issue when I ran mongod command after installing it on Windows10.
I stopped the mongodb service and started it again. Working like a charm
Command to stop mongodb service (in windows): net stop mongodb
Command to start mongodb server: mongod --dbpath PATH_TO_DATA_FOLDER
On a Fedora 18 with Mongo 2.2.4 instance I was able to get around a similar error by disabling SELinux by calling setenforce 0 as root.
BTW, this was a corporate environment, not an Amazon EC2 instance, but the symptoms were similar.
In my case the issue was solved by removing the log file.
sudo rm /log/mongod.log
Although the error message refers specifically to the lock file:
exception in initAndListen: 10309 Unable to create/open lock file:
/data/mongod.lock errno:13 Permission denied
Is a mongod instance already running?, terminating
After I killed mongod, I just had the same problem: couldn't start mongod.
$> sudo kill `pidof mongod`
2015-08-03T05:58:41.339+0000 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/mongodbtest/replset/data/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
After I delete the lock directly, I can restart the mongod process.
$> rm -rf /data/mongodbtest/replset/data/mongod.lock
This is what I did to fix the problem:
$sudo mkdir -p /data/db
$export PATH=/usr/local/Cellar/mongodb/3.0.7/bin:$PATH
$sudo chown -R id -u /data/db
and then to start mongo...
$mongod
I had the same problem.
I solved it by changing selinux status to permissive with below command:
setenforce 0
Do ls -lato know the user and group of /var/log/mongodb.
Then do sudo chown -R user:group /data/db
Now run sudo service mongodb start.
Check the status with sudo service mongodb status
On windows be sure the console is started as aministrator
You could try by these ways.
1st.
sudo chown -R mongod:mongod /data/db
but at some times,this is not useful.
2nd.
if the above way is not useful,you can try to do this:
mkdir /data/db #as the database storage path
nohup mongod --dbpath /data/db &
or type:
mongod --dbpath /data/db
to get the output stream
For me on CentOS 6.x:
sudo chown -R mongodb:mongodb <db-path>
sudo service mongod restart
And I have set a custom db-path in /etc/mongod.conf.
If you literally want a one line equivalent to the commands in your original question, you could alias:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
https://stackoverflow.com/a/11777141/7160782
In Centos Server
this works for me
chown -R mongod:mongod /var/lib/mongo
Got a similar error, fixed with removing all records (in my case directory journals, and file mongo.lock...), after that check port with sudo lsof -i:27017, if smth running on it kill <PID of the process>, and try to run ./mongod again
Fix: sudo mongod
I had the same problem, running mongod with sudo privileges fixed it.
Coming from a windows environment, I used just mongod to start the daemon, well it looks like we need the superuser privileges to access /data/db.
You can also give non root users Read and Write permissions to that path. check answers above for a guide!
Every time you when you try to start mongod
just type
sudo mongod
or if permanently want to fix this just try to give rwx premission to /data/db folder
chmod +rwx data/