One, MongoDB replica set (REPL set) Introduction
Early version use Master-slave, a master one from and MySQL similar, but slave in this architecture is read-only, when the main library down, from the library can not automatically switch to primary;
Currently has eliminated the Master-slave mode, changed to a replica set, this mode has a master (primary), and multiple from (secondary), read-only, support to them set weights, when the main outage, the highest weight from the switch-based;
In this architecture, you can also establish a quorum (arbiter) role, which is only responsible for adjudication, not storing data
Read and write data in this architecture is the Lord, to achieve the purpose of load balancing needs to manually specify the target server to read the library
Second, MongoDB replica set construction
Server Environment Preparation
Three servers are CentOS release 6.5 (Final)
192.168.1.203 (primary)
192.168.1.201 (secondary)
192.168.1.202 (secondary)
Edit the configuration file for three machines and add the following:
Replication
Oplogsizemb:20 #oplog大小
Replsetname:fansik #名称自定义
Restart three MongoDB services
Connect the master, Run command MONGO in the Lord, and then
> Use admin
>config={_id: "Fanjinbao", Members:[{_id:0,host: "192.168.1.203:27017"},{_id:1,host: "192.168.1.201:27017"},{_ Id:2,host: "192.168.1.202:27017"}]}
> rs.initiate (config)
> Rs.add ("192.168.1.201")
> Rs.add ("192.168.1.202")
> Rs.status () # View status
If the two from the status of "Statestr": "STARTUP", you need to do the following > var config={_id: "Fanjinbao", Members:[{_id:0,host: " 192.168.1.203:27017 "},{_id:1,host:" 192.168.1.201:27017 "},{_id:2,host:" 192.168.1.202:27017 "}]}
> rs.initiate (config)
viewing Rs.status () again will find the state changed from secondary
Third, MongoDB copy set test
Our Lord built the library to build a collection
Use MyDB
Db.acc.insert ({accountid:1,username: "123", passwd: "123456"})
Show DBS
View from top
Show DBS if a series of errors occur
2016-05-13t00:03:36.719+0800 E QUERY error:listdatabases failed:{"note": "From ExecCommand", "OK": 0, "errmsg": "N OT Master "}
Perform
Rs.slaveok ()
Show DBS will see the database you created.
Iv. replica set Change weight simulation main outage
The default three machine weights are 1, if any one of the weights is set to higher than the other, then the machine immediately switch to primary role, so we preset three machines to the weight of 203:3,201:2,202:1
At the Lord's execution:
CFG = rs.conf ()
Cfg.members[0].priority = 3
Cfg.members[1].priority = 2
cfg.members[2].priority = 1
Rs.reconfig (CFG)
In this case, the second node will be a candidate node
The Lord discards packets entered from Port 27017
Iptables-i input-p TCP--dport 27017-j DROP
V. MongoDB Backup and Recovery
Back up the specified library
# mongodump-h ip-d dbname-o dir #-h back with server ip,-d back with database name, not add backup all libraries,-o specify where to back up, he is a directory
Back Up all libraries
# mongodump-h 192.168.1.203-o/tmp/
Backing up a specified collection
# mongodump-d fansik-c zhangsan-o/tmp/fansik #-c Specify the name of the collection
Export Collection as JSON file
# mongoexport-d mydb-c fansik-o/tmp/test.json #-o followed by the name of a file
Recover all libraries
# Mongorestore--drop dir/#其中dir是备份所有库的目录名字 which--drop optional, meaning to restore the data before deleting, not recommended to use
Restore the specified library
# mongorestore-d Mydb/mydb #-d Restored library name, MyDB is the library backup directory
Recovering collections
# mongorestore-d Mydb-c Zhangsan/fansik/zhangsan.bson #-c Back is the name of the collection to restore, then specify the recovered collection file, Bson format file
Import Collection
# mongoimport-d MYDB-C Test--file/tmp/test.json
MongoDB replica set building and backup recovery