最近碰到一个问题,安装了一个mongodb服务,使用的execStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.cnf。有一次直接使用了这个命令启动程序后,发现无法再次通过systemctl启动了,提示:
Job for mongodb.service failed because the control process exited with error code. See "systemctl status mongodb.service" and "journalctl -xe" for details.
执行了journalctl -xe后,发现了在如下问题:
[WT_VERB_DEFAULT][ERROR]: __posix_open_file, 815: /usr/local/mongodb/data//WiredTiger.turtle: handle-open: open: Permission denied"
还有几个类似的错误提示,可以看出是权限不足,于是查看了一下这个文件,发现他的所有者为root。于是问题是:
直接使用命令时,使用的用户是root,而systemctl配置文件中配置的用户是mongodb,所以当使用root启动后,有一些文件就变成了root所有,于是systemctl尝试使用mongodb用户启动时,就无法访问这些文件,导致失败。
解决方法就是将这些文件的所有者改为mongodb,即systemctl中配置的用户。
当然后续启动时,发现还有一些文件变成了root所有,不过这些文件均处于data/目录下,找到它们并修改后即可使用systemctl启动。