阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MongoDB 3分片部署及故障模拟验证

123次阅读
没有评论

共计 6566 个字符,预计需要花费 17 分钟才能阅读完成。

本文分两部分介绍 mongodb3.2.1 分片部署配置及故障模拟验证。

第一部分  安装配置

一、实验环境

两组副本集做分片

版本 3.2.1

副本集 1:192.168.115.11:27017,192.168.115.12:27017,192.168.115.11:47017(arbiter)

副本集 2:192.168.115.11:37017,192.168.115.12:37017,192.168.115.12:47017(arbiter)

configserver:192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000

mongos:192.168.115.11:20000

二、分片介绍

1. 逻辑图

MongoDB 3 分片部署及故障模拟验证

片(shard):每一个分片一个副本集

配置服务器(config server):存储集群的配置信息,3.2 以上版本支持副本集模式部署

路由进程(mongos):路由所有请求,然后将结果聚合。它不保存存储数据或配置信息,配置信息从配置服务器上加载到内存中。

副本集方式部署 confiserver

一、部署条件

1. 集群中不能有仲裁节点

2. 集群中不能有延迟节点

3. 每个成员必须可以创建索引

二、configserver 安装配置

1. 修改配置文件(其他两个节点配置文件类似,主要修改监听端口,及数据路径,如果一台机器上运行多个实例,注意配置文件名称要不一样)

cat config.conf

fork = true

quiet = true

port = 10000

dbpath = /data/config

logpath = /usr/local/mongodb/logs/config.log

logappend = true

directoryperdb = true

configsvr = true

replSet = hnrconfig/192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000

2. 服务启动和停止

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/config.conf

 /usr/local/mongodb/bin/mongod –shutdown –port 10000 –dbpath=/data/config

3. 配置副本集

连接任意一个节点进行配置

> show dbs

2016-11-17T09:06:08.088+0800 E QUERY    [thread1] Error: listDatabases failed:{“ok” : 0, “errmsg” : “not master and slaveOk=false”, “code” : 13435} :

_getErrorWithCode@src/mongo/shell/utils.js:23:13

Mongo.prototype.getDBs@src/mongo/shell/mongo.js:53:1

shellHelper.show@src/mongo/shell/utils.js:700:19

shellHelper@src/mongo/shell/utils.js:594:15

@(shellhelp2):1:1

出现以上错误,需要执行

> rs.slaveOk()

> use admin

> db.runCommand({“replSetInitiate” : { “_id” : “hnrconfig” ,”members” : [ { “_id” : 1, “host” : “192.168.115.11:10000”},{“_id” : 2, “host” : “192.168.115.12:10000”},{“_id” : 3, “host” : “192.168.115.11:10001”}]}})

{“ok” : 1}

三、mongos 配置

1. 配置文件

cat mongos.conf

fork = true

quiet = true

port = 20000

logpath = /usr/local/mongodb/logs/mongos.log

logappend = true

configdb = 192.168.115.11:10000,192.168.115.11:10001,192.168.115.12:10000

2. 启动 mongos 服务

/usr/local/mongodb/bin/mongos -f /usr/local/mongodb/etc/mongos.conf

四、往集群中添加分片

连接 mongos

mongos> sh.addShard(“hnrtest1/192.168.115.11:27017”)

{“shardAdded” : “hnrtest1”, “ok” : 1}

mongos>

mongos> sh.addShard(“hnrtest2/192.168.115.12:37017”)

{“shardAdded” : “hnrtest2”, “ok” : 1}

mongos>

MongoDB 3 分片部署及故障模拟验证

五、开启分片

1. 先对数据库启用分片功能

mongos> sh.enableSharding(“shardtest”)

{“ok” : 1}

mongos>

2. 对集合开启分片(自动分片建)

mongos> sh.shardCollection(“shardtest.student”,{“cre_id”:1})

{“collectionsharded” : “shardtest.student”, “ok” : 1}

mongos>

MongoDB 3 分片部署及故障模拟验证

3. 修改默认 chunk 大小(默认为 64M),自动分片测试效果不好,需要插入大量数据,将其修改为 1M

mongos> use config

mongos> db.settings.save({“_id” : “chunksize”, “value” : NumberLong(1) })

修改后对 student2 集合进行分片

mongos> sh.shardCollection(“shardtest.student2”,{“cre_id”:1})

插入 5 万条数据

MongoDB 3 分片部署及故障模拟验证

直接在后端分片副本集上查询

hnrtest2:PRIMARY> db.student2.find().count()

27081

hnrtest2:PRIMARY>

hnrtest1:PRIMARY> db.student2.find().count()

22918

hnrtest1:PRIMARY>

4. 采用哈希分片

修改 chunk 为默认值 64M

mongos> db.settings.save({“_id” : “chunksize”, “value” : NumberLong(64) })

student3 集合在 cre_id 字段使用哈希分片

mongos> sh.shardCollection(“shardtest.student3”,{“cre_id”:”hashed”})

{“collectionsharded” : “shardtest.student3”, “ok” : 1}

