Master-Slave architecture :
MongoDB supports the traditional Master-slave architecture. The master node is responsible for reading and writing data, and slave does not have write permissions. There is no automatic failover feature, you need to specify the master and slave side, and are not recommended for use in production.
Configuration of master-slave architecture
Environment: CentOS6.5 MongoDB3.4
Master configuration file
Master.conf
Dbpath=/data/mongo/masterlogpath=/var/log/mongo/master/mongodb.logport=27017bind_ip=127.0.0.1master=true fork= True//Background running MongoDB service
Slave configuration file
Dbpath=/data/mongo/slavelogpath=/var/log/mongo/slave/mongodb.logport=27018bind_ip=127.0.0.1slave=truefork=true Background run MongoDB service source=127.0.0.1:27017//Configuration master IP and port
Start master and slave, respectively
MONGO--config master.conf MONGO--config slave.conf
Log in master and slave separately
MONGO 127.0.0.1:27017mongo 127.0.0.1:27018
Perform show DBS on master
> Show Dbsadmin 0.000GBlocal 0.005GB
Show dbs error on slave
> Show dbs2018-04-19t11:31:35.982+0800 E QUERY [thread1] error:listdatabases failed:{"OK": 0, "errm SG ":" Not Master and Slaveok=false "," code ": 13435," codename ":" Notmasternoslaveok "}: [Email protected]/ Mongo/shell/utils.js:25:13[email Protected]/mongo/shell/mongo.js:62:1[email Protected]/mongo/shell/utils.js : 782:19[email protected]/mongo/shell/utils.js:672:15@ (SHELLHELP2): 1:1
This error is due to the default slave does not have read and write permissions on the slave can be executed on the following command to resolve
> Rs.slaveok ()//2.6 version setup method is different > show dbsadmin 0.000GBlocal 0.000GB
Test:
Create a database on the main library masterslavetest and create some test data
> Use masterslaveswitched to DB masterslave> for (i =; i < 100000; i++) {... db.users.insert ({... "I": I,... "UserName": "User" + I,... ... "Age": Math.floor (Math.random ()),... "Created": New Date (),... Total:Math.floor (Math.random () * +) * I ...}) ... } Writeresult ({"ninserted": 1})
perform each of the two instances the following command you can see that the data on the two databases is consistent, which is performed on the primary databaseCRUDand so on, the data from the library is still consistent with the main library
Db.users.find ()
Slave also executes the above command.
Test add a piece of data to the slave node to see
> Db.mycoll.insert ({"I": 9999, "username": "Test", "Age": ()) Writeresult ({"Writeerror": {"code": 10107, "errmsg": " Not Master "}})
You can see that the slave node does not have write permissions.
Turn off the MONGO process on master.
> show dbs2018-04-19t11:58:43.464+0800 e query [thread1] Error: listDatabases failed:{ "OK" : 0, "errmsg" : "not" master and slaveok=false ", " code " : 13435, "codename" : "Notmasternoslaveok"} :[ email protected]/mongo/shell/utils.js:25:13[email protected]/mongo/shell/mongo.js:62:1[email protected]/mongo/shell/utils.js:782:19[email protected]/mongo/shell/utils.js:672:15@ (SHELLHELP2):1:1> rs.slaveok () > > > show dbsadmin &NBSP;0.000GBLOCAL&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0.000GBMASTERSLAVE&NBSP;&NBSP;0.005GB
Slave still has no write permission
MongoDB Master-Slave architecture and replica set schema