Linux install MongoDB and set it to start on boot

environment

  1. cents 7
  2. mongo3.4.9

Installation starts

1. Unzip

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

2. Create a configuration file

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. Start

bin/mongod –config config/mongodb.conf

4. Create database and user

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

set to boot

1. Create a configuration file

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

Add a service and set it to start at boot

executable permission

chmod 755 /etc/init.d/mongod

Add service

chkconfig –add mongod

set startup

chkconfig mongod on

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326265194&siteId=291194637