标签:shutdown pat setname 名称 背景 admin tps dir amp
mongoDB副本集实战mongoDB单台服务器的特点:
- 数据有丢失风险
- 单台服务器无法做高可用
mongoDB副本集的特点:
- 高可用架构,预防数据丢失
- 多台副本数据保持同步和一致
- mongodb副本集在有问题的时候自动切换
机器IP
主机名
mongo端口
配置文件路径
角色
192.168.56.11
centos7-node1
27017
/data/mongodb/27017/mongodb.conf
192.168.56.12
centos7-node2
27018
/data/mongodb/27018/mongodb.conf192.168.56.13
centos7-node3
27019
/data/mongodb/27019/mongodb.conf
[root@centos7-node1 ~]# cd /opt/ && wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.17.tgz
[root@centos7-node1 ~]# tar xf /opt/mongodb-linux-x86_64-4.0.17.tgz
[root@centos7-node1 ~]# mv mongodb-linux-x86_64-4.0.17/ /usr/local/mongodb
[root@centos7-node1 ~]# mkdir -pv /data/mongodb/27017
[root@centos7-node1 ~]# vim /data/mongodb/27017/mongodb.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/27017/mongodb.log
storage:
dbPath: /data/mongodb/27017/
journal:
enabled: true
processManagement:
fork: true
net:
port: 27017
bindIp: 0.0.0.0
replication:
replSetName: copySet
[root@centos7-node1 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27017/mongodb.conf #启动服务
[root@centos7-node1 ~]# netstat -tanlp | grep mongo #进程查看
[root@centos7-node2 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27018/mongodb.conf
[root@centos7-node3 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27019/mongodb.conf
需要注意的事项:
[root@centos7-node1 27017]# /usr/local/mongodb/bin/mongo localhost:27017 #登陆到第一台
> config={
_id: "copySet",members:[
{_id:0,host:"192.168.56.11:27017"},
{_id:1,host:"192.168.56.12:27018"},
{_id:2,host:"192.168.56.13:27019"},
]
}
> rs.initiate(config)
> rs.status()
copySet:PRIMARY> use test
copySet:PRIMARY> db.myuser.insert({userid:1})
copySet:SECONDARY> rs.slaveOk()
copySet:SECONDARY> show dbs
copySet:SECONDARY> use test
copySet:SECONDARY> db.myuser.find()
copySet:SECONDARY> rs.printSlaveReplicationInfo() #查看从库延时
# 登陆primary,然后停服务
copySet:PRIMARY> use admin
copySet:PRIMARY> db.shutdownServer()
其余两台会选举primary节点
copySet:PRIMARY> conf = rs.config()
copySet:PRIMARY> conf.members[0].priority = 5
copySet:PRIMARY> conf.members[1].priority = 4
copySet:PRIMARY> conf.members[2].priority = 3
copySet:PRIMARY> rs.reconfig(conf)
[root@centos7-node4 ~]# vim /data/mongodb/27020/mongodb.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/27020/mongodb.log
storage:
dbPath: /data/mongodb/27020/
journal:
enabled: true
processManagement:
fork: true
net:
port: 27020
bindIp: 0.0.0.0
replication:
replSetName: copySet
[root@centos7-node4 ~]# /usr/local/mongodb/bin/mongod -f /data/mongodb/27020/mongodb.conf #启动新节点
copySet:PRIMARY> rs.add(‘192.168.56.14:27020‘) #primary节点加入新的节点,然后数据自动同步
copySet:PRIMARY> rs.remove(‘192.168.56.14:27020‘) #移除secondary
备份还原工具有:
~]# mkdir /data/mongodbbacku
~]# /usr/local/mongodb/bin/mongodump -h 127.0.0.1:27020 -o /data/mongodbbackup/ #备份数据
~]# /usr/local/mongodb/bin/mongorestore -h 127.0.0.1 --port 27021 /data/mongodbbackup/ #还原数据
标签:shutdown pat setname 名称 背景 admin tps dir amp
原文地址:https://blog.51cto.com/13812615/2484716