【问题标题】:MongoDB, an Ubuntu user other than root, and storing the PID file in /var/run/mongodbMongoDB,一个非 root 的 Ubuntu 用户,并将 PID 文件存储在 /var/run/mongodb
【发布时间】:2014-10-02 00:58:55
【问题描述】:

我无法将 mongod 的 PID 文件存储在目录 /var/run/mongodb 中,并且无法以 root 以外的用户身份启动 mongod 进程。

我已经在 Ubuntu v14.04 上安装了 MongoDB v2.6.3;创建了一个名为mongodb的linux用户;并使用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

我知道只有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

当我重新启动服务器时,我希望 mongod 能够像用户 mongodb 运行 mongod 命令一样启动。但是,当我检查日志时,我看到了错误:

ERROR: Cannot write pid file to /var/run/mongodb/mongod.pid: No such file or directory

我注意到在/etc/init.d 中有一个名为mongod 的文件似乎覆盖了我在rc.local 中的命令(MongoDB 的默认启动脚本,我似乎找不到任何文档)。感觉很勇敢,我删除了那个文件,重新启动了服务器,得到了和上面一样的错误。

沮丧,我关闭了服务器并上床睡觉。第二天早上,我醒来,启动服务器,一切都按预期工作。 mongod 进程正在运行;目录/var/run/mongodb 存在;一切都很好。

但是,当我重新启动服务器时,我再次收到错误消息。

这里发生了什么? rc.local 不是在重新启动时运行吗?如果是这样,我怎样才能让它这样做?有没有更好的方法来实现我的目标?

【问题讨论】:

    标签: linux mongodb ubuntu pid rc


    【解决方案1】:

    事实证明,在 Ubuntu 中处理启动服务的首选方式是使用位于 /etc/init 中的 upstart configuration files

    查看/etc/init/mongodb,我发现一个部分看起来像这样:

    pre-start script
        mkdir -p /var/lib/mongodb/
        mkdir -p /var/log/mongodb/
    end script
    

    我添加的:

    mkdir -p /var/run/mongodb
    chown -R mongodb:mongodb /var/run/mongodb
    

    这似乎成功了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2014-05-20
      • 1970-01-01
      • 2017-03-15
      • 2018-04-09
      相关资源
      最近更新 更多