时间:2021-07-01 10:21:17 帮助过:18人阅读
mkdir /usr/local/mongodb/data/configdb nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --port 20000 --logpath=/usr/local/mongodb/log/configdb.log &
/usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --shutdown
nohup /usr/local/mongodb/bin/mongos --port 30000 --configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000 &
mongo bluejoe0:30000 mongos> use admin switched to db admin db.runCommand({"addShard":"db1/bluejoe1:10001"}) db.runCommand({"addShard":"db2/bluejoe1:10002"}) db.runCommand({"addShard":"db3/bluejoe1:10003"})
mongos> db.runCommand({listshards:1}) { "shards" : [ { "_id" : "db1", "host" : "db1/bluejoe1:10001,bluejoe2:10001,bluejoe3:10001" }, { "_id" : "db2", "host" : "db2/bluejoe1:10002,bluejoe2:10002,bluejoe3:10002" }, { "_id" : "db3", "host" : "db3/bluejoe1:10003,bluejoe2:10003,bluejoe3:10003" } ], "ok" : 1 }能够看出,虽然注冊的时候仅仅是提供了副本集的主节点,但mongos已知晓了各辅助节点;
mongos> db.runCommand({"enablesharding":"test"}) { "ok" : 1 } mongos> db.runCommand({"shardcollection":"test.person","key":{_id:‘hashed‘}}) { "collectionsharded" : "test.person", "ok" : 1 }
mongos> for(var i=0;i<10;i++){db.person.insert({name:"bluejoe"+i});} WriteResult({ "nInserted" : 1 })
[root@hadoop0 ~]# mongo bluejoe3:10002 MongoDB shell version: 2.6.5 connecting to: bluejoe3:10002/test shard1:PRIMARY> db.person.find() { "_id" : ObjectId("546ff1013a0d0ec68b42a914"), "name" : "bluejoe0" } { "_id" : ObjectId("546ff1013a0d0ec68b42a915"), "name" : "bluejoe1" } { "_id" : ObjectId("546ff1013a0d0ec68b42a91a"), "name" : "bluejoe6" } { "_id" : ObjectId("546ff1013a0d0ec68b42a91b"), "name" : "bluejoe7" } { "_id" : ObjectId("546ff1013a0d0ec68b42a91c"), "name" : "bluejoe8" } shard1:PRIMARY> exit bye [root@hadoop0 ~]# mongo bluejoe2:10001 MongoDB shell version: 2.6.5 connecting to: bluejoe2:10001/test shard2:PRIMARY> db.person.find() { "_id" : ObjectId("546ff1013a0d0ec68b42a918"), "name" : "bluejoe4" } shard2:PRIMARY> exit bye [root@hadoop0 ~]# mongo bluejoe3:10001 MongoDB shell version: 2.6.5 connecting to: bluejoe3:10001/test shard3:PRIMARY> db.person.find() { "_id" : ObjectId("546ff1013a0d0ec68b42a916"), "name" : "bluejoe2" } { "_id" : ObjectId("546ff1013a0d0ec68b42a917"), "name" : "bluejoe3" } { "_id" : ObjectId("546ff1013a0d0ec68b42a919"), "name" : "bluejoe5" } { "_id" : ObjectId("546ff1013a0d0ec68b42a91d"), "name" : "bluejoe9" } shard3:PRIMARY>
搭建mongodb集群(副本集+分片)
标签:root prim ble 文件夹 进程 switch comm 连接 person