[置顶] mongo集群常用维护命令

时间:2022-09-16 15:48:15

在上一篇blog中,大概把集群搭建起来了。在这片文章主要是讲在集群搭建之后,操作的常见命令。

首先连接路由节点

./mongo 172.17.253.217:30000/wallet


1、让数据库支持分片

mongos> db.runCommand({enablesharding:"wallet"})
{ "ok" : 1 }

通过执行以上命令,可以让数据库跨shard,如果不执行这步,数据库只会存放在一个shard,一旦激活数据库分片,数据库中不同的collection将被存放在不同的shard上,但一个collection仍旧存放在同一个shard上,要使单个collection也分片,还需单独对collection作些操作

2、让Collection支持分片

要使单个collection也分片存储,需要给collection指定一个分片key,通过以下命令操作:
> db.runCommand( { shardcollection : “<namespace>”,key : <shardkeypatternobject> });
注:
 a. 分片的collection系统会自动创建一个索引(也可用户提前创建好)
 b. 分片的collection只能有一个在分片key上的唯一索引,其它唯一索引不被允许
One note: a sharded collection can have only one unique index, which must exist on the shard key. No other unique indexes can exist on the collection.

mongos> db.runCommand({shardcollection:"wallet.order",key:{id:1}})
{ "collectionsharded" : "wallet.order", "ok" : 1 }

3、现在你需要在刚才指定dbname和tablename插入一定数量的数据。测试集群:
for (var i = 1; i <= 2000000; i++) db.order.save({id:i,value1:"1234567890"});

4、printShardingStatus:给出整个分片系统的一些状态信息:

我们可以看到shard1和shard2都分布有数据,且id是主见并有唯一索引。数据展示了id从1到10000是shard1,从10000到70535在shard2。

partitioned:true表示为开启了分片

primary:shard2 说明数据的大本营在shard2上,存储会从shard2开始。

mongos> use wallet
switched to db wallet
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("52fe25aacc5c545ae9ff8725")
}
shards:
{ "_id" : "shard1", "host" : "shard1/172.17.253.216:27017,172.17.253.217:27017" }
{ "_id" : "shard2", "host" : "shard2/172.17.253.217:27018" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "order", "partitioned" : true, "primary" : "shard1" }
{ "_id" : "wallet", "partitioned" : true, "primary" : "shard2" }
wallet.order
shard key: { "id" : 1 }
chunks:
shard15
shard26
{ "id" : { "$minKey" : 1 } } -->> { "id" : 1 } on : shard1 Timestamp(7, 1)
{ "id" : 1 } -->> { "id" : 10000 } on : shard1 Timestamp(4, 0)
{ "id" : 10000 } -->> { "id" : 70535 } on : shard2 Timestamp(6, 1)
{ "id" : 70535 } -->> { "id" : 119220 } on : shard2 Timestamp(3, 4)
{ "id" : 119220 } -->> { "id" : 200000 } on : shard2 Timestamp(3, 5)
{ "id" : 200000 } -->> { "id" : 464905 } on : shard1 Timestamp(4, 2)
{ "id" : 464905 } -->> { "id" : 729811 } on : shard1 Timestamp(4, 4)
{ "id" : 729811 } -->> { "id" : 994717 } on : shard2 Timestamp(5, 2)
{ "id" : 994717 } -->> { "id" : 1259623 } on : shard2 Timestamp(5, 4)
{ "id" : 1259623 } -->> { "id" : 1833581 } on : shard1 Timestamp(6, 2)
{ "id" : 1833581 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(7, 0)

5、db.order.stats()该命令可以查看表的存储状态

mongos> db.order.stats()
{
"sharded" : true,
"ns" : "wallet.order",
"count" : 2414349,
"numExtents" : 22,
"size" : 154518432,
"storageSize" : 247873536,
"totalIndexSize" : 158123840,
"indexSizes" : {
"_id_" : 79421664,
"id_1" : 78702176
},
"avgObjSize" : 64.00003976227131,
"nindexes" : 2,
"nchunks" : 11,
"shards" : {
"shard1" : {
"ns" : "wallet.order",
"count" : 1153766,
"size" : 73841072,
"avgObjSize" : 64.00004160289001,
"storageSize" : 123936768,
"numExtents" : 11,
"nindexes" : 2,
"lastExtentSize" : 37625856,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 71785280,
"indexSizes" : {
"_id_" : 38476256,
"id_1" : 33309024
},
"ok" : 1
},
"shard2" : {
"ns" : "wallet.order",
"count" : 1260583,
"size" : 80677360,
"avgObjSize" : 64.00003807761964,
"storageSize" : 123936768,
"numExtents" : 11,
"nindexes" : 2,
"lastExtentSize" : 37625856,
"paddingFactor" : 1,
"systemFlags" : 1,
"userFlags" : 0,
"totalIndexSize" : 86338560,
"indexSizes" : {
"_id_" : 40945408,
"id_1" : 45393152
},
"ok" : 1
}
},
"ok" : 1
}

6、Mongodb运行时添加节点

首先连接shard1的主节点

[root@localhost bin]# ./mongo 172.17.253.216:27017
MongoDB shell version: 2.4.9
connecting to: 172.17.253.216:27017/test
shard1:PRIMARY>
目前看到有两个节点
shard1:PRIMARY> rs.isMaster();
{
"setName" : "shard1",
"ismaster" : true,
"secondary" : false,
"hosts" : [
"172.17.253.216:27017"
],
"arbiters" : [
"172.17.253.67:27017"
],
"primary" : "172.17.253.216:27017",
"me" : "172.17.253.216:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2014-02-16T11:12:07.831Z"),
"ok" : 1
}
按照之前将的启动一个节点,添加节点


shard1:PRIMARY> rs.add("172.17.253.217:27017");
{ "down" : [ "172.17.253.217:27017" ], "ok" : 1 }
再次查看状态

shard1:PRIMARY> rs.conf();
{
"_id" : "shard1",
"version" : 5,
"members" : [
{
"_id" : 0,
"host" : "172.17.253.216:27017",
"priority" : 2
},
{
"_id" : 2,
"host" : "172.17.253.67:27017",
"arbiterOnly" : true
},
{
"_id" : 3,
"host" : "172.17.253.217:27017"
}
]
}

    添加成功

7、Mongodb运行时移除节点

rs.remove("172.17.253.217:27017");

8、Mongodb运行时移除分片

1、连接mongos节点 ./mongo 172.17.253.217:30000/admin

2、运行db.runCommand( { removeshard: "shard2" } )

{ msg : "draining started successfully", state: "started", shard :"shard2", ok : 1 }

3、查看状态,我们可以反复执行上面语句查看执行状态

db.runCommand( { removeshard: "shard2" } )

{ msg: "draining ongoing", state: "ongoing", remaining: { chunks: 42, dbs : 1 }, ok: 1

说明正在迁移中。