Implementation of the MongoDB replica set

Source: Internet
Author: User

Replication set (Replica sets) is a master/slave replication mechanism for asynchronous synchronization of the same data, so that multiple machines have more than one copy of the same data, due to the automatic failover and recovery features, When the main library is down and no user intervention is required to automatically switch to the master library on other backup servers, a cluster can support up to 7 servers, and any node can be the primary node. All writes are distributed to the master node, and the read operation can be performed on any node, which allows for the separation of reads and writes and increases the load.

Limited resources Testing a VM opens 3 instances:

Environment: centos7.0

192.168.1.21:20011 P

192.168.1.21:20012 S

192.168.1.21:20013 A

MongoDB verson:3.2

Construction process:

1, download the package, and unzip

2. Add Related directory Files

[email protected] mongodb27017]# LL

Total Dosage 8

Drwxr-xr-x. 2 root root 4096 December 00:59 bin

Drwxr-xr-x. 2 root root 30 December 10:47 conf

Drwxr-xr-x 4 root root 4096 December 11:19 data

Drwxr-xr-x. 2 root root 20 December 01:13 key

Drwxr-xr-x. 2 root root 23 December 10:20 logs

#3个实例配置一样

3. Unpack all files under the Bin directory under the package to CP to the current created bin directory

4, 3 instances of the configuration file consistent


[email protected] conf]# cat mongodb27017.conf

port=20011# per instance port, according to the actual configuration

Dbpath=/export/mongodb27017/data #数据目录

Logpath=/export/mongodb27017/logs/mongod.log #日志目录

Fork=true

Logappend=true

#keyFile =/export/mongodb27017/key/mongodb# is introduced later

Nohttpinterface=true

Replset=mongodb # #复制集的名字,

5. Start 3 instances:


/export/mongodb27018/bin/mongod-f/export/mongodb27018/conf/mongodb27018.conf

/export/mongodb27019/bin/mongod-f/export/mongodb27019/conf/mongodb27019.conf

/export/mongodb27017/bin/mongod-f/export/mongodb27017/conf/mongodb27017.conf

#需要把解压包bin目录下文件分别拷贝到这3个启动目录下, or one as Basedir can also, other separate

6. Configuring Replication Sets

/export/mongodb27017/bin/mongo--port 20011# into the database according to his own actual definition

1, configure the relationship of Members:

config= {_id: "MongoDB", Members:[{_id:0,host: "192.168.1.21:20011", Priority:3}]} (gave priority to 3,mongodb as the name of the replica set)

2. Initialize:

>rs.initiate (config);

3. Add standby node, quorum node


Rs.add ("192.168.1.21:20012") Add a spare node

Rs.addarb ("192.168.1.21:20013") Add quorum node

Rs.status () #查看状态

Rs.conf () #查看副本集成员及其优先级 (optimistic about the order of members, priority from 1-100, the higher the priority level)


Config=rs.conf () for priority setting

Config.members[0].priority = 3 Set Primary node priority to 3

config.members[1].priority = 2 Set Standby node priority is 2

Config.members[2].priority = 1 Sets the quorum node priority to 1

Rs.remove ("Ip:port") removes a member that already exists

Rs.reconfig (config) Save configuration, take effect

7, set the standby node as slave

Mongodb:secondary> Show DBS

2016-12-17t12:26:22.621+0800 E QUERY [thread1] error:listdatabases failed:{"OK": 0, "errmsg": "Not Master and slave Ok=false "," Code ": 13435}:

Log on to 20012 instance execution: Rs.slaveok ()

8, test the primary node is able to switch the outage

Summarize:

MongoDB Master-Slave mode is actually a single copy of the application, not very good extensibility and fault tolerance. The replica set has multiple replicas to ensure fault tolerance, even if a copy hangs up there are many replicas exist, and solve the first problem above "the primary node hangs, the entire cluster will automatically switch", if the node is hung, it will re-elect the new master node. For ordinary business can be satisfied, but high concurrency, replication set is not satisfied, it needs to Shard, to achieve high availability of clusters, can rely on LVS or other.





This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1883560

Implementation of the MongoDB 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.