Mongo 服务启动踩坑记
发布于 7 年前 作者 yvanwangl 20261 次浏览 来自 分享

在 Centos 系统中安装了mongodb,并且以后台服务的形式运行mongo,sudo service mongod start。安装完成,启动成功,一切都很顺利,但是在一次停止mongo 服务后再次启动时却提示一下错误信息:

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]

命令行输入—systemctl status mongod.service 查看错误日志如下:

 mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2017-11-28 21:17:17 CST; 1min 19s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 14690 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)

Nov 28 21:17:16 iZuf62kvdczytdgvp41b7nZ systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Nov 28 21:17:16 iZuf62kvdczytdgvp41b7nZ runuser[14701]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ systemd[1]: mongod.service: control process exited, code=exited status=1
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ mongod[14690]: Starting mongod: [FAILED]
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ systemd[1]: Unit mongod.service entered failed state.
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ systemd[1]: mongod.service failed.

从日志中并看不出什么有效的信息,之后求助 Google ,也没有搜寻到有效的解决方案,各种查找之后发现了一些答友说是文件夹权限的问题,再看日志发现有两行信息:

Nov 28 21:17:16 iZuf62kvdczytdgvp41b7nZ runuser[14701]: pam_unix(runuser:session): session opened for user mongod by (uid=0)
Nov 28 21:17:17 iZuf62kvdczytdgvp41b7nZ systemd[1]: mongod.service: control process exited, code=exited status=1

看起来确实跟用户 (mongod)权限有关,mongo 需要执行的文件有两个,一个是数据存储文件 /var/lib/mongo,一个是日志存储文件 /var/log/mongodb。所以执行以下命令对这两个文件夹的用户权限进行修改:

chown -R mongod:mongod /var/lib/mongo
chown -R mongod:mongod /var/log/mongodb

之后再次执行 sudo service mongod start,很令人失望,依然报错,再次执行 systemctl status mongod.service 查看错误日志如下:

● mongod.service - SYSV: Mongo is a scalable, document-oriented database.
   Loaded: loaded (/etc/rc.d/init.d/mongod; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2017-11-28 21:33:30 CST; 34s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 16330 ExecStart=/etc/rc.d/init.d/mongod start (code=exited, status=1/FAILURE)

Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database....
Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ systemd[1]: mongod.service: control process exited, code=exited status=1
Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ mongod[16330]: Error starting mongod. /var/run/mongodb/mongod.pid exists.
Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database..
Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ systemd[1]: Unit mongod.service entered failed state.
Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ systemd[1]: mongod.service failed.

但是令人欣慰的是这次的错误信息不一样了,说明刚才修改权限的操作是有效的,小开心一下 :) ,继续看日志,发现有一行信息:

Nov 28 21:33:30 iZuf62kvdczytdgvp41b7nZ mongod[16330]: Error starting mongod. /var/run/mongodb/mongod.pid exists.

从字面意思看就是 /var/run/mongodb/mongod.pid 这个文件存在导致进程退出,查看 mongo 服务的执行脚本 /etc/rc.d/init.d/mongod 中有一段代码:

start()
{
...
  # Make sure the pidfile does not exist
  if [ -f "$PIDFILEPATH" ]; then
      echo "Error starting mongod. $PIDFILEPATH exists."
      RETVAL=1
      return
  fi

}

上面的错误日志正是出于此处,而且注释中说了这段代码的判断逻辑是确保 pidfile 不存在,这就进一步印证了错误是由于 pidfile 的存在导致的,果断删除 /var/run/mongodb/mongod.pid

rm /var/run/mongodb/mongod.pid -f

再次重启服务:sudo service mongod start,成功了 ~~

Starting mongod (via systemctl):                           [  OK  ]

最后整理了一个 shell 脚本 mongoAuth.sh

#!/bin/bash
chown -R mongod:mongod /var/lib/mongo
chown -R mongod:mongod /var/log/mongodb
rm /var/run/mongodb/mongod.pid -f

之后再遇到 mongo 服务启动失败的问题,执行此脚本即可解决,我用了之后很好使,所以分享此文章,如果有小伙伴以后遇到这样的问题了,希望该篇文章可以帮到你。共同学习,共同进步!

文章来源于我的个人博客:Mongo 服务启动踩坑记

10 回复

建议格式为 rm /var/run/mongodb/mongod.pid -f

@waitingsong 请问为什么这种写法更好呢?能讲解下吗?

@ImSiegeLion 可以适当的减少误删

@dengnan123 老铁是遇到这个问题了么

@waitingsong 嗯,学习了 :)

@ImSiegeLion 如果你期望 rm -rf /data/foo , 然后刚输入到 rm -rf / 的时候手一抖按下了回车。。。 扩展开来,在数据库控制台执行 DELETE FROM … WHERE xx 命令,应该先输入 DELETE FROM WHERE foo=1 待条件写完毕之后再返回填写表名。 更严谨的做法是不管UPDATE,DELETE,先用同样条件执行一次 SELECT看结果是否符合预期。 当然规范的做法是禁止在生产库上直接操作

@waitingsong 嗯,说的很对,学习到了 :)

@waitingsong 哦哦,哈哈,学到了~ 👍🏼 谢谢!

回到顶部