Overview of MongoDB Replication Sets
- A replica set is an additional copy of the data that is the process of synchronizing data across multiple servers, and the replication set provides the sink to add data availability, and the benefits of replicating sets that can be replicated for a hardware-broken service through a replica set are as follows:
- Make your data more secure.
- High data Availability (24*7)
- Disaster recovery
- Maintenance without downtime (e.g. backup, index rebuild, failover)
- Read scaling (extra copy read)
- The replica set is transparent to the application. How replication sets work
- MongoDB's replica set requires at least two nodes, one master node (primary), which handles client requests, and the rest is slave nodes (secondary), responsible for replicating data on the master node.
- MongoDB each node common collocation Way is: one Master one from or a master many from. The master node records all of its operations into Oplog, and the nodes periodically poll the master node to get these operations, and then perform these operations on their own copy of the data, thereby guaranteeing the data from the node to the master node translator. The replication set features the following:
- N Node Group order
- Any node guest as the master node
- All write operations are in the master node
- Auto Fail-Over
- Automatic recovery starts deployment
Create multi-instance//create multi-instance shutdown firewall Setenforce//Some blocking items mkdir-p/data/mongodb/mongodb{2,3,4}//Create data Directory MkDir logs//to/data/mongodb/create log File directory Touch Logs/mongodb{2,3,4}.log//Create log file CD logs/to directory chmod 777 *.log//grant permissions//create multi-instance profile and turn on service mongod-f/etc/mongod. Conf--shutdown//Modify the Master profile and turn it off on mongod-f/etc/mongod.conf//Turn all services into the default port to turn on replication set cp-p/etc/mongod.conf/etc/mongod 2.conf//Ground Two instance Profile vim/etc/mongod2.conf path:/data/mongodb/logs/mongod2.log//System log Storage path DbPath:/data/m Ongodb/mongo//Data storage log port:27018//Port mkdir-p/data/mongodb///Create system log for it and data store log no directory Cd/data/mongodb/mkdir MONGO touch Mongod2.log//Create System day for it Log file chmod 777 Mongod2.log//and give permission mongod-f/etc/mongod2.conf//Open Multi-instance service MONGO--port 27018 Go to MongoDB 27018NETSTAT-NTAP//view process cfg={"_id": "ABC", "members": [{"_id": 0, "host": "192.168.10.5:27017", "Priority": 100},{"_id": 1,"Host": "192.168.10.5:27018", "Priority": 100},{"_id": 2, "host": "192.168.10.5:27019", "Priority": 0},{"_id": 3, "host" : "192.168.10.5:27020", "Arbiteronly": True}]}//Note IP precedence to determine priority ": 100//high as table quasi-node": 0// Low as passive node arbiteronly ": TRUE//Quorum node rs.initiate (CFG)//Initialize after refresh you can see your identity rs.ismaster ()//View node Status basic additions and deletions change Db.t1.insert ({ "id": 1, "name": "Tom"})//Create Collection Db.t1.find ()//view Current data Db.t1.update ({"id": 2},{$set: {"name": "Jack"}})// Modify specified content Db.t1.remove ({"id": 1})//delete specified data
Simulating failed node experiments to automatically preempt
show dbs //显示所有数据库use local //进入存放日志文件的数据库show collections //显示数据中的所有的集合db.oplog.rs.find() #查看日志记录所有操作模拟表准节点1故障mongod -f /etc/mongod.conf --shutdown #关闭一主节点服务看看是否别的mongoDB有没有抢占mongo --port 27018 #此时会选举第二个标准节点为主节点模拟标注节点2故障mongod -f /etc/mongod2.conf --shutdown #关闭第二主节点mongo --port 27019 #此时被动节点不能成为主节点
- Allow data to be read from a node
rs.slaveOk() #允许默认从节点读取数据
- Viewing replication status information
仲裁节点是不会复制信息rs.help() #帮助信息rs.printReplicationInfo() #查看日志文件大小信息configured oplog size: 990MB #这个日志文件大小是可以修改的rs.printSlaveReplicationInfo() #产看是和那个节点继进行同步#你会发现仲裁节点并不具备数据复制
- Change Oplog size Zero when modified
1. 要去调整日志文件大小,从节点会去找主节点进行同步对方的文件从而自己也进行了扩容,对于主节点来说自己的日志文件大小宽裕为最佳,若不够则可以零时进行调整 默认情况下990M,调整从节点1) 关闭节点服务,因为从节点是主节点复制集成员2)只能单独的进行修改,先关闭服务在从复制集中退出来,推出来后就成了一个单实例 ,3)use admin #先关闭服务4)db.shutdownServer() #退出复制集 离线升级5)修改从节点配置文件 6)mongod -f /etc/mongod2.conf //启动服务7)mongo --port 27028 //登陆8)mongodump --port 27028 --db local --collection ‘oplog.rs‘ //oplog进行完整性备份9)mongo --port 27028 //进端口10)use local> db.oplog.rs.drop() //删掉原来的文件11)db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } ) //进行原型创建 改为204812)use admin> db.shutdownServer() //再次关闭服务13)再次修改回来原先的数据 在replication:replSetName: abcoplogSizeMB: 2048 //添加14)mongod -f /etc/mongod2.conf //重新开启服务 mongo --port 27018 进入端口 15)rs.printReplicationInfo() //此时就成了2048 支对但对修改有效16)rs.stepDown()
Deploying certified Replication
1. 在主服务器上长创建用户为rootuse admin db.createUser({"user":"root","pwd":"123","roles":["root"]}) #创建用户密码vim /etc/mongod.conf //修改主配置文件同步并且中间有一个认证 修改所有配置文件security: keyFile: /usr/bin/kgcrskey1 //验证文件路径 clusterAuthMode: keyFile //验证密钥重启服务mongod -f /etc/mongod.conf --shutdownmongod -f /etc/mongod.conf /其他几台的重启方式都相同,重复操作即可身份验证登陆(先验证主,再验证从)当你直接使用登陆命令登陆系统时,使用show dbs 是不能够查看数据的,此时就需要使用身份验证mongo --port 27018use admindb.auth("root","123123")
MongoDB Replica Set