如何在Ubuntu上将tar.gz中提取的MongoDB设置为服务?

4
由于某些原因,我所在区域无法使用终端中的 apt-get 正常下载和安装 MongoDB。因此,我从 http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/ 下载了 .tar.gz 版本。
我可以启动它的 mongod 程序。但是,我希望将 mongod 安装为一个服务,在 Ubuntu 14.04 中开机自启动。如何正确地操作呢?我尝试了一些由这个网站提供的 init.d 脚本:https://ewan.im/15/mongodb-initd-script 唯一的问题是:在 14.04 版本中,他们移除了 chkconfig。我的尝试安装 chkconfig 最终以失败告终。
Package chkconfig is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'chkconfig' has no installation candidate

编辑:我尝试了https://askubuntu.com/questions/221293/why-is-chkconfig-no-longer-available-in-ubuntu,但也没有成功。

我希望使用 upstart 或者 systemd。有人有示例或其他解决方案吗?


你试过这个替代chkconfig的方法吗?http://askubuntu.com/questions/221293/why-is-chkconfig-no-longer-available-in-ubuntu - Bertrand Martel
不行,已经尝试过:sudo sysv-rc-conf mongod on,连接仍然出错,但如果我直接执行mongod,则可以运行! - DennyHiu
我不能为您启动MongoDB服务提供帮助,因为这取决于您的个人配置,但如果您需要,我可以创建一个init.d脚本,在特定的运行级别下启动和关闭mongod。 - Bertrand Martel
2个回答

3
这里提供一种在init.d中启动mongodb作为“服务”的解决方案:
#!/bin/sh
#title         :mongod
#author        :Bertrand Martel
#date          :15/08/2015
#description   :start/stop/restart mongod
#########################################
### install   :  cp mongod /etc/init.d/
#                update-rc.d mongod defaults
### uninstall :  update-rc.d -f mongodb remove

PATH_TO_MONGO=/usr/bin/mongod

#file containing all mongodb pid
PID_FILE=/tmp/mongodb.pid

case "$1" in
    start)
        echo "Starting mongodb service..."

        COMMAND_TO_RUN=`start-stop-daemon -S -b -m -p $PID_FILE -x $PATH_TO_MONGO& :`
        setsid sh -c $COMMAND_TO_RUN> /dev/null 2>&1 < /dev/null

        echo -e "\E[31;33m[ OK ]\E[0m"
        ;;
    stop)
        echo "Stopping mongodb service..."

        start-stop-daemon -K -q -p $PID_FILE

        echo -e "\E[31;33m[ OK ]\E[0m"
        ;;
    restart|reload)
        "$0" stop
        "$0" start
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

安装:

cp mongod /etc/init.d/

update-rc.d mongod defaults

卸载:

update-rc.d -f mongodb remove

开始:

/etc/init.d/mongod start

停止:
/etc/init.d/mongod stop

重新启动:

/etc/init.d/mongod restart

现在,mongod已经自动启动了。

代码片段: https://gist.github.com/bertrandmartel/a3865fa441248f23d51e


谢谢!我能够使用上面的init.d脚本启动mongod服务。但是有一些让我困惑的地方。cp mongod /etc/init.d/这一行意味着mongod可执行文件本身必须被复制到/etc/init.d文件夹中。为什么?我只需要在将该脚本复制到init.d文件夹后使用sudo service mongod start启动我的服务,其中mongod是我复制到init.d文件夹中的脚本文件。 - DennyHiu
还有一件事我真的很想问,上面的这个脚本是"通用"的吗?我的意思是,它适用于所有Linux系统(SUSE,Debian等),还是只适用于Ubuntu系统? - DennyHiu
这个脚本适用于基于System V的发行版,这是最常见的Linux系统。System V将脚本存储在rcX.d目录中,而BSD系统将其存储在/etc/rc.d中。init.d脚本是旧的做法,因此service允许在upstart脚本和init.d脚本之间进行过渡。upstart旨在与init.d兼容。@denny对于您的第一个问题,我没有将mongod的可执行文件复制到init.d文件夹中,而是放置了上面的启动脚本。我将脚本命名为mongod。 - Bertrand Martel

0

对于任何试图在Debian 8上执行此操作的人,我在顶部添加了这些LSB标签以使其正常工作:

### BEGIN INIT INFO
# Provides:          mongod
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MongoDB init script
# Description:       MongoDB
### END INIT INFO

另外,将第一行改为:

#!/bin/bash

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接