当前位置:Gxlcms > 数据库问题 > 搭建mongodb集群(副本集+分片)

搭建mongodb集群(副本集+分片)

时间:2021-07-01 10:21:17 帮助过:18人阅读

特性
  1. 杀掉bluejoe1:10001的进程
  2. 再次连接上bluejoe3:10002。看到shard1:PRIMARY>的字样,说明它已经变成shard1的主节点
  3. 假设这时再启动bluejoe1:10001,则发现它已经变成shard1:SECONDARY>
  • 创建data/configdb。启动配置库实例:
    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 &

    假设须要关闭mongod服务,可使用--shutdown选项。如:
    /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/data/configdb --shutdown

    到如今为止应该有12个mongd实例。当中3个为配置库实例,剩下的属于3个副本集。
  • 接下来管理分片,在bluejoe0上启动mongs:

    nohup /usr/local/mongodb/bin/mongos --port 30000 --configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000 &
  • 连接上mongos,配置分片信息:
    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   

    人气教程排行