MongoDB Master-Slave cluster

Source: Internet
Author: User
Tags connection reset

First, the environment
$ cat/etc/redhat-release CentOS Linux release 7.0.1406 (Core) $ uname-alinux zhaopin-2-201 3.10.0-123.el7.x86_64 #1 SMP Mon June 12:09:22 UTC x86_64 x86_64 x86_64 gnu/linux$ MONGO--versionmongodb Shell version:3.0.6
Second, prepare1. Create a Directory
$ sudo mkdir-p/data/mongodb/{data/db0,backup/db0,log/db0,conf/db0}


2. Writing the configuration file 1) Master:
$ sudo vim/data/mongodb/conf/db0/mongodb.conf# baseport = 27017maxConns = Filepermissions = 0700fork = Truenoauth = t Ruedbpath =/data/mongodb/data/db0pidfilepath =/data/mongodb/data/db0/mongodb.pidjournal = true# Securitynohttpinterface = Truerest = false# Log logpath =/data/mongodb/log/db0/mongodb.loglogrotate = Renamelogappend = t Rueslowms = 50master = True

2) Slave:

$ sudo vim/data/mongodb/conf/db0/mongodb.conf# baseport = 27017maxConns = 800filePermissions = 0700fork = Truenoauth = tr Uedbpath =/data/mongodb/data/db0pidfilepath =/data/mongodb/data/db0/mongodb.pidjournal = true# Securitynohttpinterface = Truerest = false# Loglogpath =/data/mongodb/log/db0/mongodb.loglogrotate = Renamelogappend = t rueslowms = 50slave = Truesource = 172.30.2.201:27017
Third, start1. Start Master First
$ sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until Ser Ver is ready for connections.forked Process:48583child process started successfully, parent exiting$ Mongomongodb Shell V Ersion:3.0.6connecting to:test> Db.ismaster (); {        "IsMaster": 0,        "info": "Dead:data too stale halted replication",        "maxbsonobjectsize": 16777216,        "Ma Xmessagesizebytes ": 48000000,        " maxwritebatchsize ": +,        " localtime ": Isodate (" 2015-09-23t07:47:52.957z ") ,        "Maxwireversion": 3,        "Minwireversion": 0,        "OK": 1}>bye
2. Start slave
$ sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until Ser Ver is ready for connections.forked Process:36715child process started successfully, parent exiting$ Mongomongodb Shell V Ersion:3.0.6connecting to:test> Db.ismaster (); {        "IsMaster": false,        "Maxbsonobjectsize": 16777216,        "maxmessagesizebytes": 48000000,        " Maxwritebatchsize ": +,        " localtime ": Isodate (" 2015-09-23t07:49:32.934z "),        " Maxwireversion ": 3,        " Minwireversion ": 0,        " OK ": 1}>bye
Iv. Verification1. Data synchronization1) Master:
$ mongomongodb Shell version:3.0.6connecting to:test> use aaa;switched to DB aaa> db.createcollection ("test"); {"OK": 1}> show collections;system.indexessystem.profiletest>
2) Slave:
$ mongomongodb Shell version:3.0.6connecting to:test> rs.slaveok ();> show dbs;aaa    0.078GBlocal  0.078gb& Gt Use aaa;switched to DB aaa> show collections;system.indexestest>
2. Read/write Verification

From the above, master is readable and writable.

To write tests from a library:

$ mongomongodb Shell version:3.0.6connecting to:test> use aaa;switched to DB aaa> db.createcollection ("test2"); {"Note": "From ExecCommand", "OK": 0, "errmsg": "Not Master"}> show collections;2015-09-23t15:57:49.240+0800 E QUE RY    Error:listcollections failed: {"note": "From ExecCommand", "OK": 0, "errmsg": ' Not Master '} at    Error (< ;anonymous>) at    Db._getcollectioninfoscommand (src/mongo/shell/db.js:646:15) at    Db.getcollectioninfos ( SRC/MONGO/SHELL/DB.JS:658:20) at    db.getcollectionnames (src/mongo/shell/db.js:669:17) at    shellHelper.show (src/mongo/shell/utils.js:625:12)    At Shellhelper (src/mongo/shell/utils.js:524:36) @    (SHELLHELP2): 1:1 at src/mongo/shell/db.js:646> Rs.slaveok ();> Show collections;system.indexestest>

Proof from the library is not writable, default is unreadable, need to execute rs.slaveok (), or Db.getmongo (). Setslaveok (), and only valid for the current session, so each connection from the library needs to be executed.

