欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mongodb数据库添加系统服务教程

程序员文章站 2022-06-17 19:20:10
1.在/etc/init.d目录下创建mongod文件,并赋予正确的权限: root@ubuntu:/etc/init.d# touch mongod root@ubunt...

1.在/etc/init.d目录下创建mongod文件,并赋予正确的权限:

root@ubuntu:/etc/init.d# touch mongod
root@ubuntu:/etc/init.d# chmod 755 mongod

2.编辑mongod文件

#!/bin/sh
### BEGIN INIT INFO
# Provides:     mongod
# Required-Start:   $local_fs $syslog
# Required-Stop:    $local_fs $syslog
# Default-Start:    2 3 4 5
# Default-Stop:     
# Short-Description:   mongodb service
### END INIT INFO

start_mongodb() 
{
    ps -ef | grep -v "grep" | grep "/usr/local/mongodb/bin/mongod"
    if [ $? -eq 0 ];then
        echo "mongodb is in running!"
        return 0
    fi
    /usr/local/mongodb/bin/mongod --auth &
}

stop_mongodb()
{
    /usr/local/mongodb/bin/mongod --shutdown
    if [ $? -eq 0 ];then
        echo "stop mongodb service successfully!"
    else
        echo "stop mongodb service failed!"
    fi
}

query_status()
{
    ps -ef | grep -v "grep" | grep "/usr/local/mongodb/bin/mongod"
    if [ $? -eq 0 ];then
        echo "mongodb is in running!"
    else
        echo "mongodb is not in running!"
    fi
}

case "$1" in
    start)
        start_mongodb
        ;;
    stop)
        stop_mongodb
        ;;
    restart)
        stop_mongodb
        start_mongodb
        ;;
    status)
        query_status
        ;;
    *)
        echo "usage: service mongodb start|stop|restart|status"
        ;;
esac

exit 0
root@ubuntu:/etc/init.d# update-rc.d mongod defaults

就可以使用命令管理mongod服务了

root@ubuntu:/etc/init.d# service mongod start