MongoDB 3.6 (v) building a replica set

Source: Internet
Author: User
Tags mongodb hosting

An overview

A replica set is a set of Mongod instances that maintain the same set of data. The replica set contains multiple data hosting nodes and an optional quorum node. In a data hosting node, only one member is considered the primary node, while the other node is considered the secondary node.
The primary device accepts all write operations, and the device can accept read operations but needs to set Rs.slaveok (). All changes to the data set of the host will be recorded in the Oplog.


Copy the Oplog log on the master device from the machine and perform the appropriate actions to ensure that the data and host are eventually consistent

The host and other members of the replica set are not contacted for more than 10 seconds (heartbeat detection), and an elected slave will hold an election and elect itself as the new master device. The first place to hold an election and get a majority vote from the device to the main device

Two sets of replicas

MongoDB recommends a replica set of three nodes or more. The following are configured on a single computer.

1. Create the configuration file. Config1.conf,config.conf, config2.conf
dbpath=logpath=port=bind_ip=replSet=
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • Replset: Replica set name that identifies a replica set. Same replset in the same cluster
    • Port: Port number, default 27017
    • BIND_IP: Default 127.0.0.1
    • DBPath: Database File storage location
    • LogPath: Where log files are stored
2. Start the Mongod server

Start a Mongod instance with a configuration file,-F Conpath or –config Conpath, and start three servers in turn based on the three configuration files

mongod -f D:\mongodb\config.confmongod --config D:\mongodb\confif.conf
    • 1
    • 2
    • 3
3. Client Connection
mongo --port 27000
    • 1
    • 2
4. Initializing a replica set

Each node in the replica set needs to perceive each other. Initializing a replica set in a client window that has a connection established

config={"_id": repSetName,        "members":[            {"_id": num,"host":"serverHost:port"},{}        ]    }rs.initiate(config)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • Members: The information for all the Mongod member in the replica set.
    • _ID: The identity of the replica set, corresponding to the Replset name in the configuration file.

After initializing the replica set, the operation will find that the prefix has changed. Replsetname:secondary indicates that the connected server has been selected as the primary device or Replsetname:promary connected server is from the device
MongoDB will use the default algorithm to elect the main device, the other is from the device. MongoDB supports up to 7 members who can participate in an election

5. Some operations of the replica set
rs.status()   //查看成员的运行状态等信息rs.config()    //查看配置信息rs.slaveOk()  //允许在SECONDARY节点上进行查询操作,默认从节点不具有查询功能rs.isMaster()  //查询该节点是否是主节点rs.add({})   //添加新的节点到该副本集中rs.remove()   //从副本集中删除节点
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

More commands can be queried using the RS.HELP ()

Three replica sets Add nodes

Now we have the original three servers based on add one more.

1. Find the main device and connect
rs.isMaster()
    • 1
    • 2


From a device that does not have add member permissions, you should first look for the main device, from which you can see that the main device is 127.0.0.1:27022.

mongo --port 27022   //连接主设备
    • 1
    • 2
2. Add Users
rs.add("127.0.0.1:27033") //添加了一个使用27033端口的服务器
    • 1
    • 2

3. Start the server
mongod -f D:\mongodb\config3.conf //改配置文件和其他的基本一致
    • 1
    • 2
4. Connect to the server
mongo --port 27033

MongoDB 3.6 (v) building a replica set

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.