系统部署之mongodb安装

系统部署之mongodb安装

安装文件

  • mongodb-linux-x86_64-rhel70-3.4.5.tgz

解压安装

  1. tar -zxf mongodb-linux-x86_64-rhel70-3.4.5.tgz
  2. mv mongodb-linux-x86_64-rhel70-3.4.5 /usr/local/mongodb

创建用户

  1. mkdir /data/mongodb/{log,conf,data}
  2. groupadd mongodb
  3. useradd -g mongodb -s /sbin/nologin mongodb
  4. chown -R mongodb:mongodb /data/mongodb

修改配置

master

#master.conf
dbpath=/data/mongodb/data
logpath=/data/mongodb/log/master.log
pidfilepath=/data/mongodb/log/master.pid
directoryperdb=true
logappend=true
replSet=rs
bind_ip=本机IP
port=27017
oplogSize=10000
fork=true
noprealloc=true
#auth=true
#keyFile=/data/mongodb/mongodb-cluster-key

slave

#slaver.conf
dbpath=/data/mongodb/data
logpath=/data/mongodb/log/slaver.log
pidfilepath=/data/mongodb/log/slaver.pid
directoryperdb=true
logappend=true
replSet=rs
bind_ip=本机IP
port=27017
oplogSize=10000
fork=true
noprealloc=true
#auth=true
#keyFile=/data/mongodb/mongodb-cluster-key

arbiter

#arbiter.conf
dbpath=/data/mongodb/data
logpath=/data/mongodb/log/arbiter.log
pidfilepath=/data/mongodb/log/arbiter.pid
directoryperdb=true
logappend=true
replSet=rs
bind_ip=本机IP
port=27017
oplogSize=10000
fork=true
noprealloc=true
#auth=true
#keyFile=/data/mongodb/mongodb-cluster-key

系统服务

master-service

/lib/systemd/system/mongodb.service 

[Unit]
Description=mongodb
After=network.target

[Service]
Type=forking
User=mongodb
ExecStart=/usr/local/mongodb/bin/mongod --config /data/mongodb/conf/master.conf
Restart=on-failure

[Install]
WantedBy=multi-user.target

其他类似,修改配置文件地址即可

启动3台服务

集群配置

mongo 主IP:27017
use admin
cfg={ _id:"rs", members:[ {_id:0,host:'masterIP:27017',priority:2}, {_id:1,host:'slave ip:27017',priority:1},
{_id:2,host:'arbiter ip:27017',arbiterOnly:true}] };
rs.initiate(cfg) 

认证配置

mongo 主IP:27017
use admin
db.system.users.remove({})
db.system.version.remove({})
db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })
db.createUser({user:"admin",pwd:"admin",roles:[{role:"userAdminAnyDatabase",db:"admin"},{role:"dbAdminAnyDatabase",db:"admin"},{role:"readWriteAnyDatabase",db:"admin"}]})
db.grantRolesToUser('admin',["clusterManager",{role:"clusterManager",db:"admin"}])
use rfid
db.createUser({user:"rfid",pwd:"rfid",roles:[{role:"dbOwner",db:"rfid"},{role:"readWrite",db:"rfid"},{role:"dbAdmin",db:"rfid"}]})

生成集群认证KEY

  1. openssl rand -base64 741 > /data/mongodb/mongodb-cluster-key
  2. chmod 600 /data/mongodb/mongodb-cluster-key
  3. chown mongodb:mongodb /data/mongodb/mongodb-cluster-key

关闭服务,取消配置文件最后2行注释

标签: none

添加新评论