范围分片
注:hash分片通常是为了规避单调递增写热点
range分片通常是为了提升范围区间查询,不广播所有分片
也可hash+range结合方式来分片,兼顾写分散,读收敛
如:订单类 {customer_id:“hashed”,order_date:1}
mongo --port 27000 --host 127.0.0.1
use admin
db.auth("admin","Admin@01");
#删除测试collection: testdb.person
use testdb
db.person.drop()
#启动testdb.person,按省2份分片
sh.enableSharding("testdb")
sh.shardCollection("testdb.person",{province:1})
db.person.getIndexes()
load("/home/mongo/testdb_func.js")
#停平衡器
db.printShardingStatus()
db.person.createIndex({province:1,age:1})
db.adminCommand( {
refineCollectionShardKey: "testdb.person",
key: { province: 1, age: 1 }
} )
#启动平衡器
重定义分片键
重定义必须满足如下条件
4.4版本以上或兼容4.4
新分片键必须有相同的前缀,即按已存在的分片键扩展
新字段只能添加做后缀
需要先建立新索引包含新的分片键
重定义分片键需关闭均衡器
具体步骤
1.检查当前库版本
db.version()
2.当前FCV版本需4.4或之后版本
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
3.关闭均衡器 (on mongos)
sh.getBalancerState() # check state (assuming “true”)
sh.isBalancerRunning() # check if running (assuming “true”)
sh.stopBalancer() # stop the balancer
sh.getBalancerState() # check state (assuming “false”)
sh.isBalancerRunning() # check if running (assuming “false”)
4.如果需要设置FCV
注:除非升级,否则不要轻易修改FCV兼容
db.adminCommand( { setFeatureCompatibilityVersion: "4.4" } )
5创建索引支持新分片键
db.<collection>.createIndex(keys, options)
db.<collection>.getIndexes();
6.运行refineCollectionShardKey 扩展分片键字段
db.adminCommand( {
refineCollectionShardKey: "<database>.<collection>",
key: { <existing key specification>, <suffix1>: <1|"hashed">, ... }
} )
7.检查分片状态确保修改生效
sh.status()
8.重启均衡器 (on mongos)
use config
sh.startBalancer() # start the balancer
sh.getBalancerState() # check state (assuming “true”)
sh.isBalancerRunning() # check if running (assuming “true”)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




