【MongoDB】从复制集迁移到分片集群
2022-06-05 14:06 abce 阅读(756) 评论(0) 编辑 收藏 举报迁移的过程
1.为Confiugure Server初始化最少包含三个节点的副本集(另一个成员可以作为隐藏节点用于备份中)。
2.现有副本集执行必要的操作系统、硬件和磁盘级别的调优。
3.在mongod配置文件中为Config服务器设置适当的 clusterRole。
4.为查询路由器(MongoS)创建至少两个节点。
5.在mongos配置文件中设置适当的configDB参数。
6.从上面重复步骤2对现有副本集进行调优。
7.在Config server和MongoS的所有新配置的节点上配置适当的SELinux策略。
8.以滚动方式将clusterRole参数添加到现有副本集节点中。
9.将所有用户从副本集中复制到任何MongoS。
10.连接到任何MongoS并将现有的副本集添加为Shard。
注意:
在完成shard key之前,不要在任何数据库上启用shding。如果最终确定后,我们就可以启用分片。
详细的迁移计划
现在假设副本集有三个节点(一主两从)。
1.为Config Server初始化创建三个节点的副本集
2.在Config Server的配置文件中,添加参数clusterRole:confgsvr和port:27019
3.如果开启了selinux,需要为dbPath、keyFile、日志文件等配置selinux策略
1 2 3 4 5 6 7 8 9 10 11 12 13 | sudo semanage fcontext -a -t mongod_var_lib_t '/dbPath/mongod.*' sudo chcon -Rv -u system_u -t mongod_var_lib_t '/dbPath/mongod' sudo restorecon -R -v '/dbPath/mongod' sudo semanage fcontext -a -t mongod_log_t '/logPath/log.*' sudo chcon -Rv -u system_u -t mongod_log_t '/logPath/log' sudo restorecon -R -v '/logPath/log' sudo semanage port -a -t mongod_port_t -p tcp 27019 |
启动所有的Config Server mongodb实例,连接到其中任何一个节点。创建临时用户并初始化副本集。
1 2 3 4 5 | > use admin > rs.initiate() > db.createUser( { user : "tempUser" , pwd: "<password>" , roles:[{role: "root" , db: "admin" }]}) |
创建一个角色anyResource,action是anyAction,并将其授权给tempUser:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | >db.getSiblingDB( "admin" ).createRole({ "role" : "pbmAnyAction" , "privileges" : [ { "resource" : { "anyResource" : true }, "actions" : [ "anyAction" ] } ], "roles" : [] }); > >db.grantRolesToUser( "tempUser" , [{role: "pbmAnyAction" , db: "admin" }] ) > rs. add ( "config_host[2-3]:27019" ) |
至此,Config Server副本集创建完成。接下来就是部署查询路由了(MongoS):
1.为MongoS创建两个实例,并调优。
2.在mongos的配置文件中,调整configDB参数,只包含非隐藏的Config Server节点。
3.应用selinux策略
4.在副本集节点的mongod.conf文件中添加以下参数。确保服务是以滚动的方式启动的。比如,即从辅助节点开始,然后降级现有的主节点并使用端口27018重新启动它。
1 | clusterRole: shardsvr |
登录到任意一个MongoS,使用tempUser验证身份,将已经存在的副本集添加成分片
1 | > sh.addShard( "replicaSetName/<URI of the replica set>" ) //Provide URI of the replica set |
确认:
1 | > sh.status() or db.getSiblingDB( "config" )[ 'shards' ].find() |
连接到副本集的主节点,备份所有用户和角色,
1 2 3 4 5 6 7 | > var mongos = new Mongo( "mongodb://put MongoS URI string here/admin?authSource=admin" ) //Provide the URI of the MongoS with tempUser for authentication/ authorization . >db.getSiblingDB( "admin" ).system.roles.find().forEach( function (d) { mongos.getDB( 'admin' ).getCollection( 'system.roles' ). insert (d)}); >db.getSiblingDB( "admin" ).system.users.find().forEach( function (d) { mongos.getDB( 'admin' ).getCollection( 'system.users' ). insert (d)}); |
1.连接到所有MongoS,并验证已经拷贝了所有用户
2.如果数据的shardkey已经完成,就对数据库分片
对数据库分片:
1 | >sh.enableSharding( "<db>" ) |
使用基于hash的分片键对集合做分片:
1 | >sh.shardCollection( "<db>.<coll1>" , { <shard key field> : "hashed" } ) |
使用基于范围的分片键对集合做分片:
1 | >sh.shardCollection( "<db>.<coll1>" , { <shard key field> : 1, ... } ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-06-05 Python中的with语句