1.1 架构思路:

192.168.50.131              192.168.50.131             192.168.50.132

mongos

mongos

mongos

configsvr

configsvr

configsvr

Shard1

Shard1

Shard1

Shard2

Shard2

Shard2

Shard3

Shard3

Shard3

1.2 安装使用虚拟机:

192.168.50.130,192.168.50.131,192.168.50.132

1.3 软件包:

mongodb-linux-x86_64-rhel62-3.4.0.tar.gz

1.4 创建软连接(三台机器)

ln -s /usr/local/ mongodb-linux-x86_64-rhel62-3.4.0/bin/mongo /usr/bin/mongo

ln -s /usr/local/ mongodb-linux-x86_64-rhel62-3.4.0/bin/mongos /usr/bin/mongos

ln -s /usr/local/ mongodb-linux-x86_64-rhel62-3.4.0/bin/mongod /usr/bin/mongod

1.5 创建数据和日志目录(三台机器)

mkdir -p /data/mongodb && cd /data/mongodb/ && mkdir -p conf/data conf/log mongos/log shard{1..3}/data shard{1..3}/log

1.6 创建configsvr(三台机器)

mongod --configsvr --replSet configset --dbpath /data/mongodb/conf/data --port 27100 --logpath /data/mongodb/conf/confdb.log   --bind_ip 0.0.0.0  --fork

1.7 将configsvr初始化为复制集

mongo192.168.25.131:27100

config_replset={_id:"configset",members:[{_id:0,host:"192.168.25.130:27100"},{_id:1,host:"192.168.25.131:27100"},{_id:2,host:" 192.168.25.132:27100"}]}

rs.initiate(config_replset)

1.8 配置mongos(三台机器)  路由

mongos

--configdb configset/192.168.25.130:27100,192.168.25.131:27100,192.168.25.132:27100 --port 27200 --logpath /data/mongodb/mongos/mongos.log --fork

1.9 配置shard(三台机器)  分片

mongod --shardsvr --replSet shard1 --port 27001 --bind_ip 0.0.0.0 --dbpath /data/mongodb/shard1/data --logpath /data/mongodb/shard1/log/shard1.log --directoryperdb --fork

mongod --shardsvr --replSet shard2 --port 27002 --bind_ip 0.0.0.0 --dbpath /data/mongodb/shard2/data --logpath /data/mongodb/shard2/log/shard2.log --directoryperdb --fork

mongod --shardsvr --replSet shard3 --port 27003 --bind_ip 0.0.0.0 --dbpath /data/mongodb/shard3/data --logpath /data/mongodb/shard3/log/shard3.log --directoryperdb --fork

1.10 初始化shard复制集

1.10.1 第一台机器(192.168.25.130没有显示只当主节点,会选择登陆的机器为主节点)

mongo --port 27001

use admin

rs.initiate({

_id: 'shard1',

members: [

{_id: 84, host: '192.168.25.130:27001'},

{_id: 89, host: '192.168.25.131:27001'},

{_id: 90, host: '192.168.25.132:27001'}

]

});

1.10.2 第二台机器(192.168.25.131)

mongo --port 27002

use admin

rs.initiate({

_id: 'shard2',

members: [

{_id: 84, host: '192.168.25.130:27002'},

{_id: 89, host: '192.168.25.131:27002'},

{_id: 90, host: '192.168.25.132:27002'}

]

});

如果要设置仲裁节点的话:

config = {_id: 'shard3', members: [{_id: 0, host: '192.168.10.202:27022'},{_id:1, host: '192.168.10.204:27022'},{_id: 2, host: '192.168.10.203:30001', arbiterOnly:true}]}
rs.initiate(config);

1.10.3 第三台机器(192.168.25.132)

mongo --port 27003

use admin

rs.initiate({

_id: 'shard3',

members: [

{_id: 84,
host: '192.168.25.130:27003'},

{_id: 89,
host: '192.168.25.131:27003'},

{_id: 90,
host: '192.168.25.132:27003'}

]

});

1.11 在mongos中注册shard

Mongo –port 27200

use admin

db.runCommand({addShard: 'shard1/192.168.25.130:27001,192.168.25.131:27001,192.168.25.132:27001'});

