Mongodb Study Notes 08 and mongodb Study Notes
Official documentation:
Http://docs.mongodb.org/manual/replication/
Replication sets multiple servers maintain the same data copy
Step 1: how to use a replica set
When mongod is started, you must specify the replica set name. You can set the replica set name by adding-replSet name or setting replication. replSetName in the configuration file.
For example:
mongod --port 270217 --dbpath /mongo/database --logpath /mongo/log/mongo.log --replSet repl_test --smallfiles &mongod --port 270218 --dbpath /mongo2/database --logpath /mongo2/log/mongo.log --replSet repl_test --smallfiles &mongod --port 270219 --dbpath /mongo3/database --logpath /mongo3/log/mongo.log --replSet repl_test --smallfiles &
Step 2
Enter one of
For example:
mongo --port 27017
Then define the rs. initiate (rsconf) parameter rsconf
rsconf = { _id:'repl_test', members: [ { _id:0,host:'127.0.0.1:27017' }, { _id:1,host:'127.0.0.1:27018' }, { _id:2,host:'127.0.0.1:27019' }, ]}
Step 3
Initialize a replica set
rs.initiate(rsconf)
Operation on replica set
View replica set information
Default Value: _ id: 0. mongod is the primary node.
rs.status()
Delete a node
rs.remove(host)
Add Node
rs.add(host)rs.reconfig(rsconf)
Operations can be performed on the master node by default. If you enter the secondary node, you need to run the CRUD operation.
rs.slaveOk()
If the master node goes down, one of the remaining nodes will automatically become the master node.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.