The study notes build MongoDB (D) cluster master-slave replication

There are three common MongoDB clusters, which are the main, this article will be a brief introduction to the main copy from the copy, replica sets and fragmentation

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

Master-slave replication (Master / Slaver) can be regarded as the easiest way to build a cluster matter, strictly speaking, probably not really a cluster

In fact, it is only using one or more data nodes from the synchronization master node only, the overall structure is not complicated, it is also relatively simple to build

Master-slave replication features of the cluster are as follows:

  • Back Up Using the data from the node to the master node, data security
  • Read and write operations are occurring on the master node, the master node leading to greater pressure
  • When the primary database hang up, the client can not continue to read and write data

2, set up

The following is a simple simulation performed locally, a master node, a node from

(1) download MongoDB

> su
> cd /root
> mkdir mongodb-master-slaver-cluster
> cd mongodb-master-slaver-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 two folders, respectively, to simulate master node and slaver

> mkdir master # 主节点
> touch ./master/mongodb.conf
> mkdir ./master/log
> mkdir -p ./master/data/db
> mkdir slaver # 从节点
> touch ./slaver/mongodb.conf
> mkdir ./slaver/log
> mkdir -p ./slaver/data/db

Once created, the directory structure is as follows:

+ mongodb-master-slaver-cluster
    + mongodb
        + mongodb-linux-x86_64-3.2.7.tgz
        + mongodb-linux-x86_64-3.2.7
    + master
        + log
        + data
            + db
        + mongodb.conf
    + slaver
        + log
        + data
            + db
        + mongodb.conf

(3) write a configuration file

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

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

# 确认本节点为 slaver 节点
slave = true
# 指定数据同步来源,指向 master 节点
source = 127.0.0.1:27001

(4) open respectively two databases

> cd /root/mongodb-master-slaver-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongod -f /root/mongodb-master-slaver-cluster/master/mongodb.conf
> ./mongod -f /root/mongodb-master-slaver-cluster/slaver/mongodb.conf

This will be all right, this time you should see two processes are running MongoDB

> ps aux | grep mongo

3, the test

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

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

Open another terminal connected to the database, the query is synchronized (read), and then try to insert data from a database (not write)

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

A terminal to open, manual close master database, the master node simulate hang

> cd /root/mongodb-master-slaver-cluster/mongodb/mongodb-linux-x86_64-3.2.7/bin/
> ./mongod --shutdown --dbpath /root/mongodb-master-slaver-cluster/master/data/db

If then we'll send a query or inserting the primary database requests will display an error connection failure

> db.user.find()
// network error while attempting to run command 'find' on host '127.0.0.1:27001' 

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

Guess you like

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