There are three common types of MongoDB clusters, namely master-slave replication, replica set and shard. This article will briefly introduce master-slave replication
To start, all the code used in this article was tested locally on CentOS 7
Ok, let’s begin!
1, the introduction
Master/Slaver replication is the easiest way to set up a cluster
In fact, it just uses one or more slave nodes to synchronize the data of the master node. The overall structure is not complicated and easy to set up
The primary and secondary replication clusters have the following characteristics:
- The secondary node backs up data on the primary node to ensure data security
- All read and write operations occur on the primary node, resulting in high pressure on the primary node
- When the primary database fails, the client cannot continue to read and write data
Data synchronization
When writing or updating data, the primary node writes operation information to oplog (Operation log) log files
The slave node periodically obtains the oplog of the master node and updates its own data based on the log content to keep the data consistent with the master node
2, build
The following is a simple simulation performed locally, with one master node and one slave node
(1) Download MongoDB
> su
> cd /root
> mkdir mongodb-master-slaver-cluster
> cd mongodb-master-slaver-cluster
Copy the code
> mkdir mongodb
> cd mongodb
>Wget HTTP: / / https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.7.tgz
>Tar - ZXVF mongo - Linux - x86_64-3.2.7. TGZ
> cd.
Copy the code
(2) Create two folders to simulate the Master and Slaver nodes, respectively
> mkdir master # master node
> touch ./master/mongodb.conf
> mkdir ./master/log
> mkdir -p ./master/data/db
Copy the code
> mkdir slaver # from the node
> touch ./slaver/mongodb.conf
> mkdir ./slaver/log
> mkdir -p ./slaver/data/db
Copy the code
After creation, 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.confCopy the code
(3) Write the configuration file
#Master /mongodb.conf file contents
#The location where data files are stored
dbpath = /root/mongodb-master-slaver-cluster/master/data/db
#Location for storing log files
logpath = /root/mongodb-master-slaver-cluster/master/log/mongodb.log
#Listen on port. Default is 27017
port = 27001
#Run MongoDB as a daemon
fork = true
#Ensure that the node is the master node
master = true
Copy the code
#Slaver /mongodb.conf file contents
#The location where data files are stored
dbpath = /root/mongodb-master-slaver-cluster/slaver/data/db
#Location for storing log files
logpath = /root/mongodb-master-slaver-cluster/slaver/log/mongodb.log
#Listen on port. Default is 27017
port = 27002
#Run MongoDB as a daemon
fork = true
#Ensure that the node is a slaver node
slave = true
#Specify the data synchronization source, pointing to the master nodeSource = 127.0.0.1:27001Copy the code
(4) Open the two databases respectively
> cd/ root/mongo - master - slaver - cluster/mongo/mongo - 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
Copy the code
That’s it, and you should see two MongoDB processes running
> ps aux | grep mongo
Copy the code
3, test,
Open a new terminal, connect to the primary database, insert a piece of data (writable), and query it (readable)
> cd/ root/mongo - master - slaver - cluster/mongo/mongo - Linux - x86_64-3.2.7 / bin /
>. / mongo mongo: / / 127.0.0.1:27001 / admin
Copy the code
> db.user.insert({'username': 'Alice'.'password': '123456'}) // Insert data (writable)
// WriteResult({ "nInserted" : 1 })
> db.user.find() // Query data (readable)
// { "username" : "Alice", "password" : "123456" }
Copy the code
Open another terminal, connect to the slave database, check whether it is synchronized (readable), and then try to insert a piece of data from the slave database (not writable).
> cd/ root/mongo - master - slaver - cluster/mongo/mongo - Linux - x86_64-3.2.7 / bin /
>. / mongo mongo: / / 127.0.0.1:27002 / admin
Copy the code
> rs.slaveOk() // You need to set the status of the secondary node before querying
> db.user.find() // Query data (readable)
// { "username" : "Alice", "password" : "123456" }
> db.user.insert({'username': 'Bob'.'password': 'abc'}) // Insert data (not writable)
// WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })
Copy the code
Open another terminal and manually shut down the primary database to simulate the failure of the primary node
> cd/ root/mongo - master - slaver - cluster/mongo/mongo - Linux - x86_64-3.2.7 / bin /
> ./mongod --shutdown --dbpath /root/mongodb-master-slaver-cluster/master/data/db
Copy the code
If we send a query or insert request to the main database at this point, we will display a connection failure error
> db.user.find()
// network error while attempting to run command 'find' on host '127.0.0.1:27001'
Copy the code