Linux 安装MongoDB并设为开机启动

环境

  1. centos 7
  2. mongo3.4.9

安装启动

1.解压

tar -zxvf mongodb-linux-x86_64-3.4.9.tgz -C /opt/mongodb

2. 创建配置文件

cd /opt/mongodb/mongodb-linux-x86_64-3.4.9

vi config/mongodb.conf

dbpath = /opt/mongodb/mongodb-linux-x86_64-3.4.9/data/db #数据文件存放目录  
logpath = /opt/mongodb/mongodb-linux-x86_64-3.4.9/logs/mongodb.log #日志文件存放目录  
bind_ip = 0.0.0.0  远程访问
port = 27017  #端口  
fork = true  #以守护程序的方式启用,即在后台运行  
nohttpinterface = true

3. 启动

bin/mongod –config config/mongodb.conf

4. 创建数据库和用户

-- 进入命令行
mongo
-- 创建data数据库
use data
-- 创建用户
db.createUser(
    {
      user: "test",
      pwd: "123456",
      roles: ["readWrite"]
    }
 ) 

设为开机启动

1. 创建配置文件

vi /etc/init.d/mongod

#!/bin/bash

export MONGO_HOME=/opt/mongodb/mongodb-linux-x86_64-3.4.9
#chkconfig:2345 20 90
#description:mongod
#processname:mongod
case $1 in
          start) 
              $MONGO_HOME/bin/mongod --config $MONGO_HOME/config/mongodb.conf
              ;;
          stop)
              $MONGO_HOME/bin/mongod  --shutdown --config $MONGO_HOME/config/mongodb.conf
              ;;
          status)
              ps -ef | grep mongod
              ;;
          restart)
              $MONGO_HOME/bin/mongod  --shutdown --config $MONGO_HOME/config/mongodb.conf
              $MONGO_HOME/bin/mongod --config $MONGO_HOME/config/mongodb.conf
              ;;
          *)
              echo "require start|stop|status|restart"
              ;;
esac

添加服务并设置为开机启动

可执行权限

chmod 755 /etc/init.d/mongod

添加服务

chkconfig –add mongod

设置开机启动

chkconfig mongod on

猜你喜欢

转载自blog.csdn.net/u013076044/article/details/80251720