mongodb 集群shard_mongodb shard集群扩容和收缩

在这个集群的基础上再加个shard-4

mongodb-shard-4:

192.168.10.91:10003

192.168.10.92:10003

192.168.10.93:10003

###shard集群水平扩展

####在上述三台服务器上启动shard4的mongodb实例

# mkdir -p /data/mongodb/shard4

# mongod --shardsvr --replSet shard4 --port 10003 --dbpath /data/mongodb/shard4 --oplogSize 100 --logpath /data/mongodb/shard4/shard4.log --logappend --fork

####建立shard关系

# mongo --port 10003

MongoDB shell version: 2.4.14

connecting to: 127.0.0.1:10003/test

> config={_id:'shard4',members:[{_id:0,host:'192.168.10.91:10003'},{_id:1,host:'192.168.10.92:10003'},{_id:2,host:'192.168.10.93:100023'}]}

{

"_id" : "shard4",

"members" : [

{

"_id" : 0,

"host" : "192.168.10.91:10003"

},

{

"_id" : 1,

"host" : "192.168.10.92:10003"

},

{

"_id" : 2,

"host" : "192.168.10.93:10003"

}

]

}

> rs.initiate(config)

{

"info" : "Config now saved locally. Should come online in about a minute.",

"ok" : 1

}

>

shard4:PRIMARY>

####mongos上添加shard4

]# mongo --port 30000

MongoDB shell version: 2.4.14

connecting to: 127.0.0.1:30000/test

mongos> use admin

switched to db admin

mongos> db.runCommand({addshard:'shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003'})

{ "shardAdded" : "shard4", "ok" : 1 }

####查看集群状态

查看集群状态

mongos> printShardingStatus()

--- Sharding Status ---

sharding version: {

"_id" : 1,

"version" : 3,

"minCompatibleVersion" : 3,

"currentVersion" : 4,

"clusterId" : ObjectId("570394b40d1b67f7f38c434a")

}

shards:

{ "_id" : "shard1", "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }

{ "_id" : "shard2", "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }

{ "_id" : "shard3", "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }

{ "_id" : "shard4", "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }

databases:

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "test", "partitioned" : false, "primary" : "shard3" }

{ "_id" : "testdb", "partitioned" : true, "primary" : "shard1" }

testdb.table1

shard key: { "_id" : 1 }

chunks:

shard4 1

shard1 1

shard2 2

shard3 2

{ "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0)

{ "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard1 Timestamp(5, 1)

{ "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1)

{ "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0)

{ "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1)

{ "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5)

###shard集群收缩

####缩减shard1

mongos上剔除shard1

mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})

{

"msg" : "draining started successfully",

"state" : "started",

"shard" : "shard1",

"note" : "you need to drop or movePrimary these databases",

"dbsToMove" : [

"testdb"

],

"ok" : 1

}

查看shard1的状态,显示draining,正在移除

mongos> printShardingStatus()

--- Sharding Status ---

sharding version: {

"_id" : 1,

"version" : 3,

"minCompatibleVersion" : 3,

"currentVersion" : 4,

"clusterId" : ObjectId("570394b40d1b67f7f38c434a")

}

shards:

{ "_id" : "shard1", "draining" : true, "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }

{ "_id" : "shard2", "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }

{ "_id" : "shard3", "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }

{ "_id" : "shard4", "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }

databases:

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "test", "partitioned" : false, "primary" : "shard3" }

{ "_id" : "testdb", "partitioned" : true, "primary" : "shard1" }

testdb.table1

shard key: { "_id" : 1 }

chunks:

shard4 2

shard2 2

shard3 2

{ "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0)

{ "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0)

{ "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1)

{ "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0)

{ "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1)

{ "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5)

再次执行removed可查看进度,chunks为0即为完成,但是dbs需要执行movePrimary

mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})

{

"msg" : "draining ongoing",

"state" : "ongoing",

"remaining" : {

"chunks" : NumberLong(0),

"dbs" : NumberLong(1)

},

"note" : "you need to drop or movePrimary these databases",

"dbsToMove" : [

"testdb"

],

"ok" : 1

}

移动数据库 #可查看config.databases发现原来primary在shard1上,现在挪至shard2

mongos> db.runCommand({movePrimary:'testdb',to:'shard2'})

{

"primary " : "shard2:shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001",

"ok" : 1

}

mongos> db.runCommand({removeshard:'shard1'})

{

"msg" : "removeshard completed successfully",

"state" : "completed",

"shard" : "shard1",

"ok" : 1

}

再次查看分片信息发现shard已移除

mongos> printShardingStatus()

--- Sharding Status ---

sharding version: {

"_id" : 1,

"version" : 3,

"minCompatibleVersion" : 3,

"currentVersion" : 4,

"clusterId" : ObjectId("570394b40d1b67f7f38c434a")

}

shards:

{ "_id" : "shard2", "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }

{ "_id" : "shard3", "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }

{ "_id" : "shard4", "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }

databases:

{ "_id" : "admin", "partitioned" : false, "primary" : "config" }

{ "_id" : "test", "partitioned" : false, "primary" : "shard3" }

{ "_id" : "testdb", "partitioned" : true, "primary" : "shard2" }

testdb.table1

shard key: { "_id" : 1 }

chunks:

shard4 2

shard2 2

shard3 2

{ "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0)

{ "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0)

{ "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1)

{ "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0)

{ "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1)

{ "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5)

{ "_id" : "testdb1", "partitioned" : false, "primary" : "shard2" }


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部