MongoDB故障测试

导读:本篇文章讲解 MongoDB故障测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

副本节点故障测试

关闭27018副本节点
发现,主节点和仲裁节点对27018的心态失败。因为主节点还在,因此,没有触发投票选举。
如果此时,在主节点写入数据:

# 主节点
myrs:PRIMARY> db.comment.insert({"_id":"1","articleid":"100001","content":"我们不应该把时间浪费在手机上","userid":"1002","nickname":"相忘于江湖","likenum":NumberInt(1000)})
WriteResult({ "nInserted" : 1 })

# 从节点
> db.comment.find()
Error: socket exception [CONNECT_ERROR] server [couldn't connect to server localhost:27018, connection attempt failed: SocketException: Error connecting to localhost:27018 (127.0.0.1:27018) :: caused by :: Connection refused]
>
# 启动从节点后,再次查询
> db.comment.find()
{ "_id" : ObjectId("608154d20a85cb7a99a65f93"), "articleid" : "100000", "content" : "今天天气真好,阳光明媚", "userid" : "1001", "nickname" : "rose", "createdatetime" : ISODate("2021-04-22T10:49:54.233Z") }
{ "_id" : "1", "articleid" : "100001", "content" : "我们不应该把时间浪费在手机上", "userid" : "1002", "nickname" : "相忘于江湖", "likenum" : 1000 }
>

再启动从节点,会发现,主节点写入的数据,会自动同步给从节点。

主节点故障测试

关闭27017节点。
发现,从节点和仲裁节点对27017的心态失败,当失败超过10秒,此时因为没有主节点,会自动发起投票。而副本节点只有27018,因此,候选人只有一个就是27018,开始投票。27019向27018投了一票,27018本身自带一票,因此共两票,超过了“大多数”。
27019是仲裁节点,没有选举权,27018不向器投票,其票数为0.最终结果,27018称为主节点。具备读写功能。

 exit
bye
[root@localhost gxm]# /usr/local/mongoDB/bin/mongo --host localhost --port 27018
MongoDB shell version v4.4.4
connecting to: mongodb://localhost:27018/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("834095b8-c4be-4936-beef-76923b139919") }
MongoDB server version: 4.4.4
---
The server generated these startup warnings when booting:
        2021-04-22T19:45:23.239+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-04-22T19:45:23.239+08:00: You are running this process as the root user, which is not recommended
        2021-04-22T19:45:23.239+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-04-22T19:45:23.239+08:00: Soft rlimits too low
        2021-04-22T19:45:23.239+08:00:         currentValue: 1024
        2021-04-22T19:45:23.239+08:00:         recommendedMinimum: 64000
        2021-04-22T19:45:23.260+08:00:
        2021-04-22T19:45:23.260+08:00: ** WARNING: This replica set has a Primary-Secondary-Arbiter architecture, but readConcern:majority is enabled
        2021-04-22T19:45:23.260+08:00: **          for this node. This is not a recommended configuration. Please see
        2021-04-22T19:45:23.260+08:00: **          https://dochub.mongodb.org/core/psa-disable-rc-majority
        2021-04-22T19:45:23.260+08:00:
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
myrs:PRIMARY>
myrs:PRIMARY>
myrs:PRIMARY>
myrs:PRIMARY> show dbs;
admin      0.000GB
articledb  0.000GB
config     0.000GB
local      0.001GB
myrs:PRIMARY>

在27018写入数据查看。

myrs:PRIMARY> db.comment.insert({"_id":"2","articleid":"100003","content":"主节点故障转移测试"})
WriteResult({ "nInserted" : 1 })

再启动27017节点,发现27017变成了从节点,27018仍保持主节点。
登录27017节点,发现是从节点了,数据自动从27018同步,从而实现了高可用。

[root@localhost mongodb]# /usr/local/mongoDB/bin/mongod -f ./replica_sets/myrs_27017/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4616
child process started successfully, parent exiting
[root@localhost mongodb]# /usr/local/mongoDB/bin/mongo --host=localhost --port=27017
MongoDB shell version v4.4.4
connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("14ca410e-ae49-4916-b1ec-22af11cbd057") }
MongoDB server version: 4.4.4
myrs:SECONDARY>
myrs:SECONDARY> show dbs;
uncaught exception: Error: listDatabases failed:{
        "topologyVersion" : {
                "processId" : ObjectId("60816459575dcb1284790a3d"),
                "counter" : NumberLong(4)
        },
        "operationTime" : Timestamp(1619092577, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotPrimaryNoSecondaryOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1619092577, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
myrs:SECONDARY> use articledb
switched to db articledb
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> db.comment.find()
{ "_id" : ObjectId("608154d20a85cb7a99a65f93"), "articleid" : "100000", "content" : "今天天气真好,阳光明媚", "userid" : "1001", "nickname" : "rose", "createdatetime" : ISODate("2021-04-22T10:49:54.233Z") }
{ "_id" : "1", "articleid" : "100001", "content" : "我们不应该把时间浪费在手机上", "userid" : "1002", "nickname" : "相忘于江湖", "likenum" : 1000 }
{ "_id" : "2", "articleid" : "100003", "content" : "主节点故障转移测试" }
myrs:SECONDARY>

仲裁节点和主节点故障

先关掉仲裁节点27019。
关掉现有的主节点27018。
登录27017后,发现,27017仍然是从节点,副本集中没有主节点了,导致此时,副本集是只读状态,无法写入。
为啥选举不了了?因为27017的票数,没有获得大多数,集没有答应等于2,它只有默认的一票(有先级为1)。
如果要触发选举,随便加入一个成员即可。

  • 如果只加入27019仲裁节点成员,则主节点一定是27017,因为没的选了,仲裁节点不参与选举,但参与投票。
  • 如果只加入27018节点,会发起选举。因为27017和27018都是两票,则按照谁数据新,谁当选主节点。

仲裁节点和从节点故障

先关掉仲裁节点27019。
关掉现在的副本节点27018。
10秒后,27017主节点自动降级为副本节点。(服务降级)
副本集不可写数据了,已经故障了。(因为没有冗余的副本节点存在了)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100345.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!