mongodb 部署笔记(二)

325 阅读1分钟

回首往昔

==mongodb程序路径:/home/mongodb/software/mongodb-linux-x86_64-rhel70-4.2.1 mongodb数据库文件路径:/home/mongodb/software/mongo/data/db/ mongodb日志文件路径:/home/mongodb/software/mongo/logs/==

  1. 创建mongodb配置文件
sudo vi /home/mongodb/software/mongo/mongo.conf

##进入后

fork=true dbpath=/home/mongodb/software/mongo/data/db port=27017 bind_ip=0.0.0.0 logpath=/home/mongodb/software/mongo/logs/mongodb.log logappend=true replSet=database_repl_grid ##自定义

2. 启动mongo服务

cd mongodb-linux-x86_64-rhel70-4.2.1/bin/

./mongod --config ../../mongo/mongo.conf

3. 查看是否成功启动

netstat -ntlp

4. 开启防火墙特定端口,重启防火墙

sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent sudo firewall-cmd --reload sudo firewall-cmd --list-all

  1. 配置副本集

var rsconf = {
    _id:'grid_database', //_id要与配置文件中指定的服务所属的复制集相同
    members:  //复制集成员
    [
        {
            _id:1, //成员的id
            host:'172.16.12.19:27017' //成员所属节点的ip以及该成员服务启动时所占的端口
        },
        {
            _id:2,
            host:'172.16.12.20:27017'
        },
        {
            _id:3,
            host:'172.16.12.21:27017'
        }
    ]
}

5.设置开机自启

1. cd /etc/init.d/
2. vi mongod

#!/bin/bash

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

3. 创建成功后,保存文件此时的 mongod 文件还是普通文件,还需要设置为可执行文件,输入命令 

chmod +x mongod 
4.可执行权限

chmod 755 /etc/init.d/mongod

5.添加服务

chkconfig --add mongod

6.设置开机启动

chkconfig mongod on

7. 任务启动命令(执行打开mongo的命令)
    systemctl start mongod