◆ 目标:搭建拥有两个分片的MongoDB分片集群,分别为shard1分片、shard2分片
◆ 环境:3台Linux虚拟机,4C8G,主机名分别名为starcto1、starcto2、starcto3
◆ starcto1扩展别名:member1.example.com/member2.example.com
◆ starcto2扩展别名:member3.example.com/member4.example.com
◆ starcto3扩展别名:member5.example.com/member6.example.com
注:扩展别名,用于方便标识分片节点。
注:这里我为了节省虚机数量,单台虚机会部署多个MongoDB节点,生产环境中,建议每台机器部署一个节点。
在3台虚拟机上分别执行以下3条命令,注意替换实际 IP 地址
echo "192.168.122.46 starcto1 member1.example.com member2.example.com" >> /etc/hosts echo "192.168.122.124 starcto2 member3.example.com member4.example.com" >> /etc/hosts echo "192.168.122.178 starcto3 member5.example.com member6.example.com" >> /etc/hosts
在各服务器上创建数据目录,我们使用 ‘/data’,请按自己需要修改为其他目录:
(1)在member1 / member3 / member5 上执行以下命令:
mkdir -p /data/shard1/ mkdir -p /data/config/
(2)在member2 / member4 / member6 上执行以下命令:
mkdir -p /data/shard2/ mkdir -p /data/mongos/
(1)创建分片
# 下载MongoDB curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz tar -xvf mongodb-linux-x86_64-rhel70-4.2.1.tgz # 将mongo、mongod、mongos等工具复制到/usr/local/bin目录下 [root@starcto1 ~]# cd mongodb-linux-x86_64-rhel70-4.2.1/bin/ [root@starcto1 bin]# cp * /usr/local/bin/ # 在 member1 / member3 / member5 上执行以下命令 mongod --bind_ip 0.0.0.0 --replSet shard1 --dbpath /data/shard1 --logpath /data/shard1/mongod.log --port 27010 --fork --shardsvr --wiredTigerCacheSizeGB 1
(2)初始化分片
# 关闭member1 / member3 / member5防火墙 systemctl stop firewalld.service systemctl disable firewalld.service # 初始化一个分片复制集 [root@starcto1 ~]# mongo --host member1.example.com:27010 rs.initiate({ _id: "shard1", "members": [ { "_id": 0, "host": "member1.example.com:27010" }, { "_id": 1, "host": "member3.example.com:27010" }, { "_id": 2, "host": "member5.example.com:27010" } ] });
(1)创建配置节点
# 在 member1 / member3 / member5 上执行以下命令。 mongod --bind_ip 0.0.0.0 --replSet config --dbpath /data/config --logpath /data/config/mongod.log --port 27019 --fork --configsvr --wiredTigerCacheSizeGB 1
(2)初始化配置节点
# 连接配置节点 mongo --host member1.example.com:27019 # 初始化配置节点 rs.initiate({ _id: "config", "members": [ { "_id": 0, "host": "member1.example.com:27019" }, { "_id": 1, "host": "member3.example.com:27019" }, { "_id": 2, "host": "member5.example.com:27019" } ] });
# 搭建mongos,并添加配置节点 mongos --bind_ip 0.0.0.0 --logpath /data/mongos/mongos.log --port 27017 --fork --configdb config/member1.example.com:27019,member3.example.com:27019,member5.example.com:27019 # 连接到mongos, 添加分片 mongo --host member1.example.com:27017 mongos> sh.addShard("shard1/member1.example.com:27010,member3.example.com:27010,member5.example.com:27010"); { "shardAdded" : "shard1", "ok" : 1, "operationTime" : Timestamp(1646408054, 7), "$clusterTime" : { "clusterTime" : Timestamp(1646408054, 7), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
# 连接到mongos, 创建分片集合 mongo --host member1.example.com:27017 # 查看分片集群状态 mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("62222ffebad25d048641cbb8") } shards: { "_id" : "shard1", "host" : "shard1/member1.example.com:27010,member3.example.com:27010,member5.example.com:27010", "state" : 1 } active mongoses: "4.2.1" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard1 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) # 针对foo集合启动分片 mongos> sh.enableSharding("foo"); { "ok" : 1, "operationTime" : Timestamp(1646408196, 5), "$clusterTime" : { "clusterTime" : Timestamp(1646408196, 5), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } # foo集合的bar文档的自增_id的hash作为片键,进行写分布 mongos> sh.shardCollection("foo.bar", {_id: 'hashed'}); { "collectionsharded" : "foo.bar", "collectionUUID" : UUID("3616a206-1ad7-4e99-906b-13b7b9ac76b4"), "ok" : 1, "operationTime" : Timestamp(1646408236, 13), "$clusterTime" : { "clusterTime" : Timestamp(1646408236, 13), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } # 再次查看分片集群状态 mongos> sh.status(); --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("62222ffebad25d048641cbb8") } shards: { "_id" : "shard1", "host" : "shard1/member1.example.com:27010,member3.example.com:27010,member5.example.com:27010", "state" : 1 } active mongoses: "4.2.1" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: # 数据库 { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard1 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) { "_id" : "foo", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("29d23289-dd58-401c-9ed8-1e66db2b3628"), "lastMod" : 1 } } # foo数据库、shard1是主分片、已经开启分区表 foo.bar # 分片表会被列出来 shard key: { "_id" : "hashed" } # 片键是_id hash unique: false balancing: true # 开启自动均衡 chunks: # 块数量 shard1 2 # shard1上有两个块 { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard1 Timestamp(1, 0) { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 1) # 模拟插入数据 use foo for (var i = 0; i < 10000; i++) { db.bar.insert({i: i}); }
(1)创建分片复制集
# 在 member2 / member4 / member6 上执行以下命令 mongod --bind_ip 0.0.0.0 --replSet shard2 --dbpath /data/shard2 --logpath /data/shard2/mongod.log --port 27011 --fork --shardsvr --wiredTigerCacheSizeGB 1
(2)初始化第2个分片复制集
mongo --host member2.example.com:27011 rs.initiate({ _id: "shard2", "members": [ { "_id": 0, "host": "member2.example.com:27011" }, { "_id": 1, "host": "member4.example.com:27011" }, { "_id": 2, "host": "member6.example.com:27011" } ] });
(3)将第2个分片加入分片集群
# 连接到mongos, 添加分片 mongo --host member1.example.com:27017 mongos> sh.addShard("shard2/member2.example.com:27011,member4.example.com:27011,member6.example.com:27011"); # 查看分片集群状态 mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("62222ffebad25d048641cbb8") } shards: { "_id" : "shard1", "host" : "shard1/member1.example.com:27010,member3.example.com:27010,member5.example.com:27010", "state" : 1 } { "_id" : "shard2", "host" : "shard2/member2.example.com:27011,member4.example.com:27011,member6.example.com:27011", "state" : 1 } active mongoses: "4.2.1" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 1 : Success databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard1 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0) { "_id" : "foo", "primary" : "shard1", "partitioned" : true, "version" : { "uuid" : UUID("29d23289-dd58-401c-9ed8-1e66db2b3628"), "lastMod" : 1 } } foo.bar shard key: { "_id" : "hashed" } unique: false balancing: true chunks: # 新增分片后,MongoDB自动将两个块分布到两个分片上 shard1 1 shard2 1 { "_id" : { "$minKey" : 1 } } -->> { "_id" : NumberLong(0) } on : shard2 Timestamp(2, 0) { "_id" : NumberLong(0) } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(2, 1)
注:MongoDB分片集群,每增加一个分片,MongoDB就会自动做数据均衡,把块移动均衡到新的分片上。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/128127.html
摘要:此文已由作者温正湖授权网易云社区发布。分片集群通过就可以实现负载均衡,不需要单独部署负载均衡组件。是一个集群,通过协议保持数据的一致性副本数量可配置,默认保存三副本,并通过做负载均衡调度。 此文已由作者温正湖授权网易云社区发布。 欢迎访问网易云社区,了解更多网易技术产品运营经验。 最近阅读了TiDB源码的说明文档,跟MongoDB的分片集群做了下简单对比。 首先展示TiDB的整体架构 ...
摘要:搭建分片分区集群环境安装三台机器关闭防火墙主节点副节点仲裁仲裁主节点副节点副节点仲裁主节点端口分配下载并且安装所有版本二进制文件自行下载改名分别在每台机器建立六个目录,因为不存储数据,只需要建立日志文件目录即可。 搭建 MongoDB分片(sharding) / 分区 / 集群环境 1. 安装 MongoDB 三台机器 关闭防火墙 systemctl stop firewalld.se...
摘要:本篇记录高可用模式部署步骤,其他部署方式见上一篇。首先准备机器,我这里是在公司云平台创建了三台,分别是,,。分片的选择策略可以参考官方文档。 本篇记录MongoDB高可用模式部署步骤,其他部署方式见上一篇。 首先准备机器,我这里是在公司云平台创建了三台DB server,ip分别是10.199.144.84,10.199.144.89,10.199.144.90。 分别安装mong...
摘要:版本目前支持和,用户可以根据需求选择相应的云数据库版本。硬盘云数据库的硬盘大小。云数据库提供自动备份和手动备份两种方式,防止数据丢失,避免误操作带来的风险。日志日志是用于记录云数据库操作事件的记录文件。什么是云数据库MongoDB?云数据库简介: 云数据库MongoDB是基于成熟云计算技术的高可用、高性能的数据库服务,完全兼容MongoDB 协议,支持灵活部署,除副本集实例架构外,云数据库...
摘要:解决方案使用,将用户数据以监管为目的进行分割,并将其保留在当地管辖范围内。我是上海小胖,专注等开源数据库的,拥抱开源,接受收费。上海小胖原创地址欢迎各位大神前来评论。每周五,敬请期待,上海小胖独更。 SteppeChange通过使用MongoDB,从其应用程序的开发进度中节省下大约六个月的时间。 SteppeChange是一家大型数据分析技术公司,负责设计和实施客户定制,快速上市的数据...
摘要:解决方案使用,将用户数据以监管为目的进行分割,并将其保留在当地管辖范围内。我是上海小胖,专注等开源数据库的,拥抱开源,接受收费。上海小胖原创地址欢迎各位大神前来评论。每周五,敬请期待,上海小胖独更。 SteppeChange通过使用MongoDB,从其应用程序的开发进度中节省下大约六个月的时间。 SteppeChange是一家大型数据分析技术公司,负责设计和实施客户定制,快速上市的数据...
阅读 1281·2024-02-01 10:43
阅读 368·2024-01-31 14:58
阅读 432·2024-01-31 14:54
阅读 814·2024-01-29 17:11
阅读 2201·2024-01-25 14:55
阅读 1481·2023-06-02 13:36
阅读 2080·2023-05-23 10:26
阅读 469·2023-05-23 10:25