MongoDB Build Replset Replication cluster

Source: Internet
Author: User
Tags mongodb readable

MongoDB replica set is a master-slave replication mode with failover cluster, any member may be master ,
When Master hangs up, it will quickly re-elect a node to act as master.

Constituent primary members of a replication set

    • Primary
      数据读写 master节点
    • Secondary
      备份Primary的数据 默认设置下 不可读 不可写
    • Arbiter
      投票节点 此节点不会存数据 只参与投票 ,当primary节点出现异常挂掉之后 arbiter节点负责从secondary 节点中选举一个节点升级为Primary节点

which can be set secondary node readable, so that the primary node is responsible for writing, which enables an efficient and simple read-write separation.

Environment construction

3 examples to illustrate the construction process of the replication cluster are: 127.0.0.1:12345,127.0.0.1:12346,127.0.0.1:12347.
You need to pre-create the folders in each directory before you start the error, there is a pit is the Pidfilepath configuration item must be an absolute path,
Otherwise, the Replset will also have a consistent name in the same replication set.

Note In the old version is the use of the master slave mode is currently used by the 3.4 official does not support the use of this way,
Officials want to use Replset instead of Master slave. So when you configure Master or slave, you will get an error.

127.0.0.1:12345 Configuration

Port=12345fork=truedbpath=data/12345logpath=log/12345/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12345/12345.pidreplset=mydbcenteroplogsize =512 127.0.0.1:12346 Configuration
Port=12346fork=truedbpath=data/12346logpath=log/12346/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12346/12346.pidreplset=mydbcenteroplogsize =512
127.0.0.1:12347 Configuration
Port=12347fork=truedbpath=data/12347logpath=log/12347/mongod.loghttpinterface=truerest=truelogappend= Truepidfilepath=/home/collect/mongodb-linux-x86_64-rhel70-3.4.1/log/12347/12347.pidreplset=mydbcenteroplogsize =512

After starting each of the 3 instances, go to an instance

Initializing a replication cluster first create 1 configuration objects in JS is a simple object, JSON string
  1. var rs_conf={
  2. " _id": "Mydbcenter",
  3. "Members": [
  4. {
  5. "_id": 0,
  6. "host": "127.0.0.1:12345"
  7. },
  8. {
  9. "_id": 1,
  10. "host": "127.0.0.1:12346"
  11. },
  12. {
  13. "_id": 2,
  14. "host": "127.0.0.1:12347"
  15. }
  16. ]
  17. }
Apply the configuration to the cluster
rs.initiate(rs_conf)

One limitation here is that the nodes in the cluster need not have the data to be emptied first or initiate the error will occur.
After a successful configuration rs.status() , use the command to view the status of each node, some normal can see the status information of each node

rs.status()

After the replication cluster is set up, the command line identifier becomes the appropriate member type, as

    1. mydbcenter:P rimary>
    2. mydbcenter:secondary>

This is also a small sign of success in verifying that the cluster is built.

You can then try writing a piece of data in the primary. This data is immediately synchronized to each secondary node.
Of course, it is said that the default secondary is not readable and will report the following error.

    1. {
    2. "OK": 0,
    3. "errmsg": "not Master and Slaveok=false",
    4. "code": 13435,
    5. " codename": "Notmasternoslaveok "
    6. }

So we need to do it in secondary.

db.getMongo().setSlaveOk()

There are a lot of postings on the web saying that just perform db.getmongo () in the primary node. Setslaveok ()
Can read the data in the secondary node, but it is not possible to try it in version 3.4. Need to be executed in secondary.
db.getMongo().setSlaveOk()Let the secondary be readable.

Adding nodes

If you have a good one replication cluster now, the boss would like to add a backup machine to go in?
Just start the instance of the new machine and call the Rs.add () method in primary to

rs.add({"host" : "127.0.0.1:12348"})

Add a polling node to invoke the rs.addArb() method.

Reprint to: Smile Pine Station http://blog.seoui.com/2017/01/11/mongodb-rs/

MongoDB Build Replset Replication 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.