MongoDB Replica Set Build

Source: Internet
Author: User

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
$ 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 = 50replSet = Rs0
Third, configure the replica set1. Configure Primary1) Start
$ sudo/opt/mongodb/bin/mongod--config/data/mongodb/conf/db0/mongodb.conf about  to fork child process, waiting Until server is connections.forked Process:48583child process started successfully, parent exiting$ Mongomongod B Shell version:3.0.6connecting to:test> rs.status (); {        "info": "Run Rs.initiate (...) If not yet do for the set ",        " OK ": 0,        " errmsg ":" No Replset config has been received ",        " code ": 94}
2) Initialization
> cfg={_id: "Rs0", members:[{_id:0,host: "172.30.2.201:27017"}]} {"_id": "Rs0", "members": [ {"_id": 0, "host": "172.30.2.201:27017"}]} > rs.initiate (CFG); {"OK": 1}rs0:other> rs.status ();                {"Set": "Rs0", "date": Isodate ("2015-09-25t08:31:36.354z"), "MyState": 1, "members": [                        {"_id": 0, "name": "172.30.2.201:27017",                        "Health": 1, "state": 1, "Statestr": "PRIMARY", "Uptime": "Optime": Timestamp (1443169891, 1), "optimedate": Isodat E ("2015-09-25t08:31:31z"), "Electiontime": Timestamp (1443169891, 2), "elect Iondate ": Isodate (" 2015-09-25t08:31:31z ")," ConfigVersion ": 1, "Self": true}], "OK": 1} 
2. Adding nodes1) Start a new node
$ 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:41794child process started successfully, parent exiting$ Sudo/opt/mongodb/bi N/mongod--config/data/mongodb/conf/db0/mongodb.confabout to fork child process, waiting until server was ready for Connec Tions.forked Process:3761child Process started successfully, parent exiting
2) Add a new node

Execute on Primary:

Rs0:primary> Rs.add ("172.30.2.202:27017"); {"OK": 1}rs0:primary> rs.status ();                {"Set": "Rs0", "date": Isodate ("2015-09-25t08:34:48.161z"), "MyState": 1, "members": [                        {"_id": 0, "name": "172.30.2.201:27017",                        "Health": 1, "state": 1, "Statestr": "PRIMARY", "Uptime": 304, "Optime": Timestamp (1443170060, 1), "optimedate": Isodat E ("2015-09-25t08:34:20z"), "Electiontime": Timestamp (1443169891, 2), "elect Iondate ": Isodate (" 2015-09-25t08:31:31z ")," ConfigVersion ": 2," Self ": TR UE}, {"_id": 1, "name": "172.30.2.202:2701                   7 "," Health ": 1,     "State": 2, "statestr": "Secondary", "uptime": 27,                        "Optime": Timestamp (1443170060, 1), "Optimedate": Isodate ("2015-09-25t08:34:20z"), "Lastheartbeat": Isodate ("2015-09-25t08:34:46.884z"), "LASTHEARTBEATRECV": isodate                ("2015-09-25t08:34:46.896z"), "Pingms": 0, "ConfigVersion": 2 }], "OK": 1}rs0:primary> rs.add ("172.30.2.203:27017"); {"OK": 1}rs0:primary> rs.status ();                {"Set": "Rs0", "date": Isodate ("2015-09-25t08:36:22.579z"), "MyState": 1, "members": [                        {"_id": 0, "name": "172.30.2.201:27017",                        "Health": 1, "state": 1, "Statestr": "PRIMARY",           "Uptime": 398,             "Optime": Timestamp (1443170158, 1), "Optimedate": Isodate ("2015-09-25t08:35:58z"), "Electiontime": Timestamp (1443169891, 2), "Electiondate": Isodate ("2015-09-                25t08:31:31z ")," ConfigVersion ": 3," Self ": true}, {"_id": 1, "name": "172.30.2.202:27017", " Health ": 1," state ": 2," statestr ":" Secondary "," Uptime ": 121," Optime ": Timestamp (1443170158, 1)," Optimedate ": Isodate ("                        2015-09-25t08:35:58z ")," Lastheartbeat ": Isodate (" 2015-09-25t08:36:22.268z "),                        "Lastheartbeatrecv": Isodate ("2015-09-25t08:36:20.949z"), "Pingms": 0, "Syncingto": "172.30.2."201:27017 "," ConfigVersion ": 3}, {" _id ": 2 , "name": "172.30.2.203:27017", "Health": 1, "state ": 2," statestr ":" Secondary "," uptime ":", "Opti                        Me ": Timestamp (1443170158, 1)," Optimedate ": Isodate (" 2015-09-25t08:35:58z "), "Lastheartbeat": Isodate ("2015-09-25t08:36:22.267z"), "Lastheartbeatrecv": Isodate ("2015-09-2        5t08:36:22.275z ")," Pingms ": 0," ConfigVersion ": 3} ], "OK": 1}
Iv. Verification1. Data synchronization1) Primary:
$ mongomongodb Shell version:3.0.6connecting to:testrs0:primary> use aaa;switched to DB aaars0:primary> Db.createC Ollection ("test"); {"OK": 1}rs0:primary> show Collections;system.indexestest


2) Secondary:
$ mongomongodb Shell version:3.0.6connecting to:testrs0:secondary> rs.slaveok ();rs0:secondary> use aaa;switched To DB aaars0:secondary> show collections;system.indexestest$ mongomongodb Shell version:3.0.6connecting To:testrs0: Secondary> Rs.slaveok ();rs0:secondary> use aaa;switched to DB aaars0:secondary> show collections; System.indexestest
2. Read/write Verification

From the above, primary is readable and writable.

Write the test on secondary:

$ 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>

Prove that the secondary node is not writable, is unreadable by default, needs to execute Rs.slaveok (), or Db.getmongo (). Setslaveok (), and is valid only for the current session, so each connection from the library needs to be executed.


V. Description

MongoDB's replica set requires at least more than 3 units to be highly available, and the number of nodes is preferably cardinality.


MongoDB Replica Set Build

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.