從零搭建mongo分片集羣的簡潔方法

1、目錄web

  一、mongo路徑,config數據路徑,shard數據路徑shell

    

   

  二、shard數據路徑的結構(共6個分片,分別位於D盤和E盤)數據庫

     1)D盤中服務器

      

    2)E盤中url

      

  三、啓動各個服務端的批處理spa

   

    1)啓動configs服務器code

mongod --dbpath=d:\shard_configs --port 23017

   2)啓動mongos服務器blog

mongos  --port 25017  --configdb 10.0.0.186:23017

   3)啓動各個shard分片服務器索引

mongod --dbpath=d:\shard_data\shard_data_000 --port 27017
mongod --dbpath=d:\shard_data\shard_data_001 --port 27018
mongod --dbpath=d:\shard_data\shard_data_002 --port 27019
mongod --dbpath=e:\shard_data\shard_data_003 --port 27020
mongod --dbpath=e:\shard_data\shard_data_004 --port 27021
mongod --dbpath=e:\shard_data\shard_data_005 --port 27022

2、將各個分片添加到集羣中md5

  一、防火牆上開放23017和25017端口

    

   二、將各個分片添加到集羣中

C:\Users\Administrator>mongo 10.0.0.186:25017
MongoDB shell version: 2.4.5
connecting to: 10.0.0.186:25017/test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:"10.0.0.186:27017",allowLocal:true})
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> db.runCommand({addshard:"10.0.0.186:27018",allowLocal:true})
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> db.runCommand({addshard:"10.0.0.186:27019",allowLocal:true})
{ "shardAdded" : "shard0002", "ok" : 1 }
mongos> db.runCommand({addshard:"10.0.0.186:27020",allowLocal:true})
{ "shardAdded" : "shard0003", "ok" : 1 }
mongos> db.runCommand({addshard:"10.0.0.186:27021",allowLocal:true})
{ "shardAdded" : "shard0004", "ok" : 1 }
mongos> db.runCommand({addshard:"10.0.0.186:27022",allowLocal:true})
{ "shardAdded" : "shard0005", "ok" : 1 }
mongos>

  三、查看最終的分片結果

mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "10.0.0.186:27017" }
{ "_id" : "shard0001", "host" : "10.0.0.186:27018" }
{ "_id" : "shard0002", "host" : "10.0.0.186:27019" }
{ "_id" : "shard0003", "host" : "10.0.0.186:27020" }
{ "_id" : "shard0004", "host" : "10.0.0.186:27021" }
{ "_id" : "shard0005", "host" : "10.0.0.186:27022" }
mongos>

3、建立數據庫和集合,並制定分片鍵

  一、建立數據庫,集合,索引

  

  二、指定news,forum集合的分片鍵

    1)數據庫web_content啓動分片功能

mongos> use admin
switched to db admin
mongos> db.runCommand({"enablesharding":"web_content"})
{ "ok" : 1 }
mongos>

    2)指定兩個集合的分片鍵

mongos> use admin
switched to db admin
mongos> db.runCommand({"shardcollection":"web_content.news","key":{"url_md5":1}}
)
{ "collectionsharded" : "web_content.news", "ok" : 1 }
mongos> db.runCommand({"shardcollection":"web_content.forum","key":{"url_md5":1}
})
{ "collectionsharded" : "web_content.forum", "ok" : 1 }
mongos>

    3)查看初始時的分片狀況

      news集合:

        

      forum集合:

        

相關文章
相關標籤/搜索