Brief introduction
MongoDB replication is the process of synchronizing data across multiple servers.
Replication sets provide redundant backups of data and store copies of data on multiple servers, increasing the availability of data and guaranteeing data security.
Replication sets also allow recovery of data from hardware failures and service outages.
Benefits of replication Sets
- Secure your data
- Data high Availability (24*7)
- Disaster recovery
- No downtime maintenance (e.g. backup, rebuild index, compression)
- Distributed Read data
MongoDB Replication principle
MongoDB requires a minimum of two nodes for replication. One is the master node, which handles client requests, and the rest is the slave node, responsible for replicating the data on the master node.
MongoDB each node common collocation method is: A master one from, a master many from.
- The master node records all operations on it, oplog the primary node periodically from the node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
The MONGODB replication structure diagram looks like this:
In the above structure diagram, the client reads data from the primary node, and when the client writes data to the master node, the master node interacts with the data from the node to ensure data consistency.
Replica set Features:
- N-node clusters
- Any node can be used as the master node
- All write operations are on the primary node
- Auto Fail-Over
- Automatic recovery
First, deploy MONGODB replication set 1. Create a multi-instance configuration file and turn on the service. (1) Create a data file and log file storage path
[[email protected] ~]# mkdir -p /data/mongodb/mongodb{2,3,4} //创建数据目录[[email protected]localhost ~]# mkdir -p /data/logs/mongodb [[email protected] ~]# touch /data/logs/mongodb/mongodb{2,3,4}.log //创建日志文件[[email protected] ~]# chmod -R 777 /data/logs/mongodb/*.log //赋予权限
(2) Edit the configuration file for 4 MongoDB instances
First edit the configuration file for MongoDB instance 1, configure the replication option, and copy 3 copies.
vim /etc/mongod.conf replication: replSetName: kgcrs //配置replSetName参数为kgcrs
Restarting a MongoDB instance 1
[[email protected] ~]# mongod -f /etc/mongod.conf --shutdown killing process with pid: 1074[[email protected] ~]# mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections.forked process: 17130child process started successfully, parent exiting
Copy 3 copies, and modify the configuration file in the port parameter configuration, dbpath parameter configuration, logpath parameter configuration.
Modify the configuration file parameters of the mongod2.conf.
vim /etc/mongod2.conf path: /data/logs/mongodb/mongodb2.log //日志文件存储路径 dbPath: /data/mongodb/mongodb2 //数据文件路径 port: 27018 //监听端口
Modify the configuration file parameters of the mongod3.conf.
vim /etc/mongod3.conf path: /data/logs/mongodb/mongodb3.log //日志文件存储路径 dbPath: /data/mongodb/mongodb3 //数据文件路径 port: 27019 //监听端口
Modify the configuration file parameters of the mongod4.conf.
vim /etc/mongod4.conf path: /data/logs/mongodb/mongodb4.log //日志文件存储路径 dbPath: /data/mongodb/mongodb4 //数据文件路径 port: 27020 //监听端口
(3) Start a multi-instance service
mongod -f /etc/mongod2.confmongod -f /etc/mongod3.confmongod -f /etc/mongod4.conf
You can see all four instances start up.
2. Configuring a replication set of 3 nodes
[[email protected] ~]# mongo //进入MongoDB27017实例MongoDB shell version v3.6.6connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.6
(1) First view the status information of the replica set through the Rs.status () command, indicating that the replica set is not yet configured.
(2) Define the CFG initialization parameters.
When the configuration is initialized, no data is guaranteed from the node or the data is lost.
> cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.113.176:27017"},{"_id":1,"host":"192.168.113.176:27018"},{"_id":2,"host":"192.168.113.176:27019"}]}
(3) Start the replication set with the rs.initiate (CFG) command.
> rs.initiate(cfg)
(4) View the replication set status.
After you start the replication set, view the full state information for the replica set again through the Rs.status () command.
Of these, health is 1, and 0 represents downtime. State 1 represents the primary node, and 2 represents the slave node.
Second, add and delete nodes
After you configure the start replica set, you can easily add or remove nodes by using the Rs.add () and Rs.remove () commands.
#添加节点kgcrs:PRIMARY> rs.add("192.168.113.176:27020") kgcrs:PRIMARY> rs.status() //查看节点是否添加成功
#删除节点kgcrs:PRIMARY> rs.remove("192.168.113.176:27020")kgcrs:PRIMARY> rs.status()
You can see that 27020 of the nodes are gone.
Third, MongoDB replica set switch
The MongoDB replica set enables high availability of the cluster and automatically switches to other nodes when the primary node fails. Administrators can also manually perform a master-slave switchover of the replica set.
1. Analog Fault Auto-transfer
The KILL command lets you stop the current master node of the replica set, and then view the master node automatically switching to the other node.
(1) First view the MongoDB process, stop the current master node 27017
[[email protected] ~]# ps aux | grep mongodroot 17130 1.0 6.2 1582772 62064 ? Sl 09:33 1:06 mongod -f /etc/mongod.confroot 17830 0.8 5.7 1462576 57628 ? Sl 09:58 0:45 mongod -f /etc/mongod2.confroot 17880 0.8 5.8 1522504 58324 ? Sl 09:58 0:45 mongod -f /etc/mongod3.confroot 17927 0.7 5.3 1441856 53356 ? Sl 09:58 0:37 mongod -f /etc/mongod4.confroot 20678 0.0 0.0 112676 984 pts/1 S+ 11:23 0:00 grep --color=auto mongod[[email protected] ~]# kill -9 17130
(2) View primary node switch
[[email protected] ~]# mongo -port 27018kgcrs:SECONDARY> rs.status()
2. Manual or Master-slave switching (1) advanced to Node 27019, suspend election
[[email protected] ~]# mongo -port 27019kgcrs:PRIMARY> rs.freeze(30) //暂停30s不参与选举kgcrs:PRIMARY> rs.stepDown(60,30) //交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步
(2) View primary node switch
kgcrs:SECONDARY> rs.status()
MongoDB Configuration Replica Set