设为首页 收藏本站
查看: 1325|回复: 0

[经验分享] MongoDB 分片(Cluster)

[复制链接]

尚未签到

发表于 2018-10-26 08:52:20 | 显示全部楼层 |阅读模式
  基本环境:
  由于资源紧张,只有3台虚拟机的关系,只做两个replicaSet,每台机器配置分别如下:
  10.10.1.55这台机器安装 Primary1,configServer1, Arbiter1
  10.10.1.56 安装 Primary2,configServer2, Arbiter2
  10.10.1.57 安装 Secondary1,Secondary2,configServer3,mongos
  1.55机器的配置文件如下:
  Primary1的conf文件:
dbpath=/data/mongodb/rs0_0  
logpath=/data/mongodb/log/rs0_0.log
  
logappend=true
  
port=40000
  
bind_ip=192.168.11.55,10.10.1.55
  
oplogSize=10000
  
fork=true
  
journal = true
  
#noprealloc = true
  
replSet=rs0
  
directoryperdb=true
  Arbiter1的配置文件:
dbpath=/data/mongodb/rs0_arbiter  
logpath=/data/mongodb/log/rs0_arbiter.log
  
logappend=true
  
port=40002
  
bind_ip=192.168.11.55,10.10.1.55
  
oplogSize=10000
  
fork=true
  
journal = true
  
#noprealloc = true
  
replSet=rs0
  
directoryperdb=true
  ConfigServer1的配置文件:
dbpath=/data/mongodb/rs0_conf  
logpath=/data/mongodb/log/rs0_conf.log
  
logappend=true
  
port=40006
  
bind_ip=192.168.11.55,10.10.1.55
  
fork=true
  
journal = true
  
#noprealloc = true
  
configsvr=true
  
directoryperdb=true
  分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55 机器上分别分配了Primary1端口:40000,Arbiter1端口:40002,configureServer1端口:40006
  1.56机器的配置:
  Primary2的配置文件:
dbpath=/data/mongodb/rs1_primary  
logpath=/data/mongodb/log/rs1_p.log
  
logappend=true
  
bind_ip=192.168.11.56,10.10.1.56
  
directoryperdb=true
  
port=40003
  
oplogSize=10000
  
fork=true
  
journal = true
  
noprealloc = true
  
replSet=rs1
  Arbiter2配置文件:
dbpath=/data/mongodb/rs1_arbiter  
logpath=/data/mongodb/log/rs1_a.log
  
logappend=true
  
bind_ip=192.168.11.56,10.10.1.56
  
directoryperdb=true
  
port=40005
  
oplogSize=10000
  
fork=true
  
journal = true
  
noprealloc = true
  
replSet=rs1
  ConfigureServer2配置文件:
dbpath=/data/mongodb/rs1_conf  
logpath=/data/mongodb/log/rs1_conf.log
  
logappend=true
  
bind_ip=192.168.11.56,10.10.1.56
  
directoryperdb=true
  
port=40007
  
oplogSize=10000
  
fork=true
  
journal = true
  
noprealloc = true
  
configsvr=true
  分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55
机器上分别分配了Primary2端口:40003,Arbiter2端口:40005,configureServer2端口:40007
  1.56机器的配置:
  rs0_Secondary1配置:
dbpath=/data/mongodb/rs0_secondary1  
logpath=/data/mongodb/log/rs0_secondary1.log
  
logappend=true
  
port=40001
  
bind_ip=192.168.11.57,10.10.1.57
  
oplogSize=10000
  
fork=true
  
journal = true
  
#noprealloc = true
  
replSet=rs0
  
directoryperdb=true
  rs1_Secondary1配置:
dbpath=/data/mongodb/rs1_secondary1  
logpath=/data/mongodb/log/rs1_secondary1.log
  
logappend=true
  
bind_ip=192.168.11.57,10.10.1.57
  
directoryperdb=true
  
port=40004
  
oplogSize=10000
  
fork=true
  
journal = true
  
noprealloc = true
  
replSet=rs1
  configureServer3配置:
dbpath=/data/mongodb/confSvr3  
logpath=/data/mongodb/log/conf3.log
  
logappend=true
  
bind_ip=192.168.11.57,10.10.1.57
  
directoryperdb=true
  
port=40008
  
oplogSize=10000
  
fork=true
  
journal = true
  
configsvr=true
  mongos 配置:(启动mongos路由器要注意多台服务器时间必须要同步,否则出现错误)
logpath=/data/mongodb/log/mongos.log  
port = 40009
  
