Mongodbreplicaset multi-server High Availability Configuration

Source: Internet
Author: User
I have previously written an article on mongodb multi-server configuration, which is in master-slave mode. For details, refer to mongodb master-slave configuration. Master-slave mode, cannot automatically implement failover and recovery. Therefore, we recommend that you use mongodb replicaset to achieve high availability of multiple servers. It seems to me that replicaset comes with h.

I have previously written an article on mongodb multi-server configuration, which is in master-slave mode. For details, refer to mongodb master-slave configuration. Master-slave mode, cannot automatically implement failover and recovery. Therefore, we recommend that you use mongodb's replica set to achieve high availability of multiple servers. It seems to me that the replica set comes with h.

Mongodb multi-server configuration. I have previously written an article about the master-slave mode. For details, refer:Mongodb master-slave Configuration. Master-slave mode, cannot automatically implement failover and recovery. Therefore, we recommend that you use mongodb's replica set to achieve high availability of multiple servers. It seems to me that the replica set comes with the heartbeat function, which is quite powerful.

One or three servers, one master, two slave

Server. 0.0.1: 27017

Server 2:127. 0.0.1: 27018

Server :127. 0.0.1: 27019

1. Create a database directory

[root@localhost ~]# mkdir /var/lib/{mongodb_2,mongodb_3}

Simulate on a machine and use three servers, so the DB directory is separated.

2. Create a configuration file

[Root @ localhost ~] # Cat/etc/mongodb. conf | awk '{if ($0 !~ /^ $/& $0 !~ /^ #/) {Print $0} '// master Server Configuration port = 27017 // listening port fork = true // run pidfilepath =/var/run/mongodb in the background. pid // process PID file logpath =/var/log/mongodb. log // log File dbpath =/var/lib/mongodb // database storage directory journal = true // storage mode nohttpinterface = true // disable httpdirectoryperdb = true // one database folder logappend = true // append mode: log replSet = repmore // cluster name, custom oplogSize = 1000 // oplog size [root @ localhost ~] # Cat/etc/mongodb_2.conf | awk '{if ($0 !~ /^ $/& $0 !~ /^ #/) {Print $0} '// slave server port = 27018 fork = truepidfilepath =/var/run/mongodb/mongodb_2.pidlogpath =/var/log/mongodb/mongodb_2.logdbpath =/var/lib /export db_2journal = truenohttpinterface = truedirectoryperdb = truelogappend = truereplSet = repmoreoplogSize = 1000 [root @ localhost ~] # Cat/etc/mongodb_3.conf | awk '{if ($0 !~ /^ $/& $0 !~ /^ #/) {Print $0} '// slave server port = 27019 fork = truepidfilepath =/var/run/mongodb/mongodb_3.pidlogpath =/var/log/mongodb/mongodb_3.logdbpath =/var/lib /export db_3journal = truenohttpinterface = trueoplogSize = 1000 directoryperdb = truelogappend = truereplSet = repmore

Note that you should not enable the authentication. Otherwise, check the rs. status (); the master and slave servers cannot be connected. "lastHeartbeatMessage": "initial sync couldn't connect to 127.0.0.1: 27017"

3. Start three servers

mongod -f /etc/mongodb.confmongod -f /etc/mongodb_2.confmongod -f /etc/mongodb_3.conf

Note: When the master server is started for the first time, the master server is relatively fast and the slave server is a little slow.

2. 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 the replica set

> rs.initiate(config);{    "info" : "Config now saved locally.  Should come online in about a minute.",    "ok" : 1}

3. view the status of each node in the replica set.

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" : "PRIMARY",            "uptime" : 33,            "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,            "lastHeartbeatMessage" : "syncing to: 127.0.0.1:27018",            "syncingTo" : "127.0.0.1:27018"        }    ],    "ok" : 1}

Note that rs. initiate initialization also takes some time. The rs is just executed. initiate, I will check the status. The stateStr of the slave server is not SECONDARY, but stateStr ":" STARTUP2 ". Wait a moment.

Iii. replica set master, slave Test

1. master server test

repmore:PRIMARY> show dbs;local    1.078125GBrepmore:PRIMARY> use testswitched to db testrepmore:PRIMARY> db.test.insert({'name':'tank','phone':'12345678'});repmore:PRIMARY> db.test.find();{ "_id" : ObjectId("52af64549d2f9e75bc57cda7"), "name" : "tank", "phone" : "12345678" }

2. slave server test

[Root @ localhost mongodb] # mongo 127.0.0.1: 27018 // connect to MongoDB shell version: 2.4.6connecting to: 127.0.0.1: 27018/testrepmore: SECONDARY> show dbs; local 1.078125 GBtest 0.203125 GBrepmore: SECONDARY> db. test. find (); // no permission to view error: {"$ err": "not master and slaveOk = false", "code": 13435} repmore: SECONDARY> rs. slaveOk (); // enable repmore from the slave Database: SECONDARY> db. test. find (); // you can see the data just inserted into the master database from the database {"_ id": ObjectId ("52af64549d2f9e75bc57cda7"), "name": "tank", "phone ": "12345678"} repmore: SECONDARY> db. test. insert ({'name': 'hanging', 'phone': '000000'}); // read-only from the slave database, not master

Here, we have configured the replica set.

Iv. Fault Testing

As I mentioned earlier, mongodb replica set has the Failover function. Next we will simulate this process.

1. Failover

1.1. Shut down the master server

[Root @ localhost mongodb] # ps aux | grep mongod // view all slaves droot 16977 0.2 1.1 3153692 44464? Sl mongod-f/etc/mongodb. confroot 17032 0.2 1.1 3128996 43640? Sl mongod-f/etc/mongodb_2.confroot 17092 0.2 0.9 3127976 38324? Sl mongod-f/etc/mongodb_3.confroot 20400 0.0 0.0 103248 860 pts/2 S + grep mongod [root @ localhost mongodb] # kill 16977 // shut down the master server process [root @ localhost mongodb] # ps aux | grep primary droot 17032 0.2 1.1 3133124 43836? Sl mongod-f/etc/mongodb_2.confroot 17092 0.2 0.9 3127976 38404? Sl mongod-f/etc/mongodb_3.confroot 20488 0.0 0.0 103248 860 pts/2 S + grep mongod

1.2. execute commands in the master database

repmore:PRIMARY> show dbs;Tue Dec 17 04:48:02.392 DBClientCursor::init call() failed

1.3. view the status from the database, for example,

Replica set Fault Test

The previous slave database changed to the master database, and the Failover was successful.

2. fault recovery

mongod -f /etc/mongodb.conf

Start the master server that has just been shut down, and then log on to the master server to check the rs. status (); status has been restored to the original status.

Original article address: mongodb replica set multi-server high availability configuration details, thanks to the original author for sharing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.