Mongo Restart Error -- /var/run/mongodb/mongod.pid exists - mongodb

I just started a new AWS Linux AMI 2016.09.1(HVM) and downloaded the latest Mongodb release. I am using MongoDB version 3.0.14. I updated /etc/mongod.conf to the following,
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on
all interfaces.
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
I run Mongodb for the first time using service,
sudo service mongod start
Everything works fine. When I stop Mongodb and restart,
sudo service mongod stop
sudo service mongod restart
I get the following error,
Error starting mongod. /var/run/mongodb/mongod.pid exists.
Here is what is in the logfile, /var/log/mongodb/mongod.log,
2017-01-16T14:11:16.869+0000 I CONTROL ***** SERVER RESTARTED *****
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] MongoDB starting :
pid=2820 port=27017 dbpath=/var/lib/mongo 64-bit hos
t=ip-10-0-0-91
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] db version v3.0.14
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] git version:
08352afcca24bfc145240a0fac9d28b978ab77f3
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] build info: Linux
ip-10-63-190-181 3.4.43-43.43.amzn1.x86_64 #1 SMP Mon
May 6 18:04:41 UTC 2013 x86_64 BOOST_LIB_VERSION=1_49
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-01-16T14:11:16.872+0000 I CONTROL [initandlisten] options: { config:
"/etc/mongod.conf", net: { port: 27017 }, processMan
agement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" },
storage: { dbPath: "/var/lib/mongo", journal: { enabled: t
rue } }, systemLog: { destination: "file", logAppend: true, path:
"/var/log/mongodb/mongod.log" } }
2017-01-16T14:11:16.893+0000 E NETWORK [initandlisten] Failed to unlink
socket file /tmp/mongodb-27017.sock errno:1 Operation
not permitted
2017-01-16T14:11:16.893+0000 I - [initandlisten] Fatal Assertion
28578
2017-01-16T14:11:16.893+0000 I - [initandlisten]
***aborting after fassert() failure
I have seen several blogs posted about this problem with solutions but none of them seem to work. Spent all day Sunday trying to figure this one out. Never had this problem before running an earlier AWS instance and Mongo.

Ok, found the problem. I deleted the /var/run/mongodb/mongod.pid file as suggested by Dave Maples above. However, MongoDB would still not run. I looked at the log file again and now saw it barking about this file,
/tmp/mongodb-27017.sock
It looked like it did not have the correct permissions. I tried,
sudo chown mongod:mongod /tmp/mongodb-27017.sock
And it worked fine. Now when I start/stop/restart MongoDB multiple times I no longer have the problem. I think the file permissions caused MongoDB to not clean itself up properly and left /var/run/mongodb/mongod.pid file in the directory. This caused the "Error starting mongod. /var/run/mongodb/mongod.pid exists" error. However, this was not the real problem. The real problem was the /tmp/ permissions. Looking at the log file after I deleted the mongod.pid file and restarting Mongod showed the permissions problem. So here is what fixed the problem,
sudo rm /var/run/mongodb/mongod.pid
sudo chown mongod:mongod /tmp/mongodb-27017.sock
I bet if I just tried "sudo chown mongod:mongod /tmp/mongodb-27017.sock" right after installing MongoDB on the new server before running MongoDB would have avoided the problem. Wasted a perfectly good Florida sunny day on this one.

Ok, found a better solution to what I had in my first answer. My previous answer simply eliminated the symptoms. I saw that several other people had to chown various other directories. I believe the problem is that mongod does not restart properly and leaves a number of directories incorrectly set up as it fails. I suspect multiple threads are running so what directory that gets incorrectly set up is somewhat random. I am using Amazon's AWS AMI Linux release and installing MongoDB 3.4. I found a very good MongoDB install script at
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-amazon/
So here is the fix. I tested it several times and it worked each time. Install mongo as you normally would. Use the above script if helpful. Before you start mongod execute the following command,
sudo chkconfig mongod on
You will note that the above install script highlights chkconfig but at the bottom of the script where it might not get seen. This will ensure that you can successfully restart mongod each time. This should work on other linux versions.

you might change the ip in configuration file:
vim /etc/mongod.conf
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on
all interfaces.
bindIp: xxx.xxx.xxx.xxx

You have another option in case of the assignation of user and group dosen't work but this depends directly of the state of sock file.
I moved the mongodb-27017.sock and renamed it, remove the .pid file and start the service and with this the process works
sudo mv /tmp/mongodb-27017.sock /tmp/mongodb-27017.sock2
sudo rm /var/run/mongodb/mongod.pid
sudo service mongod start

Try Rebooting the server , connecting to the server and run the command again. Worked fine for me.

After stopping the server, you are trying to restart. You can only start the server. That's why you are getting the stopping error. And also no need to worry about this error.

Related

