Mongodb分片(replcation set)笔记

本文介绍了MongoDB分片集群的部署配置流程,包括配置服务器、分片服务器及路由服务器的具体参数设定,以及如何初始化副本集、添加分片并进行分片操作。

配置

  • 版本:mongodb-linux-x86_64-rhel70-3.4.15
  • 服务器:CentOS Linux release 7.3.1611 (Core)
服务器信息
IPPortreplSetNameclusterRole备注
127.0.0.127020rs-mongosvrconfigsvr[固定值]配置服务器1
127.0.0.127021rs-mongosvrconfigsvr[固定值]配置服务器2
127.0.0.127018none[只分片]shardsvr[固定值]分片服务器1
127.0.0.127019none[只分片]shardsvr[固定值]分片服务器2
127.0.0.127030none[单路由]none路由服务器

测试配置文件内容如下:

  • 配置服务器1
systemLog:
   destination: file
   path: "logs20/mongod.log"
   logAppend: true
storage:
   dbPath: "data20"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27020
setParameter:
   enableLocalhostAuthBypass: false
#security:
#    authorization: "enabled"
replication:
    replSetName: rs-mongosvr
sharding:
   clusterRole: configsvr
  • 配置服务器2
systemLog:
   destination: file
   path: "logs21/mongod.log"
   logAppend: true
storage:
   dbPath: "data21"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27021
setParameter:
   enableLocalhostAuthBypass: false
#security:
#    authorization: "enabled"
replication:
    replSetName: rs-mongosvr
sharding:
   clusterRole: configsvr
  • 分片服务器1
systemLog:
   destination: file
   path: "logs18/mongod.log"
   logAppend: true
storage:
   dbPath: "data18"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27018
#security:
#    authorization: "enabled"
#replication:
#    replSetName: rs-mongodb
sharding:
   clusterRole: shardsvr
  • 分片服务器2
systemLog:
   destination: file
   path: "logs19/mongod.log"
   logAppend: true
storage:
   dbPath: "data19"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27019
#security:
#    authorization: "enabled"
#replication:
#    replSetName: rs-mongodb
sharding:
   clusterRole: shardsvr
  • 路由服务器
systemLog:
   destination: file
   path: "logs30/mongods.log"
   logAppend: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27030
sharding:
   configDB: rs-mongosvr/127.0.0.1:27020,127.0.0.1:27021

启动过程:

  1. 启动配置服务器,使用相同【的副本集名称】和【分片角色】
rs.initiate(
  {
    _id: "rs-mongosvr",
    configsvr: true,
    members: [
      { _id : 0, host : "127.0.0.1:27020" },
      { _id : 1, host : "127.0.0.1:27021" }
    ]
  }
)

其他命令使用:rs.help()查看

  1. 启动分片服务器,暂时不使用副本集,所以只需要设置【分片角色】

  2. 启动路由服务器,连接路由服务器端口进行配置:

  3. 配置路由服务器

    1. sh.addShard( "127.0.0.1:27018")``````sh.addShard( "127.0.0.1:27019")
    2. sh.enableSharding("<database>")
    3. 对一个collection进行分片:
      • 注意如果collection已经有数据,则必须在对collection分片之前使用db.collection.createIndex()在片键上建立索引,如果collection是空的则直接对collection进行分片,MongoDB会自动建立索引。
      1. Hashed:sh.shardCollection("<database>.<collection>", { <shard key> : "hashed" } )
      2. Ranged:sh.shardCollection("<database>.<collection>", { <shard key> : <direction> } )

手动预先分块:

for(var i=1;i<=10;i++)
{
    sh.splitAt('test.user',{user_id:i*10})
}

意思是在user表上进行预先分块,每当user_iduser_id必须是片键)为10,20,30…的时候进行分块,这样在还没有数据的时候块就已经分好了,只等着数据填进来,避免了trunk在服务器上移动导致IO开销增加。


结合复制集

如果需要给分片加上复制集,需要添加如下两个步骤:

  1. 按照正常方式给每个分片服务器添加各自的副本集。
  2. 在路由服务器添加分片服务器的时候需要使用:sh.addShard( "<replSetName>/s1-mongo1.example.net:27017")(添加的是primary)来添加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值