db.runCommand({addShard:
'shard2/192.168.25.130:27002,192.168.25.131:27002,192.168.25.132:27002'});

db.runCommand({addShard:
'shard3/192.168.25.130:27003,192.168.25.131:27003,192.168.25.132:27003'});

查看shard

use admin

db.runCommand({listshards: 1});

{

"shards" : [

{

"_id" : "shard1",

"host" :
"shard1/10.199.144.84:27001,10.199.144.89:27001"

},

{

"_id" : "shard2",

"host" :
"shard2/10.199.144.89:27002,10.199.144.90:27002"

},

{

"_id" : "shard3",

"host" :
"shard3/10.199.144.90:27003,10.199.144.84:27003"

}

],

"ok" :
1

}

1.12 插入数据测试

mongo --port 27200

use admin

db.runCommand({enablesharding: 'dbtest'});

db.runCommand({shardcollection: 'dbtest.coll1', key: {id:
1}});

use dbtest

db.coll1.stats()

结果是数据分配不平均,因为id没hash

此处对sano1y库的testtb使用hash策略:

  1. mongos> use
    admin
  2. switched to db admin
  3. mongos> db.runCommand({"enablesharding":"sano1y"})
  4. { "ok" : 1
    }
  5. mongos> db.runCommand({"shardcollection":"sano1y.testtb","key":{"_id":"hashed"}})
  6. { "collectionsharded" : "sano1y.testtb", "ok" : 1
    }

目前为止,已经对sano1y库的testtb集合进行了shard配置。

测试:

  1. mongos> use
    sano1y
  2. switched to db sano1y
  3. mongos> for(i=0;i<100000;i++) {db.testtb.insert({"id":i,"name":"test_hash"});}

稍等片刻,等待插入完毕:

  1. WriteResult({ "nInserted" : 1
    })

进入shard1(187)的PRIMARY实例检查

  1. shard1:PRIMARY> use sano1y
  2. switched to db sano1y
  3. shard1:PRIMARY> db.testtb.find().count()
  4. 49983
  5. shard1:PRIMARY> db.testtb.find()
  6. { "_id" : ObjectId("5837ef1dea1fd54fb38d845c"), "id" : 0, "name" : "test_hash" }
  7. { "_id" : ObjectId("5837ef1dea1fd54fb38d845d"), "id" : 1, "name" : "test_hash" }
  8. { "_id" : ObjectId("5837ef1dea1fd54fb38d845e"), "id" : 2, "name" : "test_hash" }
  9. { "_id" : ObjectId("5837ef1dea1fd54fb38d8460"), "id" : 4, "name" : "test_hash" }
  10. { "_id" : ObjectId("5837ef1dea1fd54fb38d8461"), "id" : 5, "name" : "test_hash" }
  11. { "_id" : ObjectId("5837ef1dea1fd54fb38d8465"), "id" : 9, "name" : "test_hash" }
  12. { "_id" : ObjectId("5837ef1dea1fd54fb38d8468"), "id" : 12, "name" : "test_hash" }
  13. { "_id" : ObjectId("5837ef1dea1fd54fb38d846f"), "id" : 19, "name" : "test_hash" }
  14. { "_id" : ObjectId("5837ef1dea1fd54fb38d8471"), "id" : 21, "name" : "test_hash" }
  15. { "_id" : ObjectId("5837ef1dea1fd54fb38d8475"), "id" : 25, "name" : "test_hash" }
  16. { "_id" : ObjectId("5837ef1dea1fd54fb38d8476"), "id" : 26, "name" : "test_hash" }
  17. { "_id" : ObjectId("5837ef1dea1fd54fb38d8479"), "id" : 29, "name" : "test_hash" }
  18. { "_id" : ObjectId("5837ef1dea1fd54fb38d847d"), "id" : 33, "name" : "test_hash" }
  19. { "_id" : ObjectId("5837ef1dea1fd54fb38d847e"), "id" : 34, "name" : "test_hash" }
  20. { "_id" : ObjectId("5837ef1dea1fd54fb38d8480"), "id" : 36, "name" : "test_hash" }
  21. { "_id" : ObjectId("5837ef1dea1fd54fb38d8481"), "id" : 37, "name" : "test_hash" }
  22. { "_id" : ObjectId("5837ef1dea1fd54fb38d8483"), "id" : 39, "name" : "test_hash" }
  23. { "_id" : ObjectId("5837ef1dea1fd54fb38d8486"), "id" : 42, "name" : "test_hash" }
  24. { "_id" : ObjectId("5837ef1dea1fd54fb38d848b"), "id" : 47, "name" : "test_hash" }
  25. { "_id" : ObjectId("5837ef1dea1fd54fb38d848d"), "id" : 49, "name" : "test_hash" }

