mongodb replication set (replica set) to build and manage

1. Create three node data storage directory
# mkdir -p / Data / Data / r0 of
# mkdir -p / Data / Data / R1
# mkdir -p / Data / Data / R2

2. Create three nodes log storage directory
# mkdir -p / data / log /

3. Create and configure three node replication set key file
# mkdir -p / the Data / key
# echo "the this IS RS1 Super Secret key"> / the Data / key / r0
# echo "the this IS RS1 Super Secret key"> / the Data / Key / R1
# echo "IS RS1 the this Super Secret Key"> / Data / Key / R2
# the chmod 600 / Data / Key / R & lt *

4.启动复制集的三个mongodb实例
# /usr/local/mongodb/bin/mongod --replSet rs1 --keyfile /data/key/r0 --fork --port 28010 --dbpath /data/data/r0 --logpath=/data/log/r0.log --logappend
# /usr/local/mongodb/bin/mongod --replSet rs1 --keyfile /data/key/r1 --fork --port 28011 --dbpath /data/data/r1 --logpath=/data/log/r1.log --logappend
# /usr/local/mongodb/bin/mongod --replSet rs1 --keyfile /data/key/r2 --fork --port 28012 --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend

The replication configuration and initialization sets the environment
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> config_rs1 ID = {_: 'RS1', Members: [{_ ID: 0, Host: 'localhost: 28010 '}, {_ ID:. 1, Host:' localhost: 28011 '}, {_ ID: 2, Host:' localhost: 28012 '}]}
> rs.initiate (config_rs1);

6. View replica status
> rs.Status ();
> rs.isMaster ();

7. The master-slave operation log and view the status
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> Show Databases;
> local use;
> Show Collections;
> db.oplog.rs.find ()
> db.printReplicationInfo ()
> db.printSlaveReplicationInfo ()

8. Check the configuration information from the master
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> Show Databases;
> local use;
> Show Collections;
> db.system.replset.find ()

9. manage replica
1) from the switching master
- the other examples outside the current master instance (28010) and the target primary example (28012) (28011) are set to "frozen" state, where ice 30 seconds
# cd / usr / local / MongoDB / bin
# 28011 ./mongo --port
> rs.freeze (30)

- the current primary instance (28010) "downgrade", here downgrade 30 seconds
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> rs.stepDown (30)

- Check the status replication set
# cd / usr / local / MongoDB / bin
# ./mongo --port 28010
> rs.Status ()

2) separate read and write
- a data insertion Xianxiang main library
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> db.c1.insert ({Age: 30})
> db.c1. find ()

- Log in to check from the library
# cd / usr / local / MongoDB / bin
# ./mongo --port 28011
> Show the Collections
- this time from a database query error

- Set-readable from the library
# CD / usr / local / MongoDB / bin
# 28011 ./mongo --port
> db.getMongo () setSlaveOk ().

> show collections
> db.c1.find()

3) Failover
--kill off the main 28010 Examples
# PS -ef | grep Mong
# -2 PID or the kill the kill or the kill -9 -15 PID PID
--kill -9 mongodb may cause collapse, try not to use

- Log in to check replication set from node status
# cd / usr / local / MongoDB / bin
# ./mongo --port 28011
> rs.Status ();

4) add or delete nodes
- nodes increases by oplog
# mkdir -p / Data / Data / R3
# echo "IS RS1 the this Super Secret Key"> / Data / Key / R3
# the chmod 600 / Data / Key / R3
# / usr / local / mongodb / bin / mongod --replSet rs1 --keyFile / data / key / r3 --fork --port 28013 --dbpath / data / data / r3 --logpath = / data / log / r3.log - logappend
# # cd / usr / local / MongoDB / bin
# ./mongo --port 28010
> rs.add ( "localhost: 28013")
- continuous view the replication set state
> rs.status ()

--errmsg from the initialization state (still initializing) -> Data Synchronization (initial sync need a member to be primary or secondary to do our initial sync) -> the completion of the initial synchronization (initial sync done) -> node addition was complete ( errmsg disappear; stateStr becomes second)

- Database snapshots and add nodes oplog

- take a physical replica set member as the initialization data file
# -R & lt SCP / Data / Data / R3 / Data / Data / R4
# echo "IS RS1 the this Super Secret Key"> / Data / Key / R4
# the chmod 600 / data / key / r4

- After completion of the snapshot acquisition, the main repository c1 insert a record, a subsequent synchronizing operation for verifying
# CD / usr / local / MongoDB / bin
# ./mongo --port 28010
> db.c1.find ()
> DB .c1.insert ({Age: 20 is})
> db.c1.find ()

--启用28014新实例
# /usr/local/mongodb/bin/mongod --replSet rs1 --keyFile /data/key/r4 --fork --port 28014 --dbpath /data/data/r4 --logpath=/data/log/r4.log --logappend

- Log primary database instances, add instances 28014
# cd / usr / local / MongoDB / bin
# ./mongo --port 28010
> rs.add ( "localhost: 28014")

- log 28 014 Example verification synchronization
# CD / usr / local / MongoDB / bin
# ./mongo --port 28014
> rs.slaveOk ()
> db.c1.find ()

5) reduction of node
- logging in the main library node
# cd / usr / local / MongoDB / bin
# ./mongo --port 28010
> rs.Status ()
> rs.remove ( "localhost: 28014")
> rs.remove ( "localhost: 28013")
> rs.Status ()

Guess you like

Origin www.cnblogs.com/lhdz_bj/p/12124772.html