我曾关注关联将 mongod 添加为 Linux 服务。服务器重启后,mongod 服务器不会重新启动。
/etc/rc.d/init.d/mongod文件内容:
#!/bin/bash
#
# mongodb Startup script for the mongodb server
#
# chkconfig: - 64 36
# description: MongoDB Database Server
#
# processname: mongodb
#
# Source function library
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/mongodb ]; then
. /etc/sysconfig/mongodb
fi
prog="mongod"
mongod="/usr/bin/mongod"
mongodb_user="myUserAdmin"
RETVAL=0
start() {
echo -n $"Starting $prog: "
#daemon $mongod "--fork --logpath /var/log/mongodb.log --logappend 2>&1 >>/var/log/mongodb.log"
daemon --user=$mongodb_user $mongod "--config /etc/mongodb.conf"
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}
reload() {
echo -n $"Reloading $prog: "
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
reload)
reload
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
RETVAL=1
esac
exit $RETVAL
通过引用 /var/log/messages
显示的错误是
> Mar 4 09:04:44 redhat7 systemd: Starting SYSV: MongoDB Database Server...
Mar 4 09:04:44 redhat7 systemd: Failed at step EXEC spawning /etc/rc.d/init.d/mongod: Exec format error
Mar 4 09:04:44 redhat7 systemd: mongod.service: control process exited, code=exited status=203
Mar 4 09:04:44 redhat7 systemd: Failed to start SYSV: MongoDB Database Server.
Mar 4 09:04:44 redhat7 systemd: Unit mongod.service entered failed state.
Mar 4 09:04:44 redhat7 systemd: mongod.service failed.
请帮我。
答案1
可能不是您想要的答案,但您可以考虑稍微不同的方法。
您的说明是 2013 年的,与 systemd 之前的 RedHat 有关。您使用的是 RedHat 7,据我所知,它有 systemd,我建议您使用它,而不是使用 init 脚本。
虽然我很惊讶你在安装 mongo 时没有合适的脚本,但我认为考虑使用 mongo 自己的基于 RPM 的系统服务单元(取自https://github.com/mongodb/mongo/blob/master/rpm/mongod.service):
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongod
Group=mongod
Environment="OPTIONS=--quiet -f /etc/mongod.conf"
ExecStart=/usr/bin/mongod $OPTIONS run
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
您可能想将其放入 /etc/systemd/system/mongod.service,并可能禁用您的初始化脚本。