另外如果到shard2可以看到shard1这些不连续的id。
可发现shard1和2中的document数量,还是比较均匀的。

shard1: 33320
shard2: 33421

Shard3: 33259

可以看出,count数量基本还是保持平衡的,又很小的落差

mongo复制集、分片集(亲测)的更多相关文章

  1. mongo 3.4分片集群系列之八:分片管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  2. mongo 3.4分片集群系列之七:配置数据库管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  3. mongo 3.4分片集群系列之三:搭建分片集群--哈希分片 + 安全

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  4. mongo 3.4分片集群系列之六:详解配置数据库

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  5. mongo 3.4分片集群系列之五:详解平衡器

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  6. mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  7. mongo 3.4分片集群系列之二:搭建分片集群--哈希分片

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  8. mongo 3.4分片集群系列之一:浅谈分片集群

    这篇为理论篇,稍后会有实践篇. 这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mong ...

  9. mongoDB副本集+分片集群

    首先搭建一个副本集(三台机器) 主,从,仲裁 然后搭建分片shard1,在每台机子上启用shard1(这里就写一个分片吧!!如果写多了怕初学者会混乱,先写一个.然后可以按照同样的方法写第二个,第三个) ...

  10. Cocos Creator JS web平台复制粘贴代码(亲测可用)

    Cocos Creator JS web平台复制粘贴代码(亲测可用) 1 webCopyString: function(str){ var input = str; const el = docum ...

随机推荐

  1. IDEA集成 SpringBoot+Mybaties 之 @Autowired注入报错

    原因分析: 因为@Mapper注解是由ibates提供的,需要在application.yml里加上下图配置 以及在启动类入口加上 扫描你mapper接口所在的包 ,所以Spring容器是不认识这个注 ...

  2. python模块之matplotlib

    官方网址:http://matplotlib.org/tutorials/introductory/lifecycle.html#sphx-glr-tutorials-introductory-lif ...

  3. javaweb之jsp标签

    1.JSP标签简介 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 2.JSP常用标签 ...

  4. js两个字符串明明一样却判断显示不相等

    一.问题 两个字符串看起来一样.类型一样,判断str1==str2时返回false: 二.原因 字符串可能含有其他特殊字符:换行符(%D).空格(%20)...一般不显示. 三.如何判断 encode ...

  5. flask接收前台的ajax的post数据

    html <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8& ...

  6. 边缘检测matlab算法汇总

    边缘检测matlab算法汇总 1.      基于一阶微分算子检测边缘图像 一阶微分边缘算子又称梯度边缘算子,它是利用图像在边缘处的阶跃性,及图像梯度在边缘去得极大值得特征性进行边缘检测. Sobel ...

  7. codeforces之始

    很早就听说acmer界的CF嘞!还记得刚开始听到神犇们在讨论CF的时候我还以为是网游CF(穿越火线)呢... 今年刚开学的时候就打算开始打cf的,由于一些事情耽搁了.之后又要准备省赛所以就一直拖到现在 ...

  8. 【Udacity】解决:字幕遮挡视频内容怎么办?Udacity字幕大小调整

    字幕有没有办法显示在最下方,否则把内容挡住了,根本看不清老师所指的内容 Chrome 的小插件,能方便地调节视频的字幕大小,譬如:Udacity 的学习视频字幕大小不能调节,有时候不想它占太多位置,而 ...

  9. 弧形菜单(Android)

    弧形菜单(Android) 前言:公司需求,自己写的一个弧形菜单! 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 涉及知识:1.自定义控件,2.事件分发等 部分代 ...

  10. metasploit 连接database相关问题

    我们首先去这个目录下看database.yml文件内容: 下图是我们看到的的信息 接着打开metasploit,运行db_connect 指令链接数据库.格式为: db_connect 用户名:密码@ ...