Five, fault testing and solutions1.master Downtime1) shut down the master
$ mongomongodb Shell version:3.0.6connecting to:test> use admin;switched to DB admin> db.shutdownserver (); 2015-09- 23t16:02:16.983+0800 I NETWORK  dbclientcursor::init call () Failedserver should is down ... 2015-09-23t16:02:16.986+0800 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23t16:0 2:16.987+0800 I network  reconnect 127.0.0.1:27017 (127.0.0.1) ok2015-09-23t16:02:16.992+0800 I network  Socket Recv () errno:104 Connection reset by peer 127.0.0.1:270172015-09-23t16:02:16.992+0800 I NETWORK  socketexception: remote:127.0.0.1:27017 error:9001 Socket exception [Recv_error] Server [127.0.0.1:27017]2015-09-23t16:02:16.992+0800 I NETWORK  dbclientcursor::init call () failed>bye$ PS aux | grep mongo | grep-v grep
2) View slave status
$ mongomongodb Shell version:3.0.6connecting to:test> rs.slaveok ();> show dbs;aaa    0.078GBlocal  0.078gb& Gt Use aaa;switched to DB aaa> show collections;system.indexestest> db.createcollection ("test2"); {"Note": "From ExecCommand", "OK": 0, "errmsg": "Not Master"}

Visible slave is still read-only and cannot be written

3) Workaround

Turn slave off and start after master

> Use admin;switched to DB admin> db.shutdownserver (); 2015-09-23t16:05:28.355+0800 I NETWORK Dbclientcursor::init Call () Failedserver should is down ... 2015-09-23t16:05:28.357+0800 I NETWORK Trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23t16:05:28.357+ 0800 W NETWORK Failed to connect to 127.0.0.1:27017, reason:errno:111 Connection refused2015-09-23t16:05:28.358+0800 I N Etwork Reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn ' t connect to server 127.0.0.1:27017 (127.0.0.1), Connec tion attempt failed>bye$ ps aux | grep MONGO | Grep-v grep$ sudo vim/data/mongodb/conf/db0/mongodb.confmaster = True#slave = True#source = 172.30.2.201:27017$ sudo/op T/mongodb/bin/mongod--config/data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server is REA DY for connections.forked Process:36988child process started successfully, parent exiting$ mongomongodb Shell Version:3. 0.6connecting to:test> show Dbs;aaa 0.078GBlocal 50.054Gb> use aaa;switched to DB aaa> db.createcollection ("test2"); {"OK": 1}> Show collections;system.indexessystem.profiletesttest2> db.ismaster (); {"IsMaster": True, "maxbsonobjectsize": 16777216, "maxmessagesizebytes": 48000000, "Maxwri Tebatchsize ": +," localtime ": Isodate (" 2015-09-23t08:07:41.812z ")," Maxwireversion ": 3," minwir Eversion ": 0," OK ": 1}>bye

So the slave will become the new master.

2.slave Downtime1) Make a new slave
$ sudo rm-fr/data/mongodb/data/db0/*$ sudo vim/data/mongodb/conf/db0/mongodb.conf#master = Trueslave = Truesource = 172 .30.2.202:27017$ Sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/db0/mongodb.confabout to fork child process, Waiting until server is a ready for connections.forked Process:48779child process started successfully, parent exiting$ Mon Gomongodb Shell version:3.0.6connecting to:test> db.ismaster (); {        "IsMaster": false,        "Maxbsonobjectsize": 16777216,        "maxmessagesizebytes": 48000000,        " Maxwritebatchsize ": +,        " localtime ": Isodate (" 2015-09-23t08:14:51.283z "),        " Maxwireversion ": 3,        " Minwireversion ": 0,        " OK ": 1}
2) Stop the new slave
> Use admin;switched to DB admin> db.shutdownserver (); 2015-09-23t16:15:31.329+0800 I NETWORK  dbclientcursor: : Init call () Failedserver should is down ... 2015-09-23t16:15:31.331+0800 I NETWORK  trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed2015-09-23t16 : 15:31.332+0800 I network  reconnect 127.0.0.1:27017 (127.0.0.1) ok2015-09-23t16:15:31.333+0800 I network  Socket recv () errno:104 Connection reset by peer 127.0.0.1:270172015-09-23t16:15:31.333+0800 I NETWORK  socketexception:remote:127.0.0.1:27017 error:9001 Socket exception [Recv_error] server [127.0.0.1:27017] 2015-09-23t16:15:31.333+0800 I NETWORK  dbclientcursor::init call () failed>bye$ PS aux | grep mongo | grep-v grep
3) View new master Status
$ mongomongodb Shell version:3.0.6connecting to:test> show dbs;aaa     0.078GBlocal  50.054gb> use AAA; Switched to DB aaa> db.createcollection ("test3"); {"OK": 1}> show Collections;system.indexessystem.profiletesttest2test3>bye
4) Conclusion

Visible master can read and write, only need to make from the library


MongoDB Master-Slave cluster

Related Article

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.