MongoDB database high availability solution-MongoDB replication set (master-slave replication)
Preface
This environment is based on the Centos 7.8 system to build mongodb-enterprise-4.2.8 learning environment For
specific construction, please refer to mongodb-enterprise-4.2.8 environment construction
MongoDB replication is the process of synchronizing data to multiple servers; the replication set provides redundant backup of data and improves the availability of data, usually ensuring data security; the replication set also allows technicians to recover from hardware failures and service interruptions Data recovery.
1. What is a MongoDB replica set?
What is a replica set
- Ensure data security
- Data redundancy and backup
- High data availability (24*7)
- Disaster recovery
- No downtime for maintenance (such as backup, rebuild index, compression)
- Distributed read data
- The replica set is transparent to the application layer (relative to the end user)
The working principle of MongoDB replica set
- 1. The replication set of mongodb requires at least two nodes. One of them is the master node, which is responsible for processing client requests, and the rest are slave nodes, which are responsible for copying data on the master node.
- 2. The common collocation method of each node of mongodb is: one master and one slave, one master and multiple slaves.
- 3. The master node records all operation oplogs on it, and the slave node periodically polls the master node to obtain these operations, and then performs these operations on its own data copy, so as to ensure that the data of the slave node is consistent with the master node
2. MongoDB replica set architecture design
Features of MongoDB replica set
- N-node cluster
- Any node can be used as the master node
- All write operations are on the master node
- Automatic failover
- Automatic recovery
Three, the actual case of MongoDB replication set
1. MongoDB replica set deployment
Environmental preparation
role | host | ip | mongodb-version |
---|---|---|---|
master | node01 | 192.168.5.11 | mongodb-enterprise-4.2.8 |
slave1 | node02 | 192.168.5.12 | mongodb-enterprise-4.2.8 |
slave2 | node03 | 192.168.5.13 | mongodb-enterprise-4.2.8 |
Modify MongoDB configuration file
# 所有节点修改
--- node01
[root@node01 ~]# vim /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1,192.168.5.11
replication:
oplogSizeMB: 1024
replSetName: myRs
[root@node01 ~]# systemctl restart mongod
--- node02
[root@node02 ~]# vim /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1,192.168.5.12
replication:
oplogSizeMB: 1024
replSetName: myRs
[root@node02 ~]# systemctl restart mongod
--- node03
[root@node03 ~]# vim /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1,192.168.5.13
replication:
oplogSizeMB: 1024
replSetName: myRs
[root@node03 ~]# systemctl restart mongod
Configure the master node
# 登录MongoDB
[root@node01 ~]# mongo
MongoDB shell version v4.2.8
# 查看ReplSet状态
MongoDB Enterprise > rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 初始化ReplSet复制集
MongoDB Enterprise > rs.initiate({
_id:'myRS',members:[{
_id:1,host:'192.168.5.11:27017'}]})
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1613533287, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1613533287, 1)
}
Check the status of ReplSet again
to add slave nodes on the master
MongoDB Enterprise myRS:PRIMARY> rs.add('192.168.5.12:27017','192.168.5.13:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1613534429, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1613534429, 1)
}
View ReplSet status
The master node inserts data to test the master-slave synchronization
Main insert data
# 插入数据
MongoDB Enterprise myRS:PRIMARY> use test
switched to db test
MongoDB Enterprise myRS:PRIMARY> for(var i =0; i <4; i ++){
db.user.insert({
userName:'my'+i,age:i})}
WriteResult({
"nInserted" : 1 })
# 查看数据
MongoDB Enterprise myRS:PRIMARY> db.user.find()
{
"_id" : ObjectId("602c98c21b314e8e6e90e15a"), "userName" : "my0", "age" : 0 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15b"), "userName" : "my1", "age" : 1 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15c"), "userName" : "my2", "age" : 2 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15d"), "userName" : "my3", "age" : 3 }
slave01 view
slave02 view
MongoDB replication set succeeded!!!
2. Simulate master failure to realize failover and failure recovery
Simulate shutdown of the master node
MongoDB Enterprise myRS:PRIMARY> use admin
switched to db admin
MongoDB Enterprise myRS:PRIMARY> db.shutdownServer()
2021-02-17T12:30:51.021+0800 I NETWORK [js] DBClientConnection failed to receive message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
server should be down...
2021-02-17T12:30:51.038+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2021-02-17T12:30:51.038+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2021-02-17T12:30:51.042+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2021-02-17T12:30:51.042+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
MongoDB Enterprise >
node03 view
Switch node02 to master
node02 to view and insert three records
[root@node02 ~]# mongo
MongoDB shell version v4.2.8
MongoDB Enterprise myRS:PRIMARY> use test
switched to db test
MongoDB Enterprise myRS:PRIMARY> for(var i =4; i <7; i ++){
db.user.insert({
userName:'my'+i,age:i})}
WriteResult({
"nInserted" : 1 })
MongoDB Enterprise myRS:PRIMARY> db.user.find()
{
"_id" : ObjectId("602c98c21b314e8e6e90e15a"), "userName" : "my0", "age" : 0 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15b"), "userName" : "my1", "age" : 1 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15c"), "userName" : "my2", "age" : 2 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15d"), "userName" : "my3", "age" : 3 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e15"), "userName" : "my4", "age" : 4 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e16"), "userName" : "my5", "age" : 5 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e17"), "userName" : "my6", "age" : 6 }
node03 view
start master
[root@node01 ~]# systemctl start mongod
[root@node01 ~]# systemctl is-active mongod
active
node03 view master-slave status
node01 has switched to the salve state and
node02 inserts three pieces of data again
MongoDB Enterprise myRS:PRIMARY> use test
switched to db test
MongoDB Enterprise myRS:PRIMARY> for(var i =7; i <10; i ++){
db.user.insert({
userName:'my'+i,age:i})}
WriteResult({
"nInserted" : 1 })
MongoDB Enterprise myRS:PRIMARY> db.user.find()
{
"_id" : ObjectId("602c98c21b314e8e6e90e15a"), "userName" : "my0", "age" : 0 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15b"), "userName" : "my1", "age" : 1 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15c"), "userName" : "my2", "age" : 2 }
{
"_id" : ObjectId("602c98c21b314e8e6e90e15d"), "userName" : "my3", "age" : 3 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e15"), "userName" : "my4", "age" : 4 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e16"), "userName" : "my5", "age" : 5 }
{
"_id" : ObjectId("602c9d4a2db6914f63068e17"), "userName" : "my6", "age" : 6 }
{
"_id" : ObjectId("602c9f71912509e6bd44076c"), "userName" : "my7", "age" : 7 }
{
"_id" : ObjectId("602c9f71912509e6bd44076d"), "userName" : "my8", "age" : 8 }
{
"_id" : ObjectId("602c9f71912509e6bd44076e"), "userName" : "my9", "age" : 9 }
node01 view data
node03 view data
Failover, failure recoverySuccessfully achieved! ! !