MongoDB study notes (five) to build a replica set of clusters

There are three common MongoDB clusters, namely, master-slave replication, replica sets and fragmentation, this article will be on the replica set a brief introduction

Let me talk about the beginning, all of the code used in this article are tested through local operating system bloggers to test for CentOS 7

Well, the official start below!

1 Introduction

Mentioned before, the primary backup data node from the master node from the cluster using a copy can manually restore data after a failure of the primary node

But there is a problem that every time when the primary node fails, the need to suspend service and manually transfer data

Not only cumbersome, but also out of service caused by the loss is immeasurable, replica set is to solve this problem occurred

In a replica set (Replica Set), the bearing comprising a plurality of data nodes (Data Bearing) and arbitration node (the Arbiter)

Data bearer node can be subdivided master node (Primary) and slave node (Secondary), each of which is described below:

  • The master node: one and only one, all CRUD services to clients by default
  • From node: data backup master node can provide access to services, after the main nodes hang to choose from a primary node to continue to provide services
  • Arbitration node: does not store data, determine which one selected node from the master node in master node hang

Characterized replica set as follows:

  • Write operation only on the primary node, and the node to be synchronized to ensure data consistency
  • May be read on the master node, the slave node may be, to some extent, can reduce the burden on the master node
  • When the primary node hang up, election from a primary node to continue to provide services, so no downtime for maintenance

2, set up

The following is a simple simulation, a master node from a node, an arbitration node

(1) download MongoDB

> su
> cd /root
> mkdir mongodb-replica-set-cluster
> cd mongodb-replica-set-cluster
> mkdir mongodb
> cd mongodb
> wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.7.tgz
> tar -zxvf mongodb-linux-x86_64-3.2.7.tgz
> cd ..

(2) Create three folders, each analog master node, slave nodes and node arbitration

> mkdir primary # 主节点
> touch ./primary/mongodb.conf
> mkdir ./primary/log
> mkdir -p ./primary/data/db
> mkdir secondary # 从节点
> touch ./secondary/mongodb.conf
> mkdir ./secondary/log
> mkdir -p ./secondary/data/db
> mkdir arbiter # 仲裁节点
> touch ./arbiter/mongodb.conf
> mkdir ./arbiter/log
> mkdir -p ./arbiter/data/db

Once created, the directory structure is as follows

+ mongodb-replica-set-cluster
    + mongodb
        + mongodb-linux-x86_64-3.2.7.tgz
        + mongodb-linux-x86_64-3.2.7
    + primary
        + log
        + data
            + db
        + mongodb.conf
    + secondary
        + log
        + data
            + db
        + mongodb.conf
    + arbiter
        + log
        + data
            + db
        + mongodb.conf

(3) write a configuration file

# primary/mongodb.conf 文件内容
# 数据文件的存放位置
dbpath = /root/mongodb-replica-set-cluster/primary/data/db
# 日志文件的存放位置
logpath = /root/mongodb-replica-set-cluster/primary/log/mongodb.log
# 监听的端口,默认为 27017
port = 27001
# 以守护进程的方式运行 MongoDB
fork = true

# 设置 replSet 名称
replSet = repl
# secondary/mongodb.conf 文件内容
# 数据文件的存放位置
dbpath = /root/mongodb-replica-set-cluster/secondary/data/db
# 日志文件的存放位置
logpath = /root/mongodb-replica-set-cluster/secondary/log/mongodb.log
# 监听的端口,默认为 27017
port = 27002
# 以守护进程的方式运行 MongoDB
fork = true

# 设置 replSet 名称
replSet = repl
# arbiter/mongodb.conf 文件内容
# 数据文件的存放位置
dbpath = /root/mongodb-replica-set-cluster/arbiter/data/db
# 日志文件的存放位置
logpath = /root/mongodb-replica-set-cluster/arbiter/log/mongodb.log
# 监听的端口,默认为 27017
port = 27003
# 以守护进程的方式运行 MongoDB
fork = true

# 设置 replSet 名称
replSet = repl

(4) open respectively three database

> cd /root/mongodb-replica-set-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongod -f /root/mongodb-replica-set-cluster/primary/mongodb.conf
> ./mongod -f /root/mongodb-replica-set-cluster/secondary/mongodb.conf
> ./mongod -f /root/mongodb-replica-set-cluster/arbiter/mongodb.conf

After that, you should see three processes are running MongoDB

> ps aux | grep mongo

(5) connected to the relationship between any node, the node initialize

> cd /root/mongodb-replica-set-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongo mongodb://127.0.0.1:27001/admin
> rs.initiate({
    "_id": "repl",
    "members": [ // 添加数据承载节点
        {
            "_id": 1,
            "host": "127.0.0.1:27001"
        },
        {
            "_id": 2,
            "host": "127.0.0.1:27002"
        }
    ]
})
// { "ok" : 1 }

repl:OTHER> rs.addArb("127.0.0.1:27003") // 添加仲裁节点
// { "ok" : 1 }

repl:PRIMARY> rs.status() // 在设置成功后,应该可以看到具体的配置信息

3, a copy of the test set

Open a new terminal, connected to the main database, a data insertion (write), then subjected to inquiry (read)

> cd /root/mongodb-replica-set-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongo mongodb://127.0.0.1:27001/admin
PRIMARY> db.user.insert({'username': 'Alice', 'password': '123456'}) // 插入数据(可写)
// WriteResult({ "nInserted" : 1 })
PRIMARY> db.user.find() // 查询数据(可读)
// { "username" : "Alice", "password" : "123456" }

Open another terminal connected to the node data in the main database query inserted (read), then an attempt to insert new data (non-write)

> cd /root/mongodb-replica-set-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongo mongodb://127.0.0.1:27002/admin
SECONDARY> rs.slaveOk() // 在查询之前需要先设置从节点状态
SECONDARY> db.user.find() // 查询数据(可读)
// { "username" : "Alice", "password" : "123456" }
SECONDARY> db.user.insert({'username': 'Bob', 'password': 'abc'}) // 插入数据(不可写)
// WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })

Well, then a terminal to open, manual close the master node, the master node simulate hang

> cd /root/mongodb-replica-set-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongod --shutdown --dbpath /root/mongodb-replica-set-cluster/primary/data/db

Then in connection with the 127.0.0.1:27002view of the current replica set of the terminal in the state

SECONDARY> rs.status()

And it may be seen that the node has become the master node (note that this time the first command line has changed), and can be inserted inside the data query

PRIMARY> db.user.insert({'username': 'Bob', 'password': 'abc'}) // 插入数据(可写)
// WriteResult({ "nInserted" : 1 })
PRIMARY> db.user.find() // 查询数据(可读)
// { "username" : "Alice", "password" : "123456" }
// {'username': 'Bob', 'password': 'abc'}

[Read more MongoDB series of articles, look at MongoDB study notes ]

Guess you like

Origin www.cnblogs.com/wsmrzx/p/11599039.html