Master/slave Mode of mongodb
# Master / Slave configuration of mongodb (without auth authentication)
# Notes:
-synchronize the time before the server node
-those who turn on the firewall must be allowed to pass through the relevant ports
-if you enable selinux, you should also set it
-it is best to establish a double-click mutual trust model.
# master configuration file
-add the line master = true
-the line bind_ip of multiple network card machines is written as a specific address as far as possible (preferably an intranet address), because the slave is synchronized according to this.
[root@blog ~] # sed-e'/ ^ # / etc/mongodb.confbind_ip = 192.168.58.10 port = 27017fork = truepidfilepath = / var/run/mongodb/mongodb.pidlogpath = / var/log/mongodb/mongodb.logdbpath = / mongo/datajournal = truemaster = truenohttpinterface = true [root@blog ~] #
# slave configuration file
The following two lines were added
-slave = true
-source = 192.168.58.10 purl 27017
[root@redis ~] # sed-e'/ ^ # / dscape / ^ $/ d' / etc/mongodb.confbind_ip = 192.168.58.30port = 27017fork = truepidfilepath = / var/run/mongodb/mongodb.pidlogpath = / var/log/mongodb/mongodb.logdbpath = / var/lib/mongodbjournal = truenohttpinterface = trueslave = truesource = 192.168.58.10 etc/mongodb.confbind_ip 27017 [root@redis ~] #
# restart the mongodb service of the two nodes to initialize and verify data synchronization
Master
[root@blog ~] # [root@blog ~] # hostnameblog.unix178.com [root@blog ~] # mongoMongoDB shell version: 2.4.6connecting to: test > show dbsadmin0.203125GBlocal10.0732421875GBtest0.203125GBzhuima0.203125GB >
Slave
[root@redis ~] # hostnameredis.unix178.com [root@redis ~] # mongoMongoDB shell version: 2.4.6connecting to: test > show dbsadmin0.203125GBlocal0.078125GBzhuima0.203125GB >
# View synchronization
On the master server
[root@blog ~] # mongo 192.168.58.30MongoDB shell version: 2.4.6connecting to: 192.168.58.30/test > show dbsadmin0.203125GBlocal0.078125GBnick0.203125GBtest0.203125GBzhuima0.203125GB >
On the slave server
[root@redis ~] # mongo 192.168.58.10MongoDB shell version: 2.4.6connecting to: 192.168.58.10/test > show dbs;admin0.203125GBlocal10.0732421875GBnick0.203125GBtest0.203125GBzhuima0.203125GB >
# add new data for verification
Add data on master
[root@redis ~] # mongo 192.168.58.10MongoDB shell version: 2.4.6connecting to: 192.168.58.10/test > show dbs;admin0.203125GBlocal10.0732421875GBnick0.203125GBtest0.203125GBzhuima0.203125GB > use nickswitched to db nick > use newdbswitched to db newdb > db.newdb.insert ({new: "yes", info: "nothing"}) > show collectionsnewdbsystem.indexes > db.newdb.find () {"_ id": ObjectId ("53cbd4b35974b01658718b2d"), "new": "yes", "info": "nothing"} >
Verification on slave
[root@blog ~] # mongo 192.168.58.30MongoDB shell version: 2.4.6connecting to: 192.168.58.30/test > show dbsadmin0.203125GBlocal0.078125GBnick0.203125GBtest0.203125GBzhuima0.203125GB > show dbsadmin0.203125GBlocal0.078125GBnewdb0.203125GBnick0.203125GBtest0.203125GBzhuima0.203125GB > use newdbswitched to db newdb > show collectionsnewdbsystem.indexes > db.newdb.find () {"_ id": ObjectId ("53cbd4b35974b01658718b2d"), "new": "yes", "info": "nothing"} >
# postscript
Mongodb officials no longer recommend using the structure of the master/salve pattern.
Replica_set replication set mode is officially recommended
For reference only