Master-slave replication is a feature of MongoDB database, which improves the disaster-tolerant capability of database by data backup. However, since master-slave replication does not automatically implement failover features, MongoDB has developed a new replication model in version 1.6: Replicate Sets. MongoDB recommends that you do not use the Master-slave mode to back up your data. But for learning, we can still understand the master-slave copy mode of MongoDB.
1. Download the latest version of MongoDB from MongoDB's official website and extract it to a directory.
2. Under the MongoDB folder, create the/data/master and/data/slave directories.
3. Start the primary node (master) server and run the following command:
Mongod--dbpath/data/master--port 10000--master |
After the above command is executed, MongoDB generates the data files and log files under Data/master.
4. Start the server from the node (Slave) and run the following command:
Mongod--dbpath/data/slave--port 10001--slave--source |
After the above command is executed, the data file and log files are also generated under Data/slave and the associated information with master is created under the sources table under the local database. 5. Test whether the master-slave copy (Master-slave) is effective.
Use MONGO localhost:10000 to open the master database and insert a test statement: Db.test.find (); Db.test.insert ({"Host": "1000"}); Db.test.find (); using "MONGO localhost:10001" to open the slave database, running Db.test.find () will find that the queried data is the same as the data queried from the master database.
Run Db.test.insert ({"Girl": "Lili"}), the not master is displayed. This is because the master-slave replication mode only allows data to be updated from the master database, not from the slave database. As a result, when master fails, the disadvantage of being unable to convert a primary node from a node is a burst of leakage.