MongoDB master-slave architecture


Master-slave architecture :

    mongodb supports the traditional master-slave architecture. The master node is responsible for reading and writing data, and the slave has no write permission. There is no automatic failover function, you need to specify the master and slave sides, and it is not recommended for use in production.


Advantages of master-slave replication:

  • The slave server can perform query work, reducing the access pressure of the master server

  • Perform backups on the slave to avoid locking the master's data during backups

  • When the master server fails, it can quickly switch to the slave server to reduce downtime.

 

MongoDB supports complete failover and redundancy through asynchronous replication in multiple machines. Only one of multiple machines is used for write operations at the same time, which provides mongoDB with data consistency guarantee. The machine that plays the primary role can Distribute read operations to slave machines.




Configuration of master-slave architecture

Environment: CentOS6.5 MongoDB3.4

master configuration file


master.conf

dbpath=/data/mongo/master
logpath=/var/log/mongo/master/mongodb.log
port=27017
bind_ip=127.0.0.1
master=true 
fork=true //Run mongodb service in the background


slave configuration file

dbpath=/data/mongo/slave
logpath=/var/log/mongo/slave/mongodb.log
port=27018
bind_ip=127.0.0.1
slave=true
fork=true //Run mongodb service in the background
source=127.0.0.1:27017 //Configure the main ip and port


Start master and slave respectively

mongo --config master.conf 
mongo --config slave.conf


Log in to master and slave respectively

mongo 127.0.0.1:27017
mongo 127.0.0.1:27018


Execute show dbs on the master

> show dbs
admin            0.000GB
local            0.005GB


Executing show dbs on the slave reports an error


> show dbs
2018-04-19T11:31:35.982+0800 E QUERY    [thread1] Error: listDatabases failed:{
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:782:19
shellHelper@src/mongo/shell/utils.js:672:15
@(shellhelp2):1:1

This error is because there is no read and write permission on the slave by default, you can execute the following command on the slave to solve


> rs.slaveOk() //The setting method of version 2.6 is different
> show dbs
admin            0.000GB
local            0.000GB



test:

 Create database masterslavetest  on main repository and create some test data

> use masterslave
switched to db masterslave
> for (i = 5000; i < 100000; i++) {
... db.users.insert({
... "i": i,
... "userName": "user" + i,
... 
... "age": Math.floor(Math.random() * 120),
... "created": new Date(),
... total: Math.floor(Math.random() * 100) * i
... })
... }
WriteResult({ "nInserted" : 1 })



Execute the following commands on the two instances respectively, and you can see that the data on the two databases is consistent. At this time, when CRUD and other operations are performed on the master database, the data from the slave database is still consistent with the master database.  

db.users.find()



image.png



The above command is also executed on the slave

image.png


Test adding a piece of data to the slave node to see

> db.mycoll.insert({"i":9999,"username":"test","age":30})
WriteResult({ "writeError" : { "code" : 10107, "errmsg" : "not master" } })

It can be seen that the slave node does not have write permission.


Turn off the mongodb process on the master and see

> show dbs
2018-04-19T11:58:43.464+0800 E QUERY    [thread1] Error: listDatabases failed:{
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:62:1
shellHelper.show@src/mongo/shell/utils.js:782:19
shellHelper@src/mongo/shell/utils.js:672:15
@(shellhelp2):1:1
> rs.slaveOk()
> show dbs
admin        0.000GB
local        0.000GB
masterslave  0.005GB

It can be known from the above test that if the master node hangs, the slave node cannot automatically take over the service and becomes the master role. You need to manually modify the configuration file, set it to master, and restart the service to read and write.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212363&siteId=291194637