mongos> sh.status()

 shardtest.student3

                        shard key: {“cre_id” : “hashed”}

                        unique: false

                        balancing: true

                        chunks:

                                hnrtest1        2

                                hnrtest2        2

                        {“cre_id” : { “$minKey” : 1} } –>> {“cre_id” : NumberLong(“-4611686018427387902”) } on : hnrtest1 Timestamp(2, 2)

                        {“cre_id” : NumberLong(“-4611686018427387902”) } –>> {“cre_id” : NumberLong(0) } on : hnrtest1 Timestamp(2, 3)

                        {“cre_id” : NumberLong(0) } –>> {“cre_id” : NumberLong(“4611686018427387902”) } on : hnrtest2 Timestamp(2, 4)

                        {“cre_id” : NumberLong(“4611686018427387902”) } –>> {“cre_id” : { “$maxKey” : 1} } on : hnrtest2 Timestamp(2, 5)

 

往 student3 插入 1 万条数据,在每个分片上查询

hnrtest1:PRIMARY> db.student3.find().count()

4952

hnrtest1:PRIMARY>

hnrtest2:PRIMARY> db.student3.find().count()

5047

hnrtest2:PRIMARY>

第二部分  故障模拟验证

一、模拟 config 服务副本集 primary 节点宕机

1. 关闭服务

/usr/local/mongodb/bin/mongod –shutdown –port 10000 –dbpath=/data/config

2. 副本集重新选举一个 primary 节点

MongoDB 3 分片部署及故障模拟验证

3. 读取数据,所有数据均正常返回

mongos> use shardtest

switched to db shardtest

mongos>

mongos> db.student.find().count()

99999

mongos> db.student2.find().count()

49999

mongos> db.student3.find().count()

9999

mongos>

4. 对新的集合进行分片,插入 5 千条数据

mongos> sh.shardCollection(“shardtest.student4”,{“cre_id”:”hashed”})

{“collectionsharded” : “shardtest.student4”, “ok” : 1}

mongos>

在每个分片上查询数据

hnrtest2:PRIMARY> db.student4.find().count()

2525

hnrtest2:PRIMARY>

hnrtest1:PRIMARY> db.student4.find().count()

2474

hnrtest1:PRIMARY>

二、config 服务数据备份恢复

1. 数据备份

/usr/local/mongodb/bin/mongodump -h 192.168.115.11:10001 -o configdata

2. 关闭所有 config 服务节点

/usr/local/mongodb/bin/mongod –shutdown –port 10000 –dbpath=/data/config

/usr/local/mongodb/bin/mongod –shutdown –port 10001 –dbpath=/data/config1

3. 数据读取操作

由于 mongos 是将 config 的配置信息全部加载到内存中运行,因此此时通过 mongos 查询数据一切正常,但是不能对新的集合进行分片操作

mongos> db.student.find().count()

99999

mongos> db.student2.find().count()

49999

mongos> db.student3.find().count()

9999

mongos> db.student4.find().count()

4999

mongos>

4. 对集合进行分片操作,无法完成

mongos> sh.shardCollection(“shardtest.student5”,{“cre_id”:”hashed”})

{

        “ok” : 0,

        “errmsg” : “None of the hosts for replica set hnrconfig could be contacted.”,

        “code” : 71

}

mongos>

5. 关闭 mongos 服务,删除 config 节点所有数据

6. 重新启动三个 config 服务

7. 重新初始化副本集

> rs.slaveOk()

> use admin

> db.runCommand({“replSetInitiate” : { “_id” : “hnrconfig” ,”members” : [ { “_id” : 1, “host” : “192.168.115.11:10000”},{“_id” : 2, “host” : “192.168.115.12:10000”},{“_id” : 3, “host” : “192.168.115.11:10001”}]}})

8. 启动 mongos 服务,此时没有任何数据

MongoDB 3 分片部署及故障模拟验证

9. 导入备份的 config 数据

/usr/local/mongodb/bin/mongorestore -h 192.168.115.11:10000 -d config configdata/config/

在 mongos 查询,但是查询数据会出现超时,数据无法查询

MongoDB 3 分片部署及故障模拟验证

10. 在 mongos 执行如下命令

mongos> sh.enableSharding(“shardtest”)

{“ok” : 0, “errmsg” : “Operation timed out”, “code” : 50}

mongos 日志

2016-11-17T14:46:21.197+0800 I SHARDING [Balancer] about to log metadata event into actionlog: {_id: “node1.hnr.com-2016-11-17T14:46:21.197+0800-582d523ded1c4b679a84877b”, server: “node1.hnr.com”, clientAddr: “”, time: new Date(1479365181197), what: “balancer.round”, ns: “”, details: {executionTimeMillis: 30007, errorOccured: true, errmsg: “could not get updated shard list from config server due to ExceededTimeLimit Operation timed out”} }

官网上说是 bug,恢复失败

https://jira.mongodb.org/browse/SERVER-22392

更多 MongoDB 相关教程见以下内容

CentOS 编译安装 MongoDB 与 mongoDB 的 php 扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm

CentOS 6 使用 yum 安装 MongoDB 及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm

Ubuntu 13.04 下安装 MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm

MongoDB 入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm

Ubunu 14.04 下 MongoDB 的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm

《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm

Nagios 监控 MongoDB 分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm

基于 CentOS 6.5 操作系统搭建 MongoDB 服务 http://www.linuxidc.com/Linux/2014-11/108900.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138247.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计6566字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中