MongoDB replica Set Primary-Secondary switchover Experiment
1. Create a directory for the replica Section
[Root @ bakdbserver ~] # Mkdir-p/var/lib/mongo/dbs/node1
[Root @ bakdbserver ~] # Mkdir-p/var/lib/mongo/dbs/node2
[Root @ bakdbserver ~] # Mkdir-p/var/lib/mongo/dbs/node3
2. Start the replica set
Node 1:
[Root @ bakdbserver ~] # Mongod -- dbpath/var/lib/mongo/dbs/node1 -- port 10001 -- nojournal -- replSet blort
Node 2 and 3:
Mongod -- dbpath/var/lib/mongo/dbs/node2 -- port 10002 -- nojournal -- replSet blort
Mongod -- dbpath/var/lib/mongo/dbs/node3 -- port 10003 -- nojournal -- replSet blort
3. initialize the replica set
Rs. initiate ({"_ id": "blort", "members ":[
{"_ Id": 1, "host": "bakdbserver: 10001 "},
{"_ Id": 2, "host": "bakdbserver: 10002 "},
{"_ Id": 3, "host": "bakdbserver: 10003 "},
]})
Reconnect nodes to view the master and slave roles:
You can also run the rs. status () command to view the Master/Slave role status.
4. Test Data Replication
Insert data on port 10001 of the master node:
View on replica Node
The discovery cannot be performed on the replica node to view data, which is different from the Master-Slave replication (Master-Slave) architecture.
5. Simulate master-copy Switch
The current master node is a 10001 port Process
Kill the master node process and simulate a master node failure.
[Root @ bakdbserver slave] # kill-9 15940
Log on to a replica Node
You can see that the master node has been converted to copy 3, that is, port 10003.
Login copy 3 Port 10003:
We can see that the data has been synchronized.
5. Restart faulty node 1
Delete data before Node 1
Restart
View Node 1 Status