Mongo 服务启动踩坑记

在 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=1Nov 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=1Nov 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/bashchown -R mongod:mongod /var/lib/mongo

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

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

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

    原文作者:蜗牛_96f2
    原文地址: https://www.jianshu.com/p/b7089687487e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