MongoDB replication set
What is MongoDB replication set?
Set is a cluster replication mongodb example of a set with the same data set consisting of data synchronization among multiple servers, (almost like MySQL master copy from a concept)
The advantages of a replication set: 1. higher data security, disaster recovery
2. The availability of large data, without downtime maintenance (such as backup, index rebuild, failover)
3. Read zoom (extra copies to read), a copy of the set is transparent to the application program
Works replication set (shown below)
MongoDB replication set composed of a plurality of nodes (at least two nodes), wherein a master node is responsible for handling client requests, the rest are from the node, is responsible for copying data on the primary node
MongoDB generally consists of a master-slave or a master multi-way mix from the master node to the oplog record all the operations, the node periodically polls the master node then acquires these operations, then perform these operations on their own copy of the data, and to ensure that the main consistent data node
Copy the election principles set
There are three types of nodes:
Standard node (host): storage of data , it may be selected as active (primary) node, the right to vote
Passive node (passive): passive node has a complete copy , can not become active node, the right to vote
Arbitration node (arbiter): responsible for the election , do not store data, can not serve as master and slave node
Election rules:
1. Only standard node will be elected as the active node (passive), the right to vote, the passive node has a complete copy, but can not be elected to the active node, the right to vote, the arbitration node does not copy the data, it can not be elected to the active node only the right to vote
2. The difference between the standard and passive nodes: a high value of priority is standard node, the passive nodes are low
3. Election of the last man to win votes high, priority priority is between 0 and 1000, the equivalent of an extra ticket so much
Configuring build MongoDB replication sets
1. Create four instances downloaded and installed MongoDB start (as shown below)
2. Edit the main configuration file
vim /usr/local/mongodb/bin/mongodb1.conf
Note: back replSet value of this parameter will be used to
Other profiles modify port, dbpath, logpath parameters can
3. Copy the initial configuration and log set
alias mongo="/usr/local/mongodb/bin/mongo"》mongo
> cfg={"_id":"wmz","members":[{"_id":0,"host":"192.168.0.102:27017"},{"_id":1,"host":"192.168.0.102:27018"},
{"_id":2,"host":"192.168.0.102:27019"}]}
Added: You can set the node configuration at the time, such as
> cfg={"_id":"wmz","members":[{"_id":0,"host":"192.168.0.102:27017","priority":100},{"_id":1,"host":"192.168.0.102:27018","priority":100},
{"_id":2,"host":"192.168.0.102:27019","priority":0},{"_id":2,"host":"192.168.0.102:27019","arbiterOnly":true}]}
Reset the replication set:> rs.reconfig ()
View the replication set of state elections:> rs.isMaster ()
4. View replica status information: > rs.Status ()
PRIMARY states:
SECONDARY two state
Note: Health is a representative of health , 0 represents downtime , State is a representative of the master node as a 2 from the representative node
5. Manually add nodes:> rs.add ()
Added successfully as shown below:
Delete Node:> rs.remove ()
Deleted successfully as shown below:
The test failure automatically transferred
Stop the run mongodb1
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb1.conf --shutdown
OK 27017 port master node stops
Login 27018 port logs into MongoDB database view status
mongo --port 27018
Found 192.168.0.102:27018 become successful then the master node PRIMARY
Fault simulation can also use this command
rs.freeze(30) //暂停30s不参与选举
rs.stepDown(60,30) //告诉主节点交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步
Unable to read data from node default:> rs.slaveOK ()
Copy management Set
Add Remove node server manually
> rs.add()和> rs.remove()
View the replication status information
> rs.printReolicationInfo()
> rs.printSlaveReplicationInfo()
oplog log
View the log status information:> db.oplog.rs.stats ()
Change the size of oplog:> db.runCommand ()
Certified copy set
verification method:
By auth configuration parameters to open the certification authority, but this authentication method is only suitable for stand-alone node, when we use the replication set how open should the certification authority to ensure the replication set
Single point authentication
vim /usr/local/mongodb/bin/mongodb1.conf
auth=true
keyFile certification
1. Modify the configuration file: vim /usr/local/mongodb/bin/mongodb1.conf
clusterAuthMode=keyFile
keyFile=/usr/local/mongodb/.keyFile
2. Create a file into the content kgcrskey
touch /usr/local/mongodb/.keyFile
3. Configure keyFile file to give permission
The specific content of the file is actually his string, but has set requirements for copying files keyFile
Content: base64-encoded characters written in concentration, i.e., the string can contain az, AZ, +, /
Length must not exceed 1000 bytes
Up to 600 permissions, chmod permissions at least 600 keyFile
openssl rand -base64 102 > /usr/local/mongodb/.keyFile
chmod 600 /usr/local/mongodb/.keyFile
Note: Each node should be carried out this operation