Reputation: 3987
I am having trouble storing the PID file for mongod in the directory /var/run/mongodb
and getting the mongod process to start on boot as a user other than root
.
I have installed MongoDB v2.6.3 on Ubuntu v14.04; created a linux user called mongodb
; and granted ownership of the directories in the following configuration to that user, using chown -R mongodb:mongodb {path}
:
systemLog:
path: /var/log/mongodb/mongod.log
logAppend: true
destination: file
processManagement:
pidFilePath: /var/run/mongodb/mongod.pid
fork: true
security:
keyFile: /srv/mongodb/keyfile
clusterAuthMode: keyFile
storage:
dbPath: /data
replication:
replSetName: myReplicaSet
I understand that only the root
user can write to the /var/run
directory, so I added the following code to /etc/rc.local
in order to create a subdirectory that my mongodb
user owns:
PID_FILE_DIRECTORY=/var/run/mongodb
CONFIG_FILE=/etc/mongod.conf
do_start () {
if [ ! -d $PID_FILE_DIRECTORY ]; then
mkdir $PID_FILE_DIRECTORY
chown -R mongodb:mongodb $PID_FILE_DIRECTORY
fi
setuid mongodb
mongod -f $CONFIG_FILE
}
exit 0
When I reboot the server, I expect mongod to start as if the user mongodb
had run the mongod command. However, when I check the logs I see the error:
ERROR: Cannot write pid file to /var/run/mongodb/mongod.pid: No such file or directory
I noticed in /etc/init.d
there was a file called mongod
that appeared to be overriding my commands in rc.local
(a default boot script for MongoDB that I can't seem to find any documentation on). Feeling brave, I deleted that file, rebooted the server, and got the same error as above.
Frustrated, I shutdown the server and went to bed. The next morning, I woke up, booted the server, and everything worked as expected. The mongod process was running; the directory /var/run/mongodb
existed; everything was fine.
However, when I rebooted the server I again got the error message.
What is happening here?? Is rc.local
not being run on reboot? If so, how can I get it to do so? Is there a better way of achieving my goal?
Upvotes: 0
Views: 2405
Reputation: 3987
Turns out the preferred way to handle startup services in Ubuntu is by using upstart configuration files, which are located in /etc/init
.
Looking at /etc/init/mongodb
, I found a section that looked like this:
pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script
To which I added:
mkdir -p /var/run/mongodb
chown -R mongodb:mongodb /var/run/mongodb
And that seemed to do the trick!
Upvotes: 2