mongoDB Replica Set Actual Warfare
background
Features of a mongoDB single server:
- Risk of data loss - Single server cannot be highly available
Features of the mongoDB replica set:
-High Availability Architecture to Prevent Data Loss - Keep multiple replicas synchronized and consistent - The mongodb replica set switches automatically when there is a problem
Preparation for actual combat
Replica Set Environment Configuration
- At least three servers in a production environment
Machine IP
host name
mongo port
Profile Path
role
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
- mongo software: https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.17.tgz
- configuration file
- The single-node configuration is as follows:
[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 #Start Services [root@centos7-node1 ~]# netstat -tanlp | grep mongo #Process Viewing
- The configuration of other nodes only needs to modify the port, then modify the configuration file, log directory, and the following is the service start command
[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
Replica Set Initialization
Notes:
- Sign in to a mongdb at random
[root@centos7-node1 27017]# /usr/local/mongodb/bin/mongo localhost:27017 #Log on to the first station > 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()
Test replica set data synchronization
- Inserting data can only be done from Primary, and Secondary can only read data
copySet:PRIMARY> use test copySet:PRIMARY> db.myuser.insert({userid:1})
- Query data read from library: ok if no exception
copySet:SECONDARY> rs.slaveOk() copySet:SECONDARY> show dbs copySet:SECONDARY> use test copySet:SECONDARY> db.myuser.find() copySet:SECONDARY> rs.printSlaveReplicationInfo() #View Delay From Library
Automatic switching of replica sets
- When primary hangs up, one of them will be elected as primary
- If there is only one instance left in the cluster, an exception will occur: (if there is only one)
# Log on to primary and then stop service copySet:PRIMARY> use admin copySet:PRIMARY> db.shutdownServer()
The remaining two will elect primary nodes
Specify primary by priority
- Default priority weights are all 1
- primary in the replica set is the most weighted one selected
- The priority weight is set as follows [at the primary node]:
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)
Scaling of replica sets
- The mongoDB replica set is very scalable, and it is easy to add and delete instances to the replica set
- Add replica set data to automatically synchronize
- Prepare a new machine and copy, set port and configuration file to 27020, and copy name
- Adding a replica set to the cluster is done on primary as follows
[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 #Start a new node copySet:PRIMARY> rs.add('192.168.56.14:27020') #The primary node joins the new node and the data synchronizes automatically copySet:PRIMARY> rs.remove('192.168.56.14:27020') #Remove secondary
Backup and recovery of mongoDB
- Single server, must backup
-
The backup and restore tools are:
- mongodump: backup
- mongostore: restore
- Backup of replica sets is done on primary
Full backup and restore
~]# mkdir /data/mongodbbacku ~]# /usr/local/mongodb/bin/mongodump -h 127.0.0.1:27020 -o /data/mongodbbackup/ #Backup Data ~]# /usr/local/mongodb/bin/mongorestore -h 127.0.0.1 --port 27021 /data/mongodbbackup/ #Restore data