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


    一、目录

      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_datashard_data_000 --port 27017
    mongod --dbpath=d:shard_datashard_data_001 --port 27018
    mongod --dbpath=d:shard_datashard_data_002 --port 27019
    mongod --dbpath=e:shard_datashard_data_003 --port 27020
    mongod --dbpath=e:shard_datashard_data_004 --port 27021
    mongod --dbpath=e:shard_datashard_data_005 --port 27022

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

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

        

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

    C:UsersAdministrator>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>

      3、查看最终的分片结果

    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>

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

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

      

      2、指定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集合:

            

  • 相关阅读:
    my first blog
    CentOS启动报错:Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block
    J2EE在Web容器中启动报错:com.sun.jdi.InvocationException occurred invoking method.
    SVN报错:can't open file db/txn-current-lock:permission denied 解决方法
    Linux(CentOS6.4、CentOS6.3)下安装、配置PostgreSQL9.2
    [模板] 线性代数:矩阵/高斯消元/矩阵求逆/行列式
    [模板] 杂项:二分/离散化/随机数/其他
    [模板] 图论:存储/拓扑排序
    Luogu1137 旅行计划(拓扑排序)
    C++模板、.vimrc和一些Linux配置
  • 原文地址:https://www.cnblogs.com/edisonfeng/p/3636576.html
Copyright © 2020-2023  润新知