简体   繁体   English

如何在Ubuntu中运行mongodb(从tar.gz中提取)作为服务?

[英]How to run mongodb (extracted from tar.gz) as service in Ubuntu?

For some reason in my area, I'm unable to download and install mongodb normally using apt-get from terminal. 由于某些原因,在我所在的地区,我无法通过终端使用apt-get来下载和安装mongodb。 So I downloaded the .tar.gz version from http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/ 所以我从http://docs.mongodb.org/manual/tutorial/install-mongodb-on-linux/下载了.tar.gz版本

I can start it's mongod program. 我可以开始它的mongod程序。 But I want to install mongod as a service that runs at startup in my Ubuntu 14.04. 但我想安装mongod作为在我的Ubuntu 14.04启动时运行的服务。 How to do this correctly ? 怎么做到这一点? I tried some init.d script provided by this : https://ewan.im/15/mongodb-initd-script 我尝试了一下这个提供的init.d脚本: https//ewan.im/15/mongodb-initd-script

The only problem is: in 14.04, they removed the chkconfig . 唯一的问题是:在14.04,他们删除了chkconfig My experiment to install chkconfig ended up with 我的安装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

edit: I tried https://askubuntu.com/questions/221293/why-is-chkconfig-no-longer-available-in-ubuntu with no luck either. 编辑:我尝试了https://askubuntu.com/questions/221293/why-is-chkconfig-no-longer-available-in-ubuntu也没有运气。

I expect that I should use upstart or systemd . 我希望我应该使用upstartsystemd Can anyone have example or maybe another solution? 任何人都可以有例子或者其他解决方案吗

Here is a workaround for starting mongodb as a "service" in init.d : 以下是在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 $?

For install : 安装方式:

cp mongod /etc/init.d/

update-rc.d mongod defaults

For uninstall : 对于卸载:

update-rc.d -f mongodb remove

starting : 开始:

/etc/init.d/mongod start

stopping : 停止:

/etc/init.d/mongod stop

restarting : 重启:

/etc/init.d/mongod restart

mongod is automatically launching at boot now. mongod现在在启动时自动启动。

gist : https://gist.github.com/bertrandmartel/a3865fa441248f23d51e 要点: https//gist.github.com/bertrandmartel/a3865fa441248f23d51e

For anyone trying to do this on Debian 8, I added these LSB tags near the top to get it working: 对于任何试图在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

also, change the first line to: 另外,将第一行更改为:

#!/bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM