MongoDB multiple server configuration, previously wrote an article, is Master-slave mode, please refer to: detailed MongoDB master configuration. Master-slave mode, failover and recovery are not automatically implemented. Therefore, we recommend that you use the replica set of MongoDB to achieve high availability of multiple servers. Give me the feeling is replica set seems to bring the heartbeat function, quite powerful.
One, three servers, 1 main, 2 from
Server 1:127.0.0.1:27017
Server 2:127.0.0.1:27018
Server 3:127.0.0.1:27019
1, create the database directory
[Root@localhost ~]# Mkdir/var/lib/{mongodb_2,mongodb_3}
In a computer simulation, three servers, so the DB directory separated.
2, create the configuration file
[Root@localhost ~]# cat/etc/mongodb.conf |awk ' {if ($!~/^$/&& $!~/^#/) {print $}} '///primary server configuration port = 27017 Listening Port fork = TRUE//background run Pidfilepath =/var/run/mongodb/mongodb.pid//process pid file LogPath =/var/log/mongodb/mongodb.l OG//log file DBPath =/var/lib/mongodb//db storage Directory journal = TRUE//Storage mode Nohttpinterface = TRUE//disable HTTP Directorype Rdb=true//A database A folder Logappend=true//Append Way Write Log replset=repmore//cluster name, custom oplogsize=1000//oplog size [R Oot@localhost ~]# cat/etc/mongodb_2.conf |awk ' {if ($!~/^$/&& $!~/^#/) {print $}} '/from server Port = 27018 F Ork = True Pidfilepath =/var/run/mongodb/mongodb_2.pid LogPath =/var/log/mongodb/mongodb_2.log DBPath =/var/lib/mongo db_2 Journal = True nohttpinterface = True directoryperdb=true logappend=true replset=repmore oplogsize=1000 [roo T@localhost ~]# cat/etc/mongodb_3.conf |awk ' {if ($!~/^$/&& $!~/^#/) {print $}} '//from server port = 27019 for K = True Pidfilepath =/var/run/mongodb/mongodb_3.pid LogPath =/var/log/mongodb/mongodb_3.log dbpath =/var/lib/mongodb_3 Journal = True Noht
Tpinterface = True oplogsize = 1000 directoryperdb=true logappend=true replset=repmore
Here to pay attention to, do not turn the certification up, or view rs.status (), when the master and slave server, can not connect, "Lastheartbeatmessage": "Initial sync couldn ' t connect to 127.0.0.1:27017 "
3, start three servers
Mongod-f/etc/mongodb.conf
mongod-f/etc/mongodb_2.conf
mongod-f/etc/mongodb_3.conf
Note: When the initial boot, the primary server is relatively fast, a bit slow from the server.
Second, configure and initialize the replica set
1, configure the replica set node
> config = {_id: "Repmore", Members:[{_id:0,host: ' 127.0.0.1:27017 ', priority:2},{_id:1,host: ' 127.0.0.1:27018 ', Priority:1},{_id:2,host: ' 127.0.0.1:27019 ', Priority:1}]}
2, initialize replica set
> rs.initiate (config);
{
"info": "Config now saved locally." Should come online in about a minute. ",
" OK ": 1
}
3, view replica set each node state
Repmore:primary> Rs.status (); {"Set": "Repmore", "date": Isodate ("2013-12-16t21:01:51z"), "MyState": 2, "syncingto": "127.0.0.1:27017", " Members ': [{"_id": 0, "name": "127.0.0.1:27017", "Health": 1, "state": 1, "Statestr": "PRIM
ARY "," Uptime ":" Optime ": Timestamp (1387227638, 1)," Optimedate ": Isodate (" 2013-12-16t21:00:38z "), "Lastheartbeat": Isodate ("2013-12-16t21:01:50z"), "Lastheartbeatrecv": Isodate ("2013-12-16t21:01:50z"), "PingMs"
: 0, "syncingto": "127.0.0.1:27018"}, {"_id": 1, "name": "127.0.0.1:27018", "Health": 1, "State": 2, "statestr": "Secondary", "uptime": 1808, "Optime": Timestamp (1387227638, 1), "Optimedate" : Isodate ("2013-12-16t21:00:38z"), "errmsg": "Syncing to:127.0.0.1:27017", "Self": true}, {"_id": 2, "name": "127.0.0.1:27019", "Health": 1, "state": 2, "statestr": "Secondary", "uptime": 1806," Optime ": Timestamp (1387227638, 1)," Optimedate ": Isodate (" 2013-12-16t21:00:38z ")," Lastheartbeat " : Isodate ("2013-12-16t21:01:50z"), "Lastheartbeatrecv": Isodate ("2013-12-16t21:01:51z"), "Pingms": 0, "last
Heartbeatmessage ":" Syncing to:127.0.0.1:27018 "," syncingto ":" 127.0.0.1:27018 "}]," OK ": 1}
Here to note that Rs.initiate initialization is also a certain amount of time, just finished rs.initiate, I will check the status, from the server statestr is not secondary, but Statestr ":" STARTUP2 ", and so on."
Three, replica set master, from the test
1, master server test
Repmore:primary> show DBS;
Local 1.078125GB
repmore:primary> with test
switched to DB test
repmore:primary> db.test.insert ({' Name ': ' Tank ', ' phone ': ' 12345678 '};
Repmore:primary> Db.test.find ();
2, testing from the server
[Root@localhost mongodb]# MONGO 127.0.0.1:27018//connection
MongoDB shell version:2.4.6
connecting to: 127.0.0.1:27018/test
repmore:secondary> show dbs;
Local 1.078125GB
test 0.203125GB
repmore:secondary> db.test.find (); No permission
to view error: {"$err": "Not Master and Slaveok=false", "Code": 13435}
repmore:secondary> Rs.slaveok ();
//from the library to open
repmore:secondary> db.test.find (); From the library, you can see the data that the main library just inserted
{"_id": ObjectId ("52af64549d2f9e75bc57cda7"), "name": "Tank", "Phone": "12345678"}
Repmore:secondary> Db.test.insert ({' name ': ' zhangying ', ' phone ': ' 12345678 '}); Read-only from library, no insert permission not
Master
Here, our replica set is configured.
Four, the fault test
As I said earlier, the MongoDB replica set has failover capabilities, and the following simulates the process
1, failover
1.1, turn off the primary server
[Root@localhost mongodb]# PS aux |grep mongod//View all Mongod root 16977 0.2 the 1.1 3153692 ? Sl 04:31 0:02 mongod-f/etc/mongodb.conf
Root 17032 0.2 1.1 3128996 ? Sl 04:31 0:02 mongod-f/etc/mongodb_2.conf
root 17092 0.2 0.9 3127976 ? Sl 04:31 0:02 mongod-f/etc/mongodb_3.conf root 20400 0.0 0.0 103248 860 PTS/2 s+ 04:47 0:00 grep mongod
[ Root@localhost mongodb]# Kill 16977//Shut down the primary server process
[root@localhost mongodb]# PS aux |grep-mongod
root 17032 0.2 1.1 3133124 43836? Sl 04:31 0:02 mongod-f/etc/mongodb_2.conf
root 17092 0.2 0.9 3127976 ? Sl 04:31 0:02 mongod-f/etc/mongodb_3.conf root 20488 0.0 0.0 103248 860 PTS/2 s+ 04:47 0:00 grep mongod
1.2, executing commands in the main library
Repmore:primary> show DBS;
Tue Dec 04:48:02.392 Dbclientcursor::init call () failed
1.3, view the status from the library, as shown below,
Replica set Failure test
The previous change from the library to the main library, failover success
2, failure recovery
Mongod-f/etc/mongodb.conf
Start the primary server that has just been shut down, and then log on to the primary server to view the status Rs.status (), which has been restored to its original state.