mongod service failed to restarted, 27017 port not in use - even getting address already in use error

I am not able to start mongo db service, if I try to start mongod using systemctl restart mongod getting error
Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
Logs: vi /var/log/mongo/mongod.log
In log file, if I see below message, it looks like port is already in use
exception in initAndListen std::exception: listen: Address already in use, terminating
netstat -apt| grep "27017"
No process id.
Below is mongod status:
I am not getting any solution, your help will be greatly appreciated. Thanks.
Trying starting mongodb on different port and with different data directory, that might be helpful, use below command to start with different port and different data directory
mongod --port 27027 --dbpath </path/to/your/data/directory>
If this work then some process might be using 27017 or or you might have mongod running on background, so first check the process id and manually kill it as below
ps -Aef | grep 27017
or
ps -Aef | grep mongod
Find the process id and kill it
sudo kill -9 <processId>
Then try to restart it as you are doing:
systemctl restart mongod
or:
/etc/init.d/mongodb restart
Hope this help you out.
One issue was the lock file used by MongoDB might have an incorrect ownership:
-rw-r--r-- 1 root root 0 Mar 16 02:27 /var/lib/mongo/mongod.lock
Suggest you to simply removed it which will allow MongoDB to recreate it properly. This file is only needed by MongoDB if it's currently running, and ideally the file should only exist if it's running too, since it only contains a copy of the current process ID which MongoDB is running under.
The second issue will be the configuration file itself: /etc/mongod.conf.
MongoDB expects the file to be in YAML format where apparently blank spaces matter. I have share the sample configuration file which worked in our server.
Basically, each indentation should be made using two spaces for any directives underneath their parent directive.
Additinal notes:
For example, the "net:" heading is not indented because it's the top of its configuration tier, as where "port:" and "bindIp:" are each indented with 2 spaces since "net:" is their parent directive.
The "ssl:" directive is also indented with two spaces since its parent is "net:", but since "mode:", "CAFile:" and "PEMKeyFile:" along belong to "ssl:" they go underneath it and again each take 2 additional spaces for further indentation for a total of 4 spaces.
Finally the following mongo restart will work:
systemctl restart mongod
or
systemctl restart mongod.service
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
ssl:
mode: preferSSL
CAFile: /etc/ssl/mongodb/RootCA/root-ca.pem
PEMKeyFile: /etc/ssl/mongodb/host.omkieitsolutions.com.pem
security:
authorization: enabled
clusterAuthMode: keyFile
#operationProfiling:
#replication:
"/etc/mongod.conf" 50L, 990C

installing and running mongodb in codeanywhere

i used this code:
$ mkdir data
$ echo 'mongod --bind_ip=$0.0.0.0 --dbpath=data --nojournal "$#"' > mongod
$ chmod a+x mongod
i have made a file called mongodb that contains this:
​ mongod --bind_ip=$0.0.0.0 --dbpath=data --nojournal "$#"
but when i run
./mongod
i get this and it doesn't work:
https://i.stack.imgur.com/L9224.png
Assuming you are using one of the recent versions of Mongo.
Try this:
Step1- Make a directory, say database, its path being /home/cabox/database
Step2- In the same level as the directory database, create a file named mongod.log, by using touch /home/cabox/mongod.log
Step3- Create a configuration file for our database, mongod.conf using
touch /home/cabox/mongod.conf
Step4- Now open this file using vi and put following lines in it
systemLog:
destination: file
path: "/home/cabox/mongod.log"
logAppend: true
processManagement:
fork: true
storage:
dbPath: "/home/cabox/database"
journal:
enabled: true
engine: "mmapv1"
mmapv1:
smallFiles: true
net:
bindIp: 0.0.0.0
Step5- Save this file and start mongod by running:
mongod --config /home/cabox/mongod.conf
Step6- Start the shell: mongo
Hopefully everything should be working fine by now. The problem with your method was that mongod was using wired Tiger storage engine by default, which causes space related problems in containers like codeanywhere which have a small disk size.
P.S - In the conf file, make sure you indent using spaces and not tabs.
You can shutdown the server by using:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
To check whether it has been successfully started or shutdown:
netstat -ntulp|grep 27017

MongoDB 3.2.9 fails to start as a service on CentOS

