Master-slave synchronization:
1. Create a database directory
Mkdir/mongodb/masterdb
Mkdir/mongodb/slavedb_1
Mkdir/mongodb/slavedb2.
2. Start the Master/Slave servers respectively.
Start the master server to listen to port 10000
./Bin/mongod-dbpath/mongodb/masterdb/-- port10000 -- master
You can also use master-> slave settings to start two slave servers with ports 10001 and 10002 respectively.
./Bin/mongod-dbpath/mongodb/slavedb_1 -- source localhost: 10000 -- slave -- port 10001
./Bin/mongod-dbpath/mongodb/slavedb2-source localhost: 10000 -- slave -- port 10002
# After startup, you will see a log showing the copied content from the master server.
Related Parameters:./mongod -- help
-- Autoresync when the slave server data is not the latest, the master server requests data synchronization.
-- Slavedelay synchronization delay, in seconds
3. Test Master/Slave
A. Create a database on the master server
./Bin/mongo -- port 10001
Show dbs
# Only the system database is in it. If you insert data, the system prompts not master.
Use testdb
Db. blog. save ({title: "newarticle "})
B. view the synchronized data on the slave server
./Bin/mongo -- port 10001
MongoDB shell version: 1.6.0
Connecting to: 127.0.0.1: 10001/test
> Show dbs
Admin
Local
Testdb
> Use testdb
Switched to db testdb
> Db. blog. find ()
{"_ Id": ObjectId ("4c776ccce7af0727ce4b6234"), "title": "newarticle"} # the synchronized data is tested successfully.