MongoDB Replica Set Configuration

Source: Internet
Author: User

Environment:

192.168.99.129 Master
192.168.99.130 slave1

1. Configuring Replication Sets

Execute separately on both servers

" Rs0 "

To execute the MONGO command, first add a

rs.initiate ({   "rs0",   0"master:27017 " } ]})

and add another one.

Rs.add ("slave1")

Execute Rs.status () to view the status of the replica set node

Rs0:primary>Rs.status () {"Set":"Rs0",    "Date": Isodate ("2017-01-01t05:08:29.623z"),    "MyState":1,    " Term": Numberlong (3),    "Heartbeatintervalmillis": Numberlong ( -),    "Optimes" : {        "Lastcommittedoptime" : {            "TS": Timestamp (1483247303,1),            "T": Numberlong (3)        },        "Appliedoptime" : {            "TS": Timestamp (1483247303,1),            "T": Numberlong (3)        },        "Durableoptime" : {            "TS": Timestamp (1483247303,1),            "T": Numberlong (3)        }    },    " Members" : [        {            "_id":0,            "name":"master:27017",            " Health":1,            " State":1,            "Statestr":"PRIMARY",            "Uptime": -,            "Optime" : {                "TS": Timestamp (1483247303,1),                "T": Numberlong (3)            },            "optimedate": Isodate ("2017-01-01t05:08:23z"),            "InfoMessage":"could not find member to sync from",            "Electiontime": Timestamp (1483247282,1),            "electiondate": Isodate ("2017-01-01t05:08:02z"),            "ConfigVersion":2,            " Self":true        },        {            "_id":1,            "name":"slave1:27017",            " Health":1,            " State":2,            "Statestr":"Secondary",            "Uptime": -,            "Optime" : {                "TS": Timestamp (1483247303,1),                "T": Numberlong (3)            },            "optimedurable" : {                "TS": Timestamp (1483247303,1),                "T": Numberlong (3)            },            "optimedate": Isodate ("2017-01-01t05:08:23z"),            "optimedurabledate": Isodate ("2017-01-01t05:08:23z"),            "lastheartbeat": Isodate ("2017-01-01t05:08:29.007z"),            "Lastheartbeatrecv": Isodate ("2017-01-01t05:08:27.902z"),            "Pingms": Numberlong (0),            "syncingto":"master:27017",            "ConfigVersion":2        }    ],    "OK":1}
2. Verifying Replication Sets

After the primary node inserts data, see if there is the same data from the node, but throw a wrong query from the node when querying the database instructions

Rs0:secondary>Show DBS .- A-31T20: the:59.362-0800E QUERY [main] error:listdatabases failed:{"OK":0,    "errmsg":"Not Master and Slaveok=false",    "Code":13435,    "codename":"Notmasternoslaveok"} :

Since the default from the node is not allowed to read and write, to solve this problem, there are two ways

Executing Rs.slaveok () on the slave node

or execute Db.getmongo () on the master node. Setslaveok ()

Inserting data on the master node

Rs0:primary> Db.student.insert ({"name":"xiaoming"}) Writeresult ({"ninserted":1}) Rs0:primary> Db.student.Find(){ "_id": ObjectId ("58688fa863a36c070316cd69"),"name":"xiaoming"}

Querying from a node

Rs0:secondary> db.student. Find  "_id" : ObjectId ("58688fa863a36c070316cd69""  name""xiaoming" }

MongoDB Replica Set Configuration

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.