System: centos6.5
ip:10.19.21.244 Master Node
10.19.21.245 standby Node
10.19.21.248 arbitration Point
Not only the primary node, the replica node, the quorum node, but also the secondary-only, Hidden, Delayed, non-voting.
Secondary-only: cannot be a primary node, only as a secondary replica node, preventing some low performance nodes from becoming the primary node.
Hidden: This kind of node is not able to be made by the client IP reference, can not be set as the primary node, but voting, generally used to back up data.
Delayed: You can specify a time delay to synchronize data from the primary node. Mainly used to back up data, if the real-time synchronization, the accidental deletion of data immediately synchronized to the slave node, recovery and can not recover.
Non-voting: Secondary node without the right to vote, pure backup data node.
Three hosts to install MongoDB
download mongodbwget https://fastdl.mongodb.org/linux/mongodb-linux-x86_ 64-2.6.9.tgz Extract to directory tar xf mongodb-linux-x86_64-2.6.9.tgz -c /usr/local/src change name mv mongodb-linux-x86_64-2.6.9 /usr/local/mongodbcd /usr/local/mongodb/creating the Data directory and log directory mkdir -p Data log Create a user Useradd mongodb -s /sbin/nologin change the directory belongs to the main genus Group chown mongod:mongodb /usr/ Local/mongodb -r Changing the environment variable vim ~/.bash_profilepath= $PATH:/usr/local/mongodb/binsource ~/.bash_ Profile
Config file Vim/etc/mongodb.confport = 27017fork = Truelogpath =/usr/local/mongodb/log/mongodb.logdbpath =/usr/local/mongodb /data/journal = true# data is stored as a directory, and if it already exists, modifying this configuration will cause the original data to disappear (the annotation secondary parameter will come back) unless the existing data file is migrated to the data directory generated by DIRECTORYPERDB. directoryperdb=true# Log Increase form writes logappend=true# configuration replica set, specifying the replica name as the parameter, all hosts must have the same name as the same replica set replset=testrs# Use pre-allocation to ensure stable write performance, pre-allocation is performed in the background, and each pre-allocated file is populated with 0. This allows MongoDB to always keep extra space and free data files, thus avoiding the congestion caused by the allocation of disk space due to the rapid growth of data. Setting noprealloc= True to disable pre-allocated data files can shorten the startup time, but during normal operation it can cause significant performance degradation. Noprealloc=true
Start Service Mongod-f/etc/mongodb.conf
first connect the master node mongo> use admin> conf = {_id: " Testrs ", Members:[{_id:0,host:" 10.19.21.244:27017 ", Priority:2},{_id:1,host:" 10.19.21.245:27017 ", priority:1},{_ Id:2,host: "10.19.21.248:27017", Arbiteronly:true}]}; {"_id" : "Testrs", "Version" : 1, "members" : [{"_id" : 0, "host"  : "10.19.21.244:27017", "Priority" : 2},{"_id" : 1, "host" : " 10.19.21.245:27017 "},{" _id " : 2," "Host" : "10.19.21.248:27017", "Arbiteronly" : True}]} Note that the value of the original _id is the value of the Replset set in our configuration file, Arbiteronly is the uppercase O, the configuration is completed after initialization: > rs.initiate (conf) {"Info"  : "Config now saved locally. should come online in about a minute. "," OK " : 1} This step must be ready for the node and the MONGO of the quorum node to start, otherwise this step cannot be performed.
> rs.status () {"Set" : "Testrs", "Date" : isodate ("2015-05-07t11:55:17z"), "MyState" : 1, "members" : [{"_id" : 0, "name" : "10.19.21.244:27017", "Health" : 1, "state" : 1, "Statestr" : "PRIMARY", "Uptime" : 5737, "Optime" : timestamp (1430995665, 1), "Optimedate" : isodate ("2015-05-07t10:47:45z"), "ElectionTime" : timestamp (1430993992, 1), "Electiondate" : isodate ("2015-05-07t10:19:52z"), "self" : true},{"_id" : 1, "name" : "10.19.21.245:27017", "Health" : 1, "state" : 2, "Statestr" : "secondary", "uptime" : 3077, "Optime" : timestamp ( 1430995665, 1), "Optimedate" : isodate ("2015-05-07t10:47:45z"), "Lastheartbeat" : Isodate ("2015-05-07t11:55:17z"), "Lastheartbeatrecv" : isodate ("2015-05-07t11:55:17z"), "PingMs" : 0, "syncingto" : "10.19.21.244:27017"},{"_id" : 2, "name" : "10.19.21.248:27017", "Health"  : 1, "state" : 7, "Statestr" : "arbiter", "uptime" : 5735, "Lastheartbeat" : isodate ("2015-05-07t11:55:17z"), "Lastheartbeatrecv" : isodate ("2015-05-07t11:55:17z"), "PingMs " : 0}]," OK " : 1}
> Use test to insert data > Db.testdb.insert ({"Tset1": "I ' M Master"})
Then go to the backup node to see if the data has MONGO 10.19.21.245testrs:secondary> use testswitched to DB testtestrs:secondary> show tables; 2015-05-07t19:57:29.670+0800 error: {"$err": "Not Master and Slaveok=false", "Code": 13435} at SRC/MONGO/SHELL/QUERY.J s:131 needs to be configured testrs:secondary> Db.setslaveok () testrs:secondary> show Tables;system.indexestestdbtestrs: Secondary> Db.testdb.find () {"_id": ObjectId ("554a4ee4eb804a0f1b6ec347"), "test1": "I ' M Master"}
Stop the master node and view the entire cluster status Testrs:primary> rs.status () {"Set" : "Testrs", "Date" : isodate (" 2015-05-07t11:59:42z ")," MyState " : 1," members " : [{" _id " : 0," name " : "10.19.21.244:27017", "Health" : 0, "state" : 8, "Statestr" : "(not reachable/ Healthy) "," Uptime " : 0," Optime " : timestamp (1430995665, 1)," Optimedate " : Isodate ("2015-05-07t10:47:45z"), "Lastheartbeat" : isodate ("2015-05-07t11:59:40z"), "lastHeartbeatRecv " : isodate (" 2015-05-07t11:59:32z ")," Pingms " : 0},{" _id " : 1," name " : "10.19.21.245:27017", "Health" : 1, "state" : 1, "Statestr" : "PRIMARY", "uptime" : 3342, "Optime" : timestamp (1430995665, 1), "Optimedate" : isodate (" 2015-05-07t10:47:45z ")," Electiontime " : timestamp (1430999976, 1)," Electiondate " : Isodate ("2015-05-07t11:59:36z")," self " : true},{" _id " : 2," name " : " 10.19.21.248:27017 "," Health " : 1, "state" : 7, "Statestr" : "arbiter", "uptime" : 3340, "Lastheartbeat"  : isodate ("2015-05-07t11:59:42z"), "Lastheartbeatrecv" : isodate ("2015-05-07t11:59:41z"), "PingMs"  : 0}], "OK" : 1}
The master node has been switched over, at which point the standby node has become the master node. Insert one more data Testrs:primary> db.testdb.insert ({"Test2": "I ' M new master"}) testrs:primary> Db.testdb.find (); { "_id" : objectid ("554a4ee4eb804a0f1b6ec347"), "test1" : "I ' M master" }{ "_id" : objectid ("554a505d494f70c600422b41"), "test2" : "I" m new master " }
Restart the original primary node view status Testrs:secondary> rs.status () {"Set" : "Testrs", "Date" : isodate (" 2015-05-07t14:00:33z ")," MyState " : 1," members " : [{" _id " : 0," name " : "10.19.21.244:27017", "Health" : 1, "state" : 1, "Statestr" : "PRIMARY", "uptime" : 15, "Optime" : timestamp (1430995665, 1), "Optimedate" : isodate (" 2015-05-07t10:47:45z ")," Electiontime " : timestamp (1431007222, 1)," Electiondate " : Isodate ("2015-05-07t14:00:22z"), "self" : true},{"_id" : 1, "name" : " 10.19.21.245:27017 "," Health " : 1," state " : 2," Statestr " : " secondary "," uptime " : 13, "Optime" : timestamp (1430995665, 1), "Optimedate" : isodate (" 2015-05-07t10:47:45z ")," Lastheartbeat " : isodate (" 2015-05-07t14:00:32z ")," Lastheartbeatrecv " : isodate ("2015-05-07t14:00:33z"), "Pingms" :&Nbsp;0, "Lastheartbeatmessage" : "syncing to: 10.19.21.244:27017", "Syncingto" : " 10.19.21.244:27017 "},{" _id " : 2," "Name" : "10.19.21.248:27017", "Health" : 1, " State " : 7", "Statestr" : "arbiter", "uptime" : 13, "Lastheartbeat" : Isodate ("2015-05-07t14:00:32z"), "Lastheartbeatrecv" : isodate ("2015-05-07t14:00:33z"), "PingMs" : 0}], "OK" : 1}
See if the data is synced testrs:primary> use testswitched to DB testtestrs:primary> Db.testdb.find () {"_id": ObjectId (" 554a4ee4eb804a0f1b6ec347 ")," test1 ":" I ' M Master "} {" _id ": ObjectId (" 554a505d494f70c600422b41 ")," test2 ":" I ' m new Mas Ter "}
Adding a node to the cluster CFG = rs.conf () cfg.members[3] = {_id:3,host: "10.19.21.246:27017", Priority:0.5}rs.reconfig (CFG) If the backup node cannot read the data , you need to set Db.setslaveok (). If the backup node is readable but not writable, modify the priority CFG = rs.conf () cfg.members[0].priority = 0.5cfg.members[1].priority = 2cfg.members[2].priority = 2rs.reconfig (CFG) requires reconfig configuration to take effect.
PHP connected to MongoDB <?php$options = Array (' replicaset ' = ' testrs ', ' readpreference ' = ' secondarypreferr ') Ed ',); $m = new Mongoclient ("mongodb://10.19.21.244:27017,10.19.21.245:27017/", $options); foreach ($m, GetConnections () as $c) {echo $c [' hash '], ": \ n", "-{$c [' connection '] [' Connection_type_desc ']},", "{$c [' Connec tion ' [' Ping_ms ']} ms\n ";}? >
Primary: The default parameter, only read from the primary node;
Primarypreferred: Most of the data is read from the primary node, and the data is read from the secondary node only when the primary node is unavailable.
Secondary: Only read from the secondary node, the problem is that the data of the secondary node is "old" than the primary node data.
Secondarypreferred: read from the secondary node, and read data from the master node when the secondary node is unavailable;
Nearest: Reads data from the node with the lowest network latency, whether it is the primary node or the secondary node.
This article is from the "Charlie_cen" blog, make sure to keep this source http://charlie928.blog.51cto.com/3741218/1643841
MongoDB Replica Set