Mongodb5节点异地两中心故障转移恢复测试案例
架构方式:5节点,主中心(2数据1仲裁),备中心(1数据1仲裁)
1基本情况
操作系统:Red Hat Enterprise Linux Server release 6.3 (Santiago)
Mongodb版本:db version v3.6.3
Mongodb架构:
Ip,端口规划
"hosts" : [##数据节点
"*114:28001",#主中心
"*114:28002",#主中心
"*114:28004"#备份中心
],
"arbiters" : [##仲裁节点
"*114:28003",#主中心
"*114:28005"#备份中心
],
2 mongodb
配置文件,其他配置文件28001替换为28002~28007
注意相应的data,log等数据目录要存在,记住,所有的mongodb里面执行的命令,都要有返回ok为1才成功。
[root@mysqlt1 ~]# cat /data/mongodb/conf/28001.conf
port=28001
bind_ip=*
logpath=/data/mongodb/log/28001.log
dbpath=/data/mongodb/data/28001/
logappend=true
pidfilepath=/data/mongodb/28001.pid
fork=true
oplogSize=1024
replSet=MyMongo
[root@mysqlt1 conf]# ll /data/mongodb/conf/
total 32
-rw-r--r-- 1 root root 192 Oct 16 02:48 28001.conf
-rw-r--r-- 1 root root 225 Oct 16 07:21 28002.conf
-rw-r--r-- 1 root root 192 Oct 16 02:48 28003.conf
-rw-r--r-- 1 root root 192 Oct 11 03:37 28004.conf
-rw-r--r-- 1 root root 192 Oct 11 03:38 28005.conf
-rw-r--r-- 1 root root 192 Oct 16 07:18 28006.conf
-rw-r--r-- 1 root root 192 Oct 16 08:15 28007.conf
启动5个节点
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28003.conf
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28004.conf
[root@mysqlt1 data]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28005.conf
3 测试主中心全部宕机的情况
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28001.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28002.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/28003.conf
只剩备份中心的2个节点,这时候变为secondry,备份中心只能读取,不能写入
两种解决方案
1 把备份中心的secondry节点,作为单节点,去掉参数replSet重新启动,可以继续使用,但是由于单节点缺少oplog,后面主中心恢复,备份中心的数据不能恢复到整个副本集中,可以考虑备份方式(复杂,这里还有第二种方式)。
2 在备份中心,启动2个新的仲裁节点,强制加入副本集,使secondry节点变为primary节点,详细的操作方式
一:启动备份中心的2个新节点(28006,28007)
二:在备份中心的secondry节点,重新配置副本集,加入2个仲裁节点
MyMongo:SECONDARY> use admin
MyMongo:SECONDARY> config = {
"_id":"MyMongo",
members:[
{"_id":0,host:"*:28001"},
{"_id":1,host:"*:28002"},
{"_id":2,host:"*:28003", arbiterOnly: true},
{"_id":3,host:"*:28004"},
{"_id":4,host:"*:28005", arbiterOnly: true},
{"_id":5,host:"*:28006", arbiterOnly: true},
{"_id":6,host:"*:28007", arbiterOnly: true}]
}
MyMongo:SECONDARY> rs.reconfig(config,{force:true});
MyMongo:PRIMARY> rs.status() #查看副本集的状态,及各节点的状态
MyMongo:PRIMARY> db.isMaster()
在client端批量插入数据(简单的程序),这里可以配置集群方式连接,也可以指定主节点的方式进行插入,这里是直接指定主节点
#coding:utf-8
import time
from pymongo import MongoClient
#conn = MongoClient()
# keyword argument
conn = MongoClient('*', 28004)
# MongoDB URI
#conn = MongoClient('mongodb://localhost:27017/')
#from pymongo import ReplicaSetConnection
#conn = ReplicaSetConnection("*:28001,*:28002,*:28004", replicaSet="MyMongo", read_preference=2, safe=True) for i in xrange(100):
try:
conn.test.tt3.insert({"name":"test" + str(i)})
time.sleep(1)
print conn.primary
print conn.secondaries
except:
pass
主节点执行,100条
MyMongo:PRIMARY> db.tt3.find().count()
100
启动主中心的3个节点
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28001.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28002.conf
[root@mysqlt1 conf]# /usr/local/mongodb/bin/mongod -f /data/mongodb/conf/28003.conf
[root@mysqlt1 ~]# /usr/local/mongodb/bin/mongo *:28002/admin
MyMongo:SECONDARY> rs.slaveOk(true)
MyMongo:SECONDARY> use test;
switched to db test
MyMongo:SECONDARY> db.tt3.find().count() #数据同步成功
100
之前的5个节点,现在变成了7个节点,删除新加的2个仲裁节点
MyMongo:PRIMARY> rs.remove("*:28007");
MyMongo:PRIMARY> rs.remove("*:28006");
MyMongo:PRIMARY> db.isMaster() #变回之前的5个节点,1主,2secondry,2仲裁
{
"hosts" : [
"*:28001",
"*:28002",
"*:28004"
],
"arbiters" : [
"*:28003",
"*:28005"
MyMongo:PRIMARY> rs.status()
{
"set" : "MyMongo",
"date" : ISODate("2018-10-16T18:16:15.512Z"),
"myState" : 1,
"term" : NumberLong(7),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"appliedOpTime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"durableOpTime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
}
},
"members" : [
{
"_id" : 0,
"name" : "*:28001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 237,
"optime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"optimeDurable" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2018-10-16T18:16:06Z"),
"optimeDurableDate" : ISODate("2018-10-16T18:16:06Z"),
"lastHeartbeat" : ISODate("2018-10-16T18:16:13.929Z"),
"lastHeartbeatRecv" : ISODate("2018-10-16T18:16:14.928Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "*:28004",
"configVersion" : 102086
},
{
"_id" : 1,
"name" : "*:28002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 269,
"optime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"optimeDurable" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2018-10-16T18:16:06Z"),
"optimeDurableDate" : ISODate("2018-10-16T18:16:06Z"),
"lastHeartbeat" : ISODate("2018-10-16T18:16:13.929Z"),
"lastHeartbeatRecv" : ISODate("2018-10-16T18:16:14.928Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "*:28004",
"configVersion" : 102086
},
{
"_id" : 2,
"name" : "*:28003",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 193,
"lastHeartbeat" : ISODate("2018-10-16T18:16:13.929Z"),
"lastHeartbeatRecv" : ISODate("2018-10-16T18:16:11.917Z"),
"pingMs" : NumberLong(0),
"configVersion" : 102086
},
{
"_id" : 3,
"name" : "*:28004",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 68054,
"optime" : {
"ts" : Timestamp(1539713766, 1),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2018-10-16T18:16:06Z"),
"electionTime" : Timestamp(1539712874, 1),
"electionDate" : ISODate("2018-10-16T18:01:14Z"),
"configVersion" : 102086,
"self" : true
},
{
"_id" : 4,
"name" : "*:28005",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 66987,
"lastHeartbeat" : ISODate("2018-10-16T18:16:13.929Z"),
"lastHeartbeatRecv" : ISODate("2018-10-16T18:16:11.921Z"),
"pingMs" : NumberLong(0),
"configVersion" : 102086
}
],
"ok" : 1,
"operationTime" : Timestamp(1539713766, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1539713766, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}