简体   繁体   English

MongoDB,不是root用户的Ubuntu用户,并将PID文件存储在/ var / run / mongodb中

[英]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.

相关问题 以用户身份存储守护程序的pid文件 - Storing pid file for a daemon run as user 以另一个用户(root用户以外的其他用户)身份运行整个应用程序 - Run whole application as another user (other user than root) 以非root用户身份运行系统守护程序PID文件的首选位置 - Preferred location for PID file of system daemon run as non-root user Hhvm重新启动后无法读取任何有意义的pid的pid文件/ var / run / hhvm / pid - Hhvm Unable to read pid file /var/run/hhvm/pid for any meaningful pid after reboot Ubuntu以非root用户身份运行脚本 - Ubuntu run script as non-root user 来自pid文件/var/run/mysqld/mysqld.pid的mysqld_safe mysqld防止服务器重启 - mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid prevent from server restart 普通用户在 /var/run 中触摸文件失败 - Normal user touching a file in /var/run failed 如何在Ubuntu中运行mongodb(从tar.gz中提取)作为服务? - How to run mongodb (extracted from tar.gz) as service in Ubuntu? 为什么安装时MongoDB无法在Ubuntu 11.10上创建用户? - Why is MongoDB unable to create a user on Ubuntu 11.10 when installing? 错误/ usr / libexec / mysqld:写入文件'/var/run/mysqld/mysqld.pid时出错(错误代码:28) - ERROR /usr/libexec/mysqld: Error writing file '/var/run/mysqld/mysqld.pid' (Errcode: 28)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM