MongoDB replica set and management MongoDB replication Set Overview What is a replication set
- A replica set is an additional copy of the data that is the process of synchronizing data across multiple servers, providing redundancy and increasing data availability through a replica set to recover from hardware failures and interrupted services.
The advantages of a copy set are as follows:
1), make the data more secure;
2), high data availability;
3), disaster recovery;
4), no downtime recovery (such as backup, index reconstruction, failover);
5), read zoom (additional copy read);
6), the replica set is transparent to the application;
How replication sets work
- A copy set of MongoDB requires at least two nodes. One is the master node (Primary), which handles the client's request, and the rest is the slave node (secondary), which replicates the data on the master node.
- MongoDB each node collocation way is: one Master one from or a master many from. The master node records all operations on it into Oplog, obtains these operations from the node periodically polling the master node, and then performs these operations on its own copy of the data, ensuring that the data from the node is consistent with the primary node.
- The client writes the data on the primary node, reads the data from the node, and the master node interacts with the slave node to ensure the consistency of the data. If one of the nodes fails, the other nodes will immediately connect to the business without downtime.
The replication set features the following:
1), n nodes of the cluster;
2), any node can be the master node;
3), all write operations are on the master node;
4), automatic fault transfer;
5), automatic recovery;
MongoDB replication Set deployment 1, configuring replication sets
- 1), create multiple instances;
Mkdir-p/data/mongodb/mongodb{2,3,4} #创建数据目录
Mkdir/data/logs
Touch/data/logs/mongodb{2,3,4}.log #创建日志文件
Cd/data/logs
chmod 777 *.log #赋予权限
Vi/etc/mongod.conf
bindIp: 0.0.0.0 #监听地址path: /data/mongodb/mongod.log #日志文件存储目录dbPath: /data/mongodb/mongo #数据存储目录port: 27017 #监听端口;每一个实例端口不一样---省略内容---replication: replSetName: kgcrs #复制集的名称
CP mongod.conf mongod2.conf
CP mongod.conf mongod3.conf
CP mongod.conf mongod4.conf
- #编辑完成后, copy the three points as a configuration file for the other three instances, Setting the port parameter in mongod2.conf to the port parameter in 27018,mongod3.conf to 27019,mongod4.conf is set to 27020, and the padb and Thpath parameters are also modified to the corresponding path values.
Mongod-f/etc/mongod.conf #启动所有实例
MONGO--port 27017
Mongod-f/etc/mongod2.conf
MONGO--port 27018
Mongod-f/etc/mongod3.conf
MONGO--port 27019
Mongod-f/etc/mongod4.conf
MONGO--port 27020
- 2), initialize configuration replica set
Mongo
cfg={"_id": "Kgcrs", "members": [{"_id": 0, "host": "192.168.27.28:27017"},{"_id": 1, "host": "192.168.27.128:27018"},{ "_id": 2, "host": "192.168.27.128:27019"}]} #定义cfg初始化参数
Rs.initiate (CFG) #初始化启动复制集
Mongo
Rs.add ("192.168.27.128:27020") #添加节点
Rs.status () #查看状态
Rs.remove ("192.168.27.128:27020") #删除节点
Re.status #查看状态, the newly added node has been deleted
Mongo-f/etc/mongod.conf--shutdown #关闭复制集当前主节点
MONGO--port 27018 #进入到第二个节点中
Rs.ststus () #查看状态
Mongod-f/etc/mongod.conf #开启第一个节点
Kgcrs:primary> Rs.freeze (#暂停30s不参与选举)
MONGO--port 27018
Kgcrs:primary> Rs.stepdown (60,30) #交出主节点位置, maintains from node state of not less than 60 seconds, waits 30 seconds to synchronize the master node and slave node logs
Rs.ststus () #查看状态
MongoDB copy set election principle 1, copy principle
- Replication is based on the operation Log Oplog, which is equivalent to the binary log in MySQL and records only the changes that have occurred. Replication is the process of synchronizing and applying the Oplog log of the master node to other slave nodes.
2. Principles of Election
The node types are divided into standard (host) nodes, passive (passive) nodes, and quorum (arbiter) nodes.
1), only the standard node may be elected as active (primary) node, have the right to vote. The passive node has a complete copy, cannot become an active node, and has the right to vote. The quorum node does not replicate data and cannot become an active node, only the right to vote.
- 2), the difference between the standard node and the passive node: The higher priority value is the standard node, the lower is the passive node.
-3), the election rules are the highest number of votes wins, priority is the value of 0~1000, equivalent to an additional 0~1000 votes. Election result: The number of votes is high, if the number of votes is the same, the new data winner.
3. Configure the priority of the replication set and view the Oplog log
Mongon
cfg={"_id": "Kgcrs", "members": [{"_id": 0, "host": "192.168.27.128:27017", "Priority": 100},{"_id": 1, "host": " 192.168.27.128:27018 "," Priority ": 100},{" _id ": 2," host ":" 192.168.27.128:27019 "," Priority ": 0},{" _id ": 3," host ":" 192.168.27.128:27020 "," Arbiteronly ": True}]} #配置4个节点的复制集
Rs.reconfig (CFG)
Kgcrs:primary> Rs.ismaster () #查看状态信息
Kgcrs:primary> Use KGC
Kgcrs:primary> Db.t1.insert ({"id": 1, "name": "Tom"}) #插入数据
Kgcrs:primary> Db.t1.insert ({"id": 2, "name": "Jerry"}) #插入数据
Kgcrs:primary> Db.t1.find () #查看集合中的数据
Kgcrs:primary> db.t1.update ({"id": 2},{$set: {"name": "Jack"}}) #更改
Kgcrs:primary> Db.t1.remove ({"id": 1}) #删除
kgcrs:primary> Use Local
Kgcrs:primary> Show Collections #查看集合
显示 oplog.rs
Kgcrs:primary> Db.oplog.rs.find () #查看日志记录所有操作
#oplog会包含所有对数据有修改的操作, but the query operation is not logged.
4, Analog node failure
-1), if the primary node fails, another standard node will be elected as a new master node;
Mongod-f/etc/mongod.conf--shutdown #关闭第一个节点服务器
MONGO--port 27018 #此时会选举第二个标准节点为主节点
Rs.status () #查看状态信息
- 2), when all the standard nodes have failed, the passive node can not become the primary node;
Mongod-f/etc/mongod2.conf--shutdown #关闭第二个节点服务器
MONGO--port 27019 #此时被动节点不能成为主节点
Rs.status () #查看状态信息
MongoDB replica set management allows data to be read from nodes
- The slave node of the default MongoDB replica set cannot read data, and the Rs.slaveok () command can be used to read data from the node.
Viewing replication Set status information
- You can use Rs.printreplicationinfo () to view the log size, time range, and Rs.printslavereplicationinfo () to see the copied node, time;
Change the Oplog size
MONGO--port 27018
Rs.printreplicationinfo () #查看日志大小, time range
Use admin
Db.shutdownserver ()
Exit
vim/etc/mongod2.conf #注销replication: Related startup parameters, and modify port port number 27028
net: port: 27028#replication: #replSetName: kgcrs
Mongod-f/etc/mongod2.conf #单实例模式启动
Mongodump--port 27028--db local--collection ' oplog.rs ' #全备当前节点的所有oplog记录
MONGO--port 27028
Use local
Db.oplog.rs.drop () #删除原日志文件
Db.runcommand ({create: "oplog.rs", Capped:true, Size: (2 1024x768 * 1024x768)}) #重建新的日志文件 and specify the size
Use admin
Db.shutdownserver () #关闭服务
Exit
Vim/etc/mongod2.conf
net: port: 27018replication: replSetName: kgcrs oplogSizeMB: 2048 #指定大小
Mongod-f/etc/mongod2.conf #启动
MONGO--port 27018 #进入第二个节点
Rs.printreplicationinfo () #查看日志大小, time range
Exit
MONGO #进入主节点
Rs.stepdown () #有效产生选举; let go of the master node, the second becomes the master node
Deployment of Certified Replication
Use admin #在主节点中创建一个用户
Db.createuser ({"User": "Root", "pwd": "123", "Roles": ["Root"]})
Exit
vim/etc/mongod.conf #编辑配置文件; 4 need to add the following content
security: keyFile: /usr/bin/kgcrskey1 #秘钥文件位置 clusterAuthMode: keyFile #验证模式为秘钥验证模式
cd/usr/bin/#写入四个实例的秘钥文件 (content required)
echo "Kgcrs key" > Kgcrskey1
echo "Kgcrs key" > Kgcrskey2
echo "Kgcrs key" > Kgcrskey3
echo "Kgcrs key" > Kgcrskey4
chmod kgcrskey{1..4}
#四个实例依次进行重启, and enter the master node to verify;
Show DBS #无法查看数据库
Rs.status () #无法查看复制集
Use admin #身份登录验证
Db.auth ("root", "123")
Rs.status () #可以查看数据库
Show DBS #可以查看复制集
MongoDB Replication Set and management