"MongoDB" configuration master-Slave mode replication set to build a complete tutorial

Source: Internet
Author: User

The biggest advantage of the NoSQL database is that it supports the cluster well, and today it is ready to use MongoDB to configure a master-slave server.

About the installation of MongoDB is not introduced, I use the Mac side of the MongoDB, version 3.4.9

There are two modes of MongoDB, one is the early Master-slave mode, and the other is the Replica-set mode, which is generally recommended to use Replica-set mode, because if the master node is hung, the remaining nodes will be elected. The next primary node is selected, and the fault tolerance is greatly improved.

The following is the beginning of configuring a Replica-set node set. There are three main nodes:

127.0.0.1:29010,primary

127.0.0.1:29011,secondary

127.0.0.1:29012,secondary

To build the directory first, I am under the MAC directory structure:

The root directory is/data/replica-project and there are three subdirectories under the root directory, respectively:

Log, store logs;

Data, storing the information;

Config, storage boot configuration;

In the Config directory, store three nodes of the configuration file:

R0.conf:

#数据文件夹
dbpath=/data/replica-project/data/r0

#日志文件夹, if run later, you must specify LogPath
logpath=/data/ Replica-project/log/r0.log

#以追加而非覆盖方式写入日志
logappend=true

#端口
port=29010

#以后台方式运行
fork= True

R1.conf:

#数据文件夹
dbpath=/data/replica-project/data/r1

#日志文件夹, if run later, you must specify LogPath
logpath=/data/ Replica-project/log/r1.log

#以追加而非覆盖方式写入日志
logappend=true

#端口
port=29011

#以后台方式运行
fork= True

R2.conf:

#数据文件夹
dbpath=/data/replica-project/data/r2

#日志文件夹, if run later, you must specify LogPath
logpath=/data/ Replica-project/log/r2.log

#以追加而非覆盖方式写入日志
logappend=true

#端口
port=29012

#以后台方式运行
Fork=true

Then start the above three nodes separately:

sudo mongod-f/data/replica-project/config/r0.conf--replset "Rs0"
sudo mongod-f/data/replica-project/config/r1.conf--replset "Rs0"
sudo mongod-f/data/replica-project/config/r2.conf--replset "Rs0"
The results are as follows:



Then we need to log on to the machine that we want to use as the primary node:

MONGO--port 29010


Then configure Replica-set:

Define a configuration object in the shell just logged in:

config={_id: "Rs0", Members:[{_id:0,host: "localhost:29010", Priority:1}]}

Set priority to 1, the machine will be elected as a primary node.

Invoke the RS API to initialize set:

Rs.initiate (config)


As you can see, this is different from the command prompt for logging on to the normal Mongod, where there is a RS0 prefix indicating a replica-set. But we're not setting it up for primary anymore. How is secondary. Don't worry, it hasn't been elected yet. We can use the command

Rs.conf ()

Check out the configuration we just made:



This prompt has changed to primary, stating that this node has been elected to master. The specialty of a master or Priamry node is that there will be at most one primary node in the set, and only the primary node can configure set nodes, such as additions and deletions, and only the primary nodes are used to add or update data. The secondary node can only read data.

Then we need to add the other nodes to this set:

Rs.add ("localhost:29011")
rs.add ("localhost:29012")



Use the command to view the status of the current set cluster:

Rs.status ()

The results are:

Rs0:primary> Rs.status () {"Set": "Rs0", "date": Isodate ("2017-10-03t01:18:55.080z"), "MyState": 1, "term": Num Berlong (1), "Heartbeatintervalmillis": Numberlong (Watts), "Optimes": {"Lastcommittedoptime": {"ts": Timestamp ( 1506993534, 1), "T": Numberlong (1)}, "Appliedoptime": {"ts": Timestamp (1506993534, 1), "T": Numberlong ( 1}, "Durableoptime": {"ts": Timestamp (1506993534, 1), "T": Numberlong (1)}}, "members": [{] _i D ": 0," name ":" localhost:29010 "," Health ": 1," state ": 1," Statestr ":" PRIMARY "," uptime ": 470," Optime ': {"ts": Timestamp (1506993534, 1), "T": Numberlong (1)}, "Optimedate": Isodate ("2017-10-03t01:18 : 54Z ")," Electiontime ": Timestamp (1506993212, 2)," Electiondate ": Isodate (" 2017-10-03t01:13:32z ")," Configversi On ': 3, ' self ': true}, {' _id ': 1, ' name ': ' localhost:29011 ', ' health ': 1, ' state ': 2, ' state Str ":" Secondary ","Uptime ":" Optime ": {" ts ": Timestamp (1506993534, 1)," T ": Numberlong (1)}," Optimedurable ": { "TS": Timestamp (1506993534, 1), "T": Numberlong (1)}, "Optimedate": Isodate ("2017-10-03t01:18:54z"), "Op Timedurabledate ": Isodate (" 2017-10-03t01:18:54z ")," Lastheartbeat ": Isodate (" 2017-10-03t01:18:54.457z ")," lastHear Tbeatrecv ": Isodate (" 2017-10-03t01:18:53.457z ")," Pingms ": Numberlong (0)," syncingto ":" localhost:29010 "," con Figversion ": 3}, {" _id ": 2," name ":" localhost:29012 "," Health ": 1," state ": 2," Statestr ":" SE Condary "," Uptime ":" Optime ": {" ts ": Timestamp (1506993534, 1)," T ": Numberlong (1)}," optimed Urable ': {"ts": Timestamp (1506993534, 1), "T": Numberlong (1)}, "Optimedate": Isodate ("2017-10-03t01:18 : 54Z ")," Optimedurabledate ": Isodate (" 2017-10-03t01:18:54z ")," Lastheartbeat ": Isodate (" 2017-10-03t01:18:54.457z ") , "Lastheartbeatrecv" : Isodate ("2017-10-03t01:18:53.650z"), "Pingms": Numberlong (0), "syncingto": "localhost:29010", "configversion ": 3}]," OK ": 1}
From here you can see that the configuration was successful.

Let's verify the master server, insert a piece of data inside the main library, and then read it from the library.

Insert a piece of data in the main library:

Use Test
db.test.insert ({name: "Liyao", age:25})

Log on to a secondary server:

MONGO--port 29011

And then try to read:

Use Test
db.test.find ()
The following message appears:


This is because the main library can only be manipulated by default, and if you want to read from the library, you must use the following command settings:

Db.setslaveok ()
Only then can inquire.

So far, a simple MongoDB master-slave architecture is configured.

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.