MongoDB Series (ii): MongoDB replica set

Source: Internet
Author: User

1. What is Replica set

A replica set is a cluster of MongoDB replicas.

The same-time principle is that the write operation occurs in the main library, which synchronizes the Oplog log from the main library.

There is no specific main library in the cluster, the main library is elected, and if the main library is down, a main library will be elected.

MongoDB can also be configured as Master-slave mode, however, it is not recommended to use master-slave mode, the alternative is to adopt the replica set mode.

Replica sets have the following characteristics:

1. The minimum composition is: Primary,secondary,arbiter, General deployment is: primary,2 secondary.

2. The number of members should be odd, and if the addition of arbiter,arbiter for even cases does not save data, only votes.

3. Max members, but only 7 voting members, others are non-voting members.

2. Configure the replica set

In this experiment, a replica set of 1 Master 2 is configured.

test166:27017,test167:27017,test167:27018

2.1 Creating a replica set

Start MongoDB on 3 servers, specifying the replica set name is Rs0

Start the first MongoDB on a test166

# Mongod--dbpath/var/lib/mongo--replset Rs0

Start a second MongoDB on test167

# Mongod--dbpath/var/lib/mongo--replset Rs0

The third MongoDB is started on the test167 and the specified port is 27018

# mkdir-p/data/mongo
# mongod--dbpath/data/mongo--port 27018--replset rs0

You can also specify it in the settings file, and then start

# vi/etc/mongod.conf

replication:
  replsetname:rs0

# Systemctl Start Mongod
2.2 Configuring Replica Sets

Connect MongoDB on any of the platforms, initialize

# MONGO
> Use admin
> rs.initiate ()

Confirm Settings

Rs0:primary> rs.conf ()

Add member to replica set

Set hostname in/etc/hosts first

Add first from library, test167:27017

Rs0:primary> rs.add (' test167:27017 ')

Add a second from library, test167:27018

The main library can be adjusted by priority, and when the election is over, priorities are elected as the main library

Rs0:primary> Rs.add ({host: "test167:27018", Priority:5})

Confirm Settings

Rs0:primary> rs.conf ()

View status

Rs0:secondary> Rs.status ()

Remove from library

First close the MongoDB from the library, and then remove the library from the main library

Rs0:primary> rs.remove (' test166:27019 ')
2.3 Simultaneous Action Confirmation

Insert a record on the main library

> Db.user.insert ({"username": "Test", "age": +, "Sex": "M"})

Viewing data from a library has been in the same period

Rs0:secondary> Db.getmongo (). Setreadpref (' secondarypreferred ')
rs0:secondary> use new2
rs0: Secondary> Db.user.find ()

2.4 Failover Action

Replica set heartbeat ping 2 seconds once, 10 seconds no response thought down.

The highest priority is elected as the main library, priority 0 can not be promoted.

Optime the highest from library to be selected as the primary library.

The main library needs to be able to connect to the other from the library, 1 main 2 from the case, down 2 sets, will not elect the main library.

Close the main library

> Use admin
> Db.shutdownserver ()

When viewing replica set status from a library, you can see an upgrade from the library to the primary library

Rs0:secondary> Rs.status ()

Start the closed Front main library, view the replica set status, see the main library drop to from the library, the former main library becomes the main library

Rs0:secondary> Rs.status ()

Note: Due to the use of Oplog logs during the same period, the former main library down the time, in the 1 main 1 from the composition of the data update, the former main library started, will come over the same period

3. Read and write separation

The main library, which supports read operations from the library. However, the default read is also read from the main library.

From the library you can open the support read operation by setting Readpreference, Readpreference has several modes:

Primary #从主的读, default

Primarypreferred #基本上从主的读, when the master is unavailable, from the read

Secondary #从从的读

Secondarypreferred #基本上从从的读, never available, read from the master

Nearest #从网络延迟最小的读

Basically, the primary,secondary,nearest is the most common

In the settings of the replica set, the members can be categorized by tag, and the type of reading is specified by the following method:

1, when the program is connected, specify the type of Read readpreference

2, connect with MONGO command, only valid for current connection

Rs0:secondary> Db.getmongo (). Setreadpref (' secondarypreferred ')
4. Special member types

Secondary also has some special member types:

Priority 0 #不能升为主, can be used in multi-data center scenarios

Hidden #对客户端来说是不可见的, generally used for backup or statistical reporting

Delayed #数据比副集晚, typically used as a rolling backup or historical snapshot

5. PostScript

This article describes the configuration of the replica set, and the next time you will describe the Shard.

MongoDB Series (ii): 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.