[英]MongoDB, an Ubuntu user other than root, and storing the PID file in /var/run/mongodb
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
. 我无法将mongod的PID文件存储在目录
/var/run/mongodb
并且无法以root
以外的用户身份启动mongod进程。
I have installed MongoDB v2.6.3 on Ubuntu v14.04; 我已经在Ubuntu v14.04上安装了MongoDB v2.6.3; created a linux user called
mongodb
; 创建了一个名为
mongodb
的linux用户; and granted ownership of the directories in the following configuration to that user, using chown -R mongodb:mongodb {path}
: 并使用
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: 我知道只有
root
用户才能写入/var/run
目录,因此我将以下代码添加到/etc/rc.local
中,以创建mongodb
用户拥有的子目录:
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. 重新引导服务器时,我希望mongod能够像用户
mongodb
运行过mongod命令一样启动。 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). 我注意到
/etc/init.d
有一个名为mongod
的文件,该文件似乎覆盖了rc.local
命令(MongoDB的默认启动脚本,我似乎找不到任何文档)。 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;
mongod进程正在运行; the directory
/var/run/mongodb
existed; 目录
/var/run/mongodb
存在; 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? rc.local
不在重新启动时运行吗? If so, how can I get it to do so? 如果是这样,我该怎么做呢? Is there a better way of achieving my goal?
有没有更好的方法可以实现我的目标?
Turns out the preferred way to handle startup services in Ubuntu is by using upstart configuration files , which are located in /etc/init
. 事实证明,在Ubuntu中处理启动服务的首选方式是使用upstart配置文件 ,该文件位于
/etc/init
。
Looking at /etc/init/mongodb
, I found a section that looked like this: 查看
/etc/init/mongodb
,我发现一个部分如下所示:
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! 这似乎可以解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.