configdb=10.10.1.55:40006,10.10.1.56:40007,10.10.1.57:40008
  
fork = true
  分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55
机器上分别分配了rs0_secondary1端口:40001,rs1_secondary1端口:40004,configureServer3端口:40008,mongos路由端口:40009
  现在用mongo shell登录primary1 配置replicaSet0,步骤如下:
  cfg={ "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "10.10.1.55:40000" }, { "_id" : 1, "host" : "10.10.1.57:40001" } ] }
  rs.initiate(cfg)
  rs.status()
  rs.addArb("10.10.1.55:40002")
  现在用mongo shell登录primary2 配置replicaSet1,步骤如下:
  cfg={ "_id" : "rs1", "members" : [ { "_id" : 0, "host" : "10.10.1.56:40003" }, { "_id" : 1, "host" : "10.10.1.57:40004" } ] }
  rs.initiate(cfg)
  rs.status()
  rs.addArb("10.10.1.56:40005")
  使用mongo shell登录mongos路由添加分片信息:
mongo --host 10.10.1.57 --port 40009  
mongos> sh.addShard("rs0/10.10.1.55:40000,10.10.1.57:40001")
  
{ "shardAdded" : "rs0", "ok" : 1 }
  
mongos> sh.addShard("rs1/10.10.1.56:40003,10.10.1.57:40004")
  
{ "shardAdded" : "rs1", "ok" : 1 }
  
mongos> sh.status()
  
--- Sharding Status ---
  
  sharding version: {
  
    "_id" : 1,
  
    "version" : 4,
  
    "minCompatibleVersion" : 4,
  
    "currentVersion" : 5,
  
    "clusterId" : ObjectId("561c7bdd4315b18f9862adb4")
  
}
  
  shards:
  
    {  "_id" : "rs0",  "host" : "rs0/10.10.1.55:40000,10.10.1.57:40001" }
  
    {  "_id" : "rs1",  "host" : "rs1/10.10.1.56:40003,10.10.1.57:40004" }
  
  databases:
  
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
  现在创建一个新的数据库来测试一下分片:
  mongos> use people
  switched to db people
  mongos> for(var i=1;i for(var i=1;i for(var i=1;i for(var i=1;i for(var i=1;i db.customers.ensureIndex({country:1,_id:1})  
{
  
    "raw" : {
  
        "rs0/10.10.1.55:40000,10.10.1.57:40001" : {
  
            "createdCollectionAutomatically" : false,
  
            "numIndexesBefore" : 1,
  
            "numIndexesAfter" : 2,
  
            "ok" : 1
  
        }
  
    },
  
    "ok" : 1
  
}
  
mongos> sh.shardCollection("people.customers",{country:1,_id:1})
  
{ "collectionsharded" : "people.customers", "ok" : 1 }
  
mongos> sh.status()
  
--- Sharding Status ---
  
  sharding version: {
  
    "_id" : 1,
  
    "version" : 4,
  
    "minCompatibleVersion" : 4,
  
    "currentVersion" : 5,
  
    "clusterId" : ObjectId("561c7bdd4315b18f9862adb4")
  
}
  
  shards:
  
    {  "_id" : "rs0",  "host" : "rs0/10.10.1.55:40000,10.10.1.57:40001" }
  
    {  "_id" : "rs1",  "host" : "rs1/10.10.1.56:40003,10.10.1.57:40004" }
  
  databases:
  
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
  
    {  "_id" : "test",  "partitioned" : false,  "primary" : "rs0" }
  
    {  "_id" : "people",  "partitioned" : true,  "primary" : "rs0" }
  
        people.customers
  
            shard key: { "country" : 1, "_id" : 1 }
  
            chunks:
  
                rs0    1
  
            { "country" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "country" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)
  现在由于数据量不多只有一个分片在rs0上,可以通过增加数据量来提高分片:
for(var i=10;i> { "country" : "American", "_id" : ObjectId("561c7da73af7c7865defefb1") } on : rs1 Timestamp(2, 0)
  { "country" : "American", "_id" : ObjectId("561c7da73af7c7865defefb1") } -->> { "country" : "UK", "_id" : ObjectId("561c7db63af7c7865defefd4") } on : rs0 Timestamp(3, 1)
  { "country" : "UK", "_id" : ObjectId("561c7db63af7c7865defefd4") } -->> { "country" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : rs1 Timestamp(3, 0)
  现在rs0上有一个分片,rs1上有两个分片



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-626568-1-1.html 上篇帖子: MongoDB 简单复制配置 下篇帖子: 【未完】mongodb安装+副本集搭建+数据导入
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表