每天一篇,深入学习MongoDB复制集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天一篇,深入学习MongoDB复制集相关的知识,希望对你有一定的参考价值。
复制集概念:
复制集是额外的数据副本,是跨多个服务器同步数据的过程,提供了冗余并增加了数据的可用性,通过它可以对硬件故障和中断的服务进行数据恢复
复制集工作原理:
MongoDB复制集最少需要两个节点。
主节点:负责处理客户端的请求,
从节点:负责复制主节点上的数据
搭配方式:一主一从或一主多从
注:客户端在主节点写入数据,在从节点读取数据,主从进行数据交互,保证数据的一致性
MongoDB复制集部署
(1)配置复制集
[[email protected] ~]# mkdir -p /data/moongodb/mongodb{2,3,4} //创建多实例
[[email protected] ~]# cd /data/mongodb/
[[email protected] mongodb]#
mongo mongod2.log mongodb2 mongodb3 mongodb4
[[email protected] mongodb]# mkdir logs
[[email protected] mongodb]# ls
logs mongo mongodb2 mongodb3 mongodb4
[[email protected] mongodb]# touch logs/mongodb{2,3,4}.log
[[email protected] mongodb]# cd logs/
[[email protected] logs]# ls
mongodb2.log mongodb3.log mongodb4.log
[[email protected] logs]# chmod 777 *.log //赋予最大权限
[[email protected] logs]# ls
mongodb2.log mongodb3.log mongodb4.log
[[email protected] logs]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb2.log
-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb3.log
-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb4.log
(2)编辑4个MongoDB实例的配置文件
[[email protected] etc]# vim mongod.conf
replication: //注释此项
replSetName: root123 //添加复制集名称(自取),注意前面空两格
[[email protected] etc]# mongod -f /etc/mongod.conf --shutdown //关闭服务
killing process with pid: 1084
[[email protected] etc]# mongod -f /etc/mongod.conf //开启服务
about to fork child process, waiting until server is ready for connections.
forked process: 11329
child process started successfully, parent exiting
注:修改配置文件后需重启服务,文件生效
[[email protected] etc]# cp -p mongod.conf mongod2.conf
cp:是否覆盖"mongod2.conf"? y
[[email protected] etc]# vim mongod2.conf
path:/data/mongodb/logs/mongodb2.log //日志存放位置
dbpath:/data/mongodb/mongodb2 //数据存放位置
port: //修改端口号,端口号不能相同
[[email protected] etc]# cp -p mongod2.conf mongod3.conf
[[email protected] etc]# cp -p mongod2.conf mongod4.conf
[[email protected] etc]# vim mongod3.conf //改序号和端口号即可,其它和2相同配置
[[email protected] etc]# vim mongod4.conf
[[email protected] etc]# mongod -f /etc/mongod2.conf
[[email protected] etc]# mongod -f /etc/mongod3.conf
[[email protected] etc]# mongod -f /etc/mongod4.conf
[[email protected] etc]# netstat -antp | grep mongod //四台实例都已开启
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 11599/mongod
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 11627/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11459/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 10252/mongod
(3)配置三个节点的复制集
[[email protected] etc]# systemctl stop firewalld.service //关闭防火墙
[[email protected] etc]# setenforce 0
[[email protected] etc]#mongo
> show dbs
> cfg={"_id":"root123","members":[{"_id":0,"host":"192.168.200.184:27017"},{"_id":1,"host":"192.168.200.184:27018"},{"_id":2,"host":"192.168.200.184:27019"}]} //配置节点复制集
{
"_id" : "root123",
"members" : [
{
"_id" : 0,
"host" : "192.168.200.184:27017"
},
{
"_id" : 1,
"host" : "192.168.200.184:27018"
},
{
"_id" : 2,
"host" : "192.168.200.184:27019"
}
]
}
> db.stats()
{
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1, //状态ok=1,复制成功
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
123:SECONDARY> rs.status() //查看复制集状态
{
"set" : "root123",
"date" : ISODate("2018-07-17T03:24:03.253Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.200.184:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //状态1为主
"uptime" : 980,
"optime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-17T03:24:00Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1531797808, 1),
"electionDate" : ISODate("2018-07-17T03:23:28Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.200.184:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //状态2为从
"uptime" : 45,
"optime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-17T03:24:00Z"),
"optimeDurableDate" : ISODate("2018-07-17T03:24:00Z"),
"lastHeartbeat" : ISODate("2018-07-17T03:24:02.633Z"),
"lastHeartbeatRecv" : ISODate("2018-07-17T03:24:02.920Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.184:27017",
"syncSourceHost" : "192.168.200.184:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.200.184:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //状态2为从
"uptime" : 45,
"optime" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531797840, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-17T03:24:00Z"),
"optimeDurableDate" : ISODate("2018-07-17T03:24:00Z"),
"lastHeartbeat" : ISODate("2018-07-17T03:24:02.633Z"),
"lastHeartbeatRecv" : ISODate("2018-07-17T03:24:02.896Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.184:27017",
"syncSourceHost" : "192.168.200.184:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1531797840, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531797840, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
root123:PRIMARY> //最后显示此状态
注:初始化配置时保证从节点没有数据
(4)添加和删除节点
root123:PRIMARY> rs.add("192.168.200.184:27020")
{
"ok" : 1,
"operationTime" : Timestamp(1531799035, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531799035, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
root123:PRIMARY> rs.stats()
"_id" : 3,
"name" : "192.168.200.184:27020", //添加成功
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //也为从节点
"uptime" : 26,
"optime" : {
"ts" : Timestamp(1531799060, 1),
"t" : NumberLong(1)
},
root123:PRIMARY> rs.remove("192.168.200.184:27020")
{
"ok" : 1, //删除成功
"operationTime" : Timestamp(1531799257, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531799257, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
(5)模拟故障自动转移
[[email protected] mongodb]# ps aux | grep mongod
root 12342 1.3 5.8 1465664 58768 ? Sl 11:07 0:38 mongod -f /etc/mongod3.conf
root 12387 1.0 5.9 1442988 59124 ? Sl 11:07 0:29 mongod -f /etc/mongod4.conf
root 12428 1.4 6.4 1582772 64516 ? Sl 11:07 0:40 mongod -f /etc/mongod.conf
root 12667 1.5 6.2 1459800 62268 ? Sl 11:17 0:35 mongod -f /etc/mongod2.conf
root 13655 0.0 0.0 112676 984 pts/0 S+ 11:55 0:00 grep --color=auto mongod
[[email protected] mongodb]# kill -9 12428
[[email protected] mongodb]# mongo
root123:SECONDARY> rs.status()
"members" : [
{
"_id" : 0,
"name" : "192.168.200.184:27017",
"health" : 0, //第一台健康值为0,已经不存在
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
{
"_id" : 2,
"name" : "192.168.200.184:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //27019跳为主节点
"uptime" : 2039,
"optime" : {
"ts" : Timestamp(1531799828, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1531799828, 1),
"t" : NumberLong(2)
},
多学多练,快乐无限~!!!
以上是关于每天一篇,深入学习MongoDB复制集的主要内容,如果未能解决你的问题,请参考以下文章