I am trying to start mongod as a service on CentOS:
mongod --version
db version v3.2.9
git version: 22ec9e93b40c85fc7cae7d56e7d6a02fd811088c
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
CentOS Linux release 7.2.1511 (Core)
/etc/selinux/config:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
mongod.conf:
systemLog:
destination: file
path: "/mnt/log/mongod.log"
logAppend: true
storage:
dbPath: "/mnt/data"
engine: wiredTiger
journal:
enabled: true
processManagement:
fork: true
pidFilePath: "/var/run/mongodb/mongod.pid"
net:
# bindIp: 127.0.0.1
port: 27017
replication:
replSetName: XXXX
security:
keyFile: "/usr/mongodb/mongodb-keyfile"
The issue states:
Starting mongod (via systemctl): Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
[FAILED]
journalctl -xe
...
Sep 20 13:10:55 ip-10-0-231-19.localdomain systemd[1]: Unit mongod.service entered failed state.
Sep 20 13:10:55 ip-10-0-231-19.localdomain systemd[1]: mongod.service failed.
Sep 20 13:10:55 ip-10-0-231-19.localdomain polkitd[9717]: Unregistered Authentication Agent for unix-process:26567:1630816 (system bus name :1.27, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale en_US.UTF-8) (disconnected from bus)
Sep 20 13:11:04 ip-10-0-231-19.localdomain sudo[26587]: centos : TTY=pts/1 ; PWD=/home/centos ; USER=root ; COMMAND=/bin/vi /etc/mongod.conf
The server starts using:
sudo mongod -f /etc/mongod.conf
I know that there was a bug opened regarding similar issue but the fix might be available in 3.2.7 already and besides it was related to the scenario when SELINUX=enforcing...
Please support.
Recently encountered a similar problem, but on FC26; here, the problem could be traced back to SELINUX and specifically the permissions on the custom dbPath directory.
After trying many things, I found that the following sequence helped resolve my problem:
MONGODB_DATADIR=/mnt/data
sudo chown -R mongodb:mongodb $MONGODB_DATADIR
sudo chmod -R 700 $MONGODB_DATADIR
SELinux settings:
sudo semanage port -a -t mongod_port_t -p tcp 27017
sudo semanage fcontext -a -t mongod_var_lib_t ${MONGODB_DATADIR}
sudo semanage fcontext -a -t mongod_var_lib_t ${MONGODB_DATADIR}/mongod.lock
sudo restorecon -v ${MONGODB_DATADIR}
You probably also have to add mongod_log_t to the custom log file location, /mnt/log/mongod.log, I did not modify that one on my end.
Actually, for me the fix was:
sudo chown -R mongod:mongod $MONGODB_DATADIR

unable to launch config server for mongo shard

I am trying to get the config server running for mongo shard using the following command and config file:
mongod --config /etc/mongod2.conf
mongod2.config
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb2
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
processManagement:
pidFilePath: /srv/mongodb/db2.pid
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27019
bindIp: 127.0.0.1
processManagement:
fork: true
#security:
# authorization: enabled
replication:
replSetName: configReplSet
sharding:
clusterRole: configsvr
When I run the command above, I get the following output (with no log output in the mongo logs):
about to fork child process, waiting until server is ready for connections.
forked process: 14246
ERROR: child process failed, exited with error number 1
I am running mongo v3.2.2 on ubuntu v14. The port 27019 is not bound by another process.
The issue was caused since I never created a separate data directory. I saw better error messages when I ran the following:
mongod --dbpath /var/lib/mongodb2 --port 27019
This command gave better error messages (it showed that the data directory was not present etc.). This helped solve the issue and I was able to run the config server (output below):
2016-06-29T12:21:30.216-0400 I CONTROL [initandlisten] MongoDB starting : pid=16060 port=27019 dbpath=/var/lib/mongodb2 64-bit host=abcdefghi
2016-06-29T12:21:30.216-0400 I CONTROL [initandlisten] db version v3.2.0

Mongodb restore problems

Mongodb, running on an Ubuntu 12.04 box, decided to dump core because it opened too many files (applause). After running mongod --repair as root as http://docs.mongodb.org/manual/tutorial/recover-data-following-unexpected-shutdown/ alluded to (but didn't specify), I now find that mongo has apparently whacked out its own file permissions somehow - every time I try to start I get
Mon Apr 1 15:10:08 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/etc/mongodb.conf", dbpath: "/var/lib/mongodb", logappend: "true", logpath: "/var/log/mongodb/mongodb.log", nojournal: "true" }
Mon Apr 1 15:10:09 [initandlisten] couldn't open /var/lib/mongod/cerebro_test_import_ayp_part2.ns errno:1 Operation not permitted
Mon Apr 1 15:10:09 [initandlisten] error couldn't open file /var/lib/mongodb/cerebro_test_import_ayp_part2.ns terminating
"Operation not permitted"? Really? Even after
$ sudo chmod -R 777 /var/lib/mongodb
? I'm a couple of millimeters away from just blowing this whole database away and chalking it up as yet another reason Mongo sucks. What can I do to get mongo happy again so I don't have to do that? What haven't I tried? What gives?
Did you try
sudo chown -Rh mongodb:mongodb /var/lib/mongod*?
This has all the hallmarks of a Linux permissions problem, chmod'ding 777 is always a bad idea.