【Mongodb】 Replica set的主从切换测试

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
日志服务 SLS,月写入数据量 50GB 1个月
简介:
Replica set 为我们提供了自动故障切换功能,这个机制是由mongodb自己来操作的,它根据从库的优先级或者数据新鲜度(也就是最新的从主库同步数据的那个节点)来选择primary,而当以前的primary起来之后,会成为secondary ,接受新的primary 的日志。
                                                               完整的replica sets

                                                              primary 当机
                   mongodb 会根据数据的新鲜度来选择下一个主库

接上一篇文章,搭建好了replica set,查看端口为 27018 27020两个服务的状态:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27018/test
PRIMARY> db.isMaster();
{
        "setName" : "myset",
        "ismaster" : true,  --为主库
        "secondary" : false,
        "hosts" : [
                "10.250.7.220:27018",
                "10.250.7.220:27020",
                "10.250.7.220:27019"
        ],
        "primary" : "10.250.7.220:27018",
        "me" : "10.250.7.220:27018",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
PRIMARY> exit
bye
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
SECONDARY> 
SECONDARY> db.isMaster();
{
        "setName" : "myset",
        "ismaster" : false,
        "secondary" : true, --为从库
        "hosts" : [
                "10.250.7.220:27020",
                "10.250.7.220:27019",
                "10.250.7.220:27018"
        ],
        "primary" : "10.250.7.220:27018",
        "me" : "10.250.7.220:27020",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}

PRIMARY> 手工杀掉primary 
[root@rac4 ~]# ps -ef | grep 27018 
mongodb  14826 14794  1 20:24 pts/4    00:00:05 ./mongod --dbpath /opt/mongodata/r1 --port 27018 --replSet myset --rest
mongodb  14999 14430  0 20:28 pts/2    00:00:00 ./mongo 127.0.0.1:27018
[root@rac4 ~]# kill -9 14826 14794
[root@rac4 ~]# ps -ef | grep mongodb |grep -v root
mongodb  14883 14853  1 20:26 pts/7    00:00:05 ./mongod --dbpath /opt/mongodata/r2 --port 27019 --replSet myset --rest
mongodb  14901 14548  1 20:27 pts/6    00:00:07 ./mongod --dbpath /opt/mongodata/r3 --port 27020 --replSet myset --rest
mongodb  14999 14430  0 20:28 pts/2    00:00:00 ./mongo 127.0.0.1:27018
mongodb  15102 15072  0 20:30 pts/5    00:00:00 ./mongo 127.0.0.1:27019
mongodb  15136 15106  0 20:30 pts/8    00:00:00 ./mongo 127.0.0.1:27020
[root@rac4 ~]# 
27019 端口的mongodb 输出日志显示的选择10.250.7.220 作为主库的日志记录
Mon Oct 31 20:27:59 [FileAllocator] allocating new datafile /opt/mongodata/r2/local.2, filling with zeroes...
Mon Oct 31 20:27:59 [rsHealthPoll] replSet info member 10.250.7.220:27018 is up
Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state SECONDARY
Mon Oct 31 20:27:59 [rsHealthPoll] replSet info 10.250.7.220:27020 is down (or slow to respond): still initializing
Mon Oct 31 20:27:59 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state DOWN
Mon Oct 31 20:28:01 [initandlisten] connection accepted from 10.250.7.220:10857 #3
Mon Oct 31 20:28:05 [conn2] replSet RECOVERING
Mon Oct 31 20:28:05 [conn2] replSet info voting yea for 10.250.7.220:27018 (0)
Mon Oct 31 20:28:07 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state PRIMARY
Mon Oct 31 20:28:09 [FileAllocator] done allocating datafile /opt/mongodata/r2/local.2, size: 1024MB,  took 10.89 secs
Mon Oct 31 20:28:10 [rsSync] ******
Mon Oct 31 20:28:10 [rsSync] replSet initial sync pending
Mon Oct 31 20:28:10 [rsSync] replSet syncing to: 10.250.7.220:27018
Mon Oct 31 20:28:10 [rsSync] build index local.me { _id: 1 }
Mon Oct 31 20:28:10 [rsSync] build index done 0 records 0.001 secs
Mon Oct 31 20:28:10 [rsSync] replSet initial sync drop all databases
Mon Oct 31 20:28:10 [rsSync] dropAllDatabasesExceptLocal 1
Mon Oct 31 20:28:10 [rsSync] replSet initial sync clone all databases
Mon Oct 31 20:28:10 [rsSync] replSet initial sync query minValid
Mon Oct 31 20:28:10 [rsSync] replSet initial oplog application from 10.250.7.220:27018 starting at Oct 31 20:27:53:1 to Oct 31 20:27:53:1
Mon Oct 31 20:28:13 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up
Mon Oct 31 20:28:13 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state STARTUP2
Mon Oct 31 20:28:14 [rsSync] replSet initial sync finishing up
Mon Oct 31 20:28:14 [rsSync] replSet set minValid=4eae9449:1
Mon Oct 31 20:28:14 [rsSync] build index local.replset.minvalid { _id: 1 }
Mon Oct 31 20:28:14 [rsSync] build index done 0 records 0.005 secs
Mon Oct 31 20:28:14 [rsSync] replSet initial sync done
Mon Oct 31 20:28:15 [rsSync] replSet syncing to: 10.250.7.220:27018
Mon Oct 31 20:28:15 [rsSync] replSet SECONDARY
Mon Oct 31 20:28:15 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state RECOVERING
Mon Oct 31 20:28:26 [clientcursormon] mem (MB) res:16 virt:2677 mapped:1232
Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10872 #4
Mon Oct 31 20:28:52 [initandlisten] connection accepted from 10.250.7.220:10873 #5
Mon Oct 31 20:28:52 [rsGhostSync] handshake between 2 and 10.250.7.220:27018
Mon Oct 31 20:28:53 [slaveTracking] build index local.slaves { _id: 1 }
Mon Oct 31 20:28:53 [slaveTracking] build index done 0 records 0.003 secs
Mon Oct 31 20:28:55 [conn5] end connection 10.250.7.220:10873
Mon Oct 31 20:28:55 [conn4] end connection 10.250.7.220:10872
Mon Oct 31 20:28:57 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state SECONDARY
Mon Oct 31 20:29:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232
Mon Oct 31 20:30:21 [initandlisten] connection accepted from 127.0.0.1:44672 #6
Mon Oct 31 20:33:35 [conn2] end connection 10.250.7.220:42493
Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018
Mon Oct 31 20:33:35 [rsHealthPoll] DBClientCursor::init call() failed
Mon Oct 31 20:33:35 [rsHealthPoll] replSet info 10.250.7.220:27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220:27018 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.220:27019" }
Mon Oct 31 20:33:35 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN
Mon Oct 31 20:33:35 [rsMgr] not electing self, 10.250.7.220:27020 would veto
Mon Oct 31 20:33:36 [conn3] replSet info voting yea for 10.250.7.220:27020 (2)
Mon Oct 31 20:33:37 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY
Mon Oct 31 20:33:46 [rsSync] replSet syncing to: 10.250.7.220:27020
Mon Oct 31 20:34:27 [clientcursormon] mem (MB) res:19 virt:2693 mapped:1232

27020 端口的mongodb 输出日志显示的选择10.250.7.220 作为主库的日志记录
Mon Oct 31 20:33:35 [rsSync] replSet syncThread: 10278 dbclient error communicating with server: 10.250.7.220:27018
Mon Oct 31 20:33:36 [rsHealthPoll] DBClientCursor::init call() failed
Mon Oct 31 20:33:36 [rsHealthPoll] replSet info 10.250.7.220:27018 is down (or slow to respond): DBClientBase::findN: transport error: 10.250.7.220:27018 query: { replSetHeartbeat: "myset", v: 1, pv: 1, checkEmpty: false, from: "10.250.7.220:27020" }
Mon Oct 31 20:33:36 [rsHealthPoll] replSet member 10.250.7.220:27018 is now in state DOWN
Mon Oct 31 20:33:36 [rsMgr] replSet info electSelf 2
Mon Oct 31 20:33:36 [rsMgr] replSet PRIMARY
Mon Oct 31 20:33:46 [initandlisten] connection accepted from 10.250.7.220:37261 #5
Mon Oct 31 20:33:47 [slaveTracking] build index local.slaves { _id: 1 }
Mon Oct 31 20:33:47 [slaveTracking] build index done 0 records 0.001 secs
Mon Oct 31 20:33:48 [clientcursormon] mem (MB) res:19 virt:2692 mapped:1232
Mon Oct 31 20:34:35 [conn4] end connection 127.0.0.1:17500
Mon Oct 31 20:34:37 [initandlisten] connection accepted from 127.0.0.1:36525 #6
进入数据库查看:
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27020
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27020/test
PRIMARY> 
PRIMARY> 
PRIMARY> db.isMaster();
{
        "setName" : "myset",
        "ismaster" : true,--成为主库master
        "secondary" : false,
        "hosts" : [
                "10.250.7.220:27020",
                "10.250.7.220:27019",
                "10.250.7.220:27018"
        ],
        "primary" : "10.250.7.220:27020",
        "me" : "10.250.7.220:27020",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
PRIMARY> 
重新启动端口为27018的mongodb的数据库服务:从日志中可以看出其进行恢复的操作记录
[mongodb@rac4 bin]$ ./mongod --dbpath /opt/mongodata/r1 --port 27018  --rest --replSet myset &
[1] 16290
[mongodb@rac4 bin]$ Mon Oct 31 20:48:32 [initandlisten] MongoDB starting : pid=16290 port=27018 dbpath=/opt/mongodata/r1 64-bit host=rac4
Mon Oct 31 20:48:32 [initandlisten] db version v2.0.1, pdfile version 4.5
Mon Oct 31 20:48:32 [initandlisten] git version: 3a5cf0e2134a830d38d2d1aae7e88cac31bdd684
Mon Oct 31 20:48:32 [initandlisten] build info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_41
Mon Oct 31 20:48:32 [initandlisten] options: { dbpath: "/opt/mongodata/r1", port: 27018, replSet: "myset", rest: true }
Mon Oct 31 20:48:32 [initandlisten] journal dir=/opt/mongodata/r1/journal
Mon Oct 31 20:48:32 [initandlisten] recover begin
Mon Oct 31 20:48:32 [initandlisten] recover lsn: 231055
Mon Oct 31 20:48:32 [initandlisten] recover /opt/mongodata/r1/journal/j._0
Mon Oct 31 20:48:32 [initandlisten] recover skipping application of section seq:198962 < lsn:231055
Mon Oct 31 20:48:32 [initandlisten] recover cleaning up
Mon Oct 31 20:48:32 [initandlisten] removeJournalFiles
Mon Oct 31 20:48:32 [initandlisten] recover done
Mon Oct 31 20:48:32 [initandlisten] waiting for connections on port 27018
Mon Oct 31 20:48:32 [websvr] admin web console waiting for connections on port 28018
Mon Oct 31 20:48:32 [initandlisten] connection accepted from 127.0.0.1:11930 #1
Mon Oct 31 20:48:32 [rsStart] replSet STARTUP2
Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27019 is up
Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27019 is now in state SECONDARY
Mon Oct 31 20:48:32 [rsHealthPoll] replSet info member 10.250.7.220:27020 is up
Mon Oct 31 20:48:32 [rsHealthPoll] replSet member 10.250.7.220:27020 is now in state PRIMARY
Mon Oct 31 20:48:32 [rsSync] replSet SECONDARY
Mon Oct 31 20:48:33 [initandlisten] connection accepted from 10.250.7.220:35971 #2
Mon Oct 31 20:48:34 [initandlisten] connection accepted from 10.250.7.220:35972 #3
Mon Oct 31 20:48:36 [rsSync] replSet syncing to: 10.250.7.220:27020
Mon Oct 31 20:48:36 [rsSync] build index local.me { _id: 1 }
Mon Oct 31 20:48:36 [rsSync] build index done 0 records 0 secs
[mongodb@rac4 bin]$ ./mongo 127.0.0.1:27018
MongoDB shell version: 2.0.1
connecting to: 127.0.0.1:27018/test
SECONDARY> 
SECONDARY> db.isMaster();
{
        "setName" : "myset",
        "ismaster" : false,   --端口为 27018的数据库服务变为从库
        "secondary" : true,
        "hosts" : [
                "10.250.7.220:27018",
                "10.250.7.220:27020",
                "10.250.7.220:27019"
        ],
        "primary" : "10.250.7.220:27020",
        "me" : "10.250.7.220:27018",
        "maxBsonObjectSize" : 16777216,
        "ok" : 1
}
SECONDARY> 
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
NoSQL Cloud Native MongoDB
MongoDB 主从集群 2
MongoDB 主从集群 2
|
3月前
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
33 0
|
3月前
|
Kubernetes 容器 Perl
在K8S中,Replica Set和Replication Controller之间有什么区别?
在K8S中,Replica Set和Replication Controller之间有什么区别?
|
6月前
|
NoSQL 测试技术 MongoDB
【MongoDB 专栏】MongoDB 的性能基准测试与评估
【5月更文挑战第11天】MongoDB的性能基准测试对于优化至关重要,涉及数据读写速度、查询响应时间及吞吐量等指标。测试应明确目标和范围,选择合适的工具,考虑数据模型、索引、查询优化和系统配置等因素。性能评估需关注读写吞吐量、响应时间和资源利用率。通过多次测试、逐步增加负载和对比其他系统,识别性能瓶颈并持续优化。随着技术发展,测试方法和工具将持续创新,以应对复杂性能挑战。
283 3
【MongoDB 专栏】MongoDB 的性能基准测试与评估
|
4月前
|
消息中间件 NoSQL 中间件
MongoDB多数据中心的主从结构
【7月更文挑战第3天】
42 0
|
4月前
|
消息中间件 NoSQL 中间件
MongoDB主从结构、仲裁节点
【7月更文挑战第2天】
61 0
|
6月前
|
DataWorks NoSQL 关系型数据库
DataWorks操作报错合集之在使用 DataWorks 进行 MongoDB 同步时遇到了连通性测试失败,实例配置和 MongoDB 白名单配置均正确,且同 VPC 下 MySQL 可以成功连接并同步,但 MongoDB 却无法完成同样的操作如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
101 1
|
存储 NoSQL 网络协议
MongoDB 主从集群 1
MongoDB 主从集群 1
|
6月前
|
监控 NoSQL 测试技术
MongoDB性能最佳实践:如何制定更有效的基准测试?
感谢你与我们一起走过这段MongoDB性能最佳实践之旅,希望你能从中获取一些有用的信息
1913 3
|
6月前
|
运维 NoSQL Linux
MongoDB详解(六)——MongoDB主从同步配置
MongoDB详解(六)——MongoDB主从同步配置
279 5