# 列出分片信息
mongos> use admin
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "shard1",
"host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023",
"state" : 1
},
{
"_id" : "shard2",
"host" : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1600679934, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1600679934, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
或者:
mongos> use config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023", "state" : 1 }
{ "_id" : "shard2", "host" : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026", "state" : 1 }
# 分片整体状态查看
mongos> sh.status()
## 数据库分片配置
# 激活数据库分片功能
语法:( { enablesharding : "数据库名称" } )
mongos> use admin
mongos> db.runCommand( { enablesharding : "test" } )
# 指定分片建对集合分片,范围片键--创建索引
mongos> use test
mongos> db.vast.ensureIndex( { id: 1 } )
mongos> use admin
mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
# 集合分片验证
mongos> use test
mongos> for(i=0;i<20000;i++){ db.vast1.insert({"id":i,"name":"clsn","age":70,"date":new Date()}); }
mongos> db.vast.stats()
## 分片集群的操作
# 范围片键
admin> sh.shardCollection("数据库名称.集合名称",key : {分片键: 1} )
或
admin> db.runCommand( { shardcollection : "数据库名称.集合名称",key : {分片键: 1} } )
eg:
admin> sh.shardCollection("test.vast",key : {id: 1} )
或
admin> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
# 哈希片键
admin> sh.shardCollection( "数据库名.集合名", { 片键: "hashed" } )
创建哈希索引
admin> db.vast.ensureIndex( { a: "hashed" } )
admin> sh.shardCollection( "test.vast", { a: "hashed" } )
## 分片集群的操作
# 判断是否Shard集群
admin> db.runCommand({ isdbgrid : 1})
# 列出所有分片信息
mongos> use admin
mongos> db.runCommand({ listshards : 1})
# 列出开启分片的数据库
mongos> use config
mongos> db.databases.find( { "partitioned": true } )
# 查看所有数据库分片情况
config> db.databases.find()
# 查看分片的片键
config> db.collections.find()
# 查看分片的详细信息
admin> sh.status()
或
admin> db.printShardingStatus()
# 添加分片节点
db.runCommand( { addshard : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026",name:"shard2"} )
操作完需要查看验证
sh.status()
# 删除分片节点
mongos> use admin
mongos> db.runCommand( { removeShard: "shard2" } )
如果删除的片是 primary ,则会报如下的错误,必须手动移动或删除数据库,用moveprimary命令
mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard2" } )
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "shard2",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"guodb"
],
"ok" : 1,
"operationTime" : Timestamp(1600681033, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1600681033, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
正式操作
查看balancer是否开启
mongos> sh.getBalancerState()
true
查找要删除的分片id
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f698c4006a39080aee80cd0")
}
shards:
{ "_id" : "shard1", "host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023", "state" : 1 }
{ "_id" : "shard2", "host" : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026", "state" : 1 }
查看primary信息
mongos> use config
mongos> db.databases.find()
{ "_id" : "guodb", "primary" : "shard2", "partitioned" : true }
{ "_id" : "test", "primary" : "shard1", "partitioned" : true }
移除分片操作
mongos> use admin
mongos> db.runCommand({"moveprimary":"guodb","to":"shard1"})
这时再执行removeshard命令移除分片
mongos> use admin
mongos> db.runCommand({"removeshard":"shard2"})
验证查看
mongos> use config
switched to db config
mongos> db.databases.find()
{ "_id" : "guodb", "primary" : "shard1", "partitioned" : true }
{ "_id" : "test", "primary" : "shard1", "partitioned" : true }
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5f698c4006a39080aee80cd0")
}
shards:
{ "_id" : "shard1", "host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023", "state" : 1 }
## balance操作
# 查看mongo集群是否开启了balance状态
mongos> sh.getBalancerState()
true
当然你也可以通过在路由节点mongos上执行 sh.status() 查看balance状态。
如果balance开启,查看是否正在有数据的迁移
连接mongo集群的路由节点
mongos> sh.isBalancerRunning()
false
# 设置balance窗口
(1)连接mongo集群的路由节点
(2)切换到配置节点
use config
(3)确定balance开启中
sh.getBalancerState()
如果未开启,执行命令
sh.setBalancerState( true )
(4)修改balance窗口的时间
db.settings.update(
{ _id: "balancer" },
{ $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },
{ upsert: true }
)
eg:
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "00:00", stop : "5:00" } } }, true )
当你设置了activeWindow,就不能用sh.startBalancer()启动balance
(5)删除balance窗口
use config
db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })
# 关闭balance
默认balance的运行可以在任何时间,只迁移需要迁移的chunk,如果要关闭balance运行,停止一段时间可以用下列方法:
(1)连接到路由mongos节点
(2)停止balance
sh.stopBalancer()
(3)查看balance状态
sh.getBalancerState()
(4)停止balance后,没有迁移进程正在迁移,可以执行下列命令
use config
while( sh.isBalancerRunning() ) {
print("waiting...");
sleep(1000);
}
# 重新打开balance
如果你关闭了balance,准备重新打开balance
(1)连接到路由mongos节点
(2)打开balance
sh.setBalancerState(true)
如果驱动没有命令 sh.startBalancer(),可以用下列命令
use config
db.settings.update( { _id: "balancer" }, { $set : { stopped: false } } , { upsert: true } )
# 关闭某个集合的balance
sh.disableBalancing("students.grades")
# 打开某个集合的balance
sh.enableBalancing("students.grades")
# 确定某个集合的balance是开启或者关闭
db.getSiblingDB("config").collections.findOne({_id : "students.grades"}).noBalance;
# 禁用分片的自动平衡
mongodb在做自动分片平衡的时候,或引起数据库响应的缓慢,可以通过禁用自动平衡以及设置自动平衡进行的时间来解决这一问题。
use config
db.settings.update( { _id: "balancer" }, { $set : { stopped: true } } , true );
# 自定义自动平衡进行的时间段
use config
db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "21:00", stop : "9:00" } } }, true )