从零搭建mongo分片集群的简洁方法

来源:互联网 发布:怎样在微信上做淘宝客 编辑:程序博客网 时间:2024/04/30 12:59

一、目录

  1、mongo路径,config数据路径,shard数据路径

    

   

  2、shard数据路径的结构(共6个分片,分别位于D盘和E盘)

     1)D盘中

      

    2)E盘中

      

  3、启动各个服务端的批处理

   

    1)启动configs服务器

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

   2)启动mongos服务器

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

二、将各个分片添加到集群中

  1、防火墙上开放23017和25017端口

    

   2、将各个分片添加到集群中

复制代码
C:\Users\Administrator>mongo 10.0.0.186:25017MongoDB shell version: 2.4.5connecting to: 10.0.0.186:25017/testmongos> use adminswitched to db adminmongos> 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>
复制代码

  3、查看最终的分片结果

复制代码
mongos> use configswitched to db configmongos> 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>
复制代码

三、创建数据库和集合,并制定分片键

  1、创建数据库,集合,索引

  

  2、指定news,forum集合的分片键

    1)数据库web_content启动分片功能

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

    2)指定两个集合的分片键

复制代码
mongos> use adminswitched to db adminmongos> 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集合:

        

 

0 0
原创粉丝点击