安装:linux 下指定配置文件安装mongodb
1、复制集replica sets
1.1 什么是复制集?
复制集是由一组拥有相同数据集的mongod实例做组成的集群。 复制集是一个集群,它是2台及2台以上的服务器组成,以及复制集成员包括Primary主节点,secondary从节点和投票节点。 复制集提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,保证数据的安全性。
1.2 为什么要使用复制集
1.高可用 防止设备(服务器、网络)故障。 提供自动failover 功能。 技术来保证高可用 2.灾难恢复 当发生故障时,可以从其他节点恢复 用于备份。 3.功能隔离 我们可以在备节点上执行读操作,减少主节点的压力 比如:用于分析、报表,数据挖掘,系统任务等。
2 复制集搭建
注:如果之前安装过mongon ,请先停掉
[root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# ./bin/mongod --shutdown --dbpath /home/mongodb-linux-x86_64-amazon-3.6.21/data/mongo
步骤:
//新建文件夹 mkdir replica_sets //copy cp cp mongodb-linux-x86_64-amazon-3.6.21.tgz replica_sets/ cd replica_sets/ //解压 tar -xvf mongodb-linux-x86_64-amazon-3.6.21.tgz
节点配置
[root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# vi mongo_37017.conf [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# cp mongo_37017.conf mongo_37018.conf [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# cp mongo_37018.conf mongo_37019.conf [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# mkdir /data/mongo/data/server1 -p [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# mkdir /data/mongo/data/server2 -p [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# mkdir /data/mongo/data/server3 -p [root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# mkdir /data/mongo/logs
1.主节点配置 mongo_37017.conf
# 主节点配置 dbpath=/data/mongo/data/server1 bind_ip=0.0.0.0 port=37017 fork=true logpath=/data/mongo/logs/server37017.log replSet=wgCluster
2.从节点1配置 mongo_37018.conf
# 从节点配置 dbpath=/data/mongo/data/server2 bind_ip=0.0.0.0 port=37018 fork=true logpath=/data/mongo/logs/server37018.log replSet=wgCluster
3.从节点2配置 mongo_37019.conf
dbpath=/data/mongo/data/server3 bind_ip=0.0.0.0 port=37019 fork=true logpath=/data/mongo/logs/server37019.log replSet=wgCluster
4.初始化节点配置
//启动节点
[root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# ./bin/mongod -f mongo_37017.conf about to fork child process, waiting until server is ready for connections. forked process: 10653
//进入节点
[root@VM_0_4_centos mongodb-linux-x86_64-amazon-3.6.21]# ./bin/mongo --port 37017
启动三个节点 然后进入任意一个节点 运行如下命令:
var cfg ={"_id":"lagouCluster", "protocolVersion" : 1, "members":[ {"_id":1,"host":"主机ip:37017","priority":10}, {"_id":2,"host":"主机ip:37018"} ] } rs.initiate(cfg) rs.status()
5.节点的动态增删,上边我们只添加了俩个节点,下边动态的增加删除节点。
增加节点 rs.add("192.168.211.133:37019") 删除slave 节点 rs.remove("192.168.211.133:37019")
查看节点状态
wgCluster:PRIMARY> rs.status() { "set" : "wgCluster", "date" : ISODate("2020-12-24T01:19:35.883Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 1, "name" : "152.136.193.58:37017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 57238, "optime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-12-24T01:19:32Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1608715848, 1), "electionDate" : ISODate("2020-12-23T09:30:48Z"), "configVersion" : 2, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "152.136.193.58:37018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 56937, "optime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-12-24T01:19:32Z"), "optimeDurableDate" : ISODate("2020-12-24T01:19:32Z"), "lastHeartbeat" : ISODate("2020-12-24T01:19:33.896Z"), "lastHeartbeatRecv" : ISODate("2020-12-24T01:19:34.917Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "152.136.193.58:37017", "syncSourceHost" : "152.136.193.58:37017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 2 }, { "_id" : 3, "name" : "152.136.193.58:37019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 13, "optime" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1608772772, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-12-24T01:19:32Z"), "optimeDurableDate" : ISODate("2020-12-24T01:19:32Z"), "lastHeartbeat" : ISODate("2020-12-24T01:19:33.897Z"), "lastHeartbeatRecv" : ISODate("2020-12-24T01:19:35.214Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "152.136.193.58:37017", "syncSourceHost" : "152.136.193.58:37017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 2 } ], "ok" : 1, "operationTime" : Timestamp(1608772772, 1), "$clusterTime" : { "clusterTime" : Timestamp(1608772772, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
6.复制集操作演示
进入主节点 ----- 插入数据 ------ 进入从节点验证
注意:默认节点下从节点不能读取数据。调用 rs.slaveOk() 解决
为了保证高可用,在集群当中如果主节点挂掉后,会自动 在从节点中选举一个 重新做为主节点。
rs.status()
节点说明:
PRIMARY 主节点: 可以查询和新增数据
SECONDARY 从节点:只能查询 不能新增 基于priority 权重可以被选为主节点
ARBITER 仲裁节点: 不能查询数据 和新增数据 ,不能变成主节点
7.主从自动切换操作演示
现在我们起俩个窗口,一主一从(37017主机,37018从机),新开启窗口干掉主节点。。验证从节点是否会变成主节点。发现从节点37018变成主节点,,当再次启动节点37017节点,因优先界别高,37017会再次变为主节点。
8.添加冲裁节点
注:新建配置文件mongo_37020.conf,并建立server4 文件夹,启动37020节点
#仲裁节点 dbpath=/data/mongo/data/server4 bind_ip=0.0.0.0 port=37020 fork=true logpath=/data/mongo/logs/server37020.log replSet=wgCluster
配置方式
var cfg ={"_id":"wgCluster", "protocolVersion" : 1, "members":[ {"_id":1,"host":"152.136.193.58:37017","priority":10}, {"_id":2,"host":"152.136.193.58:37018","priority":0}, {"_id":3,"host":"152.136.193.58:37019","priority":5}, {"_id":4,"host":"152.136.193.58:37020","arbiterOnly":true} ] }; // 重新装载配置,并重新生成集群节点。 rs.reconfig(cfg) //重新查看集群状态 rs.status()
动态添加
和上面的配置步骤相同 只是增加了 一个特殊的仲裁节点 注入节点 执行 rs.addArb("IP:端口"); rs.addArb("192.168.211.133:37020")
9复制集成员的配置参数