MongoDB 2.3 copy (replica set)

2.3, copy (replica set)

What is the Copy

  • Copy provides a redundant backup data and store copies of data on multiple servers and improve data availability and data security can be guaranteed
  • Copy also allows restoring data from hardware failures and service interruptions in

Why do you want to copy

  • data backup
  • Data Disaster Recovery
  • Separate read and write
  • High (24 * 7) Data Availability
  • No maintenance downtime
  • Replica set is transparent to applications

Copy works

  • Replication requires at least two nodes A, B ...
  • A master node is responsible for handling client requests
  • The rest are from the node, responsible for copying the data on the primary node
  • Common with the way nodes: a master-slave, a master multi-slave
  • All operations master node recorded thereon, obtained from the master node periodically polls these operations, then perform these operations on their own copy of the data to ensure data consistency from the master node node
  • The master node performs data interaction with the consistency of data from node to protect

Copy features

  • N cluster nodes
  • Any node as a master node can be
  • All write operations in the primary site
  • Automatic failover
  • Automatic recovery

Setting copy nodes

  • Next Steps need to open multiple terminal windows, and may connect multiple hosts ubuntu, would be a bit messy, it is recommended to achieve in the xshell
  • step1: Create a database directory t1, t2
  • In the Desktop directory demonstration, other directories can also pay attention to rights
mkdir t1
mkdir t2
  • step2: Use the following format to start mongod, note the name replSet is consistent
mongod --bind_ip 192.168.196.128 --port 27017 --dbpath ~/Desktop/t1 --replSet rs0
mongod --bind_ip 192.168.196.128 --port 27018 --dbpath ~/Desktop/t2 --replSet rs0
  • step3: connect the main server, where the server provided based 192.168.196.128:27017
mongo --host 192.168.196.128 --port 27017
  • step4: Initialization
rs.initiate()
  • After initialization, the prompt as shown below:

initialization

  • step5: View current status
rs.status()
  • Below the current status:

initialization

  • step6: add a replica set
rs.add('192.168.196.128:27018')
  • STEP7: adding replica set successfully, the current status as shown below:

initialization

  • step8: Connect the second mongo Service
mongo --host 192.168.196.128 --port 27018
  • After successful connection, the prompt as shown below:

initialization

  • step9: inserting data into the master server
use test1
for(i=0;i<10;i++){db.t1.insert({_id:i})}
db.t1.find()
  • step10: In the insert query from the server
  • Note: If the read operation from the server needs to be set rs.slaveOk ()
rs.slaveOk()
db.t1.find()

other instructions

  • Deleted from the node
rs.remove('192.168.196.128:27018')
  • After turning off the main server, and then restart, you will find the original from the server becomes from the server, the server newly started (originally from the server) goes from the server

Guess you like

Origin www.cnblogs.com/LiuYanYGZ/p/12241752.html