mongodb分片配置

1 配置replica_set


replica_set有三个角色:primary(主库),secondary(从库),arbiter(仲裁)。arbiter在选举主库时,进行投票,不进行数据存储。
(1)配置主库:
配置文件如下:
logpath=/opt/log/mongo/replica_set_0_primary
logappend=true
journal = true
quiet = true
fork = true
port = 27018
dbpath=/opt/db/replica_set_0/pdb
replSet = replica_set_0
oplogSize = 512#<MB>

启动主库:mongod --config /etc/rds/primary.conf

(2)配置从库:
配置文件如下:
logpath=/opt/log/mongo/replica_set_0_second
dbpath=/opt/db/replica_set_0/sdb
replSet = replica_set_0
logappend=true
journal = true
quiet = true
fork = true
port = 27019
oplogSize = 512#<MB>

启动从库:mongod --config /etc/rds/secondary.conf

(3)配置arbiter:
配置文件如下:
replSet = replica_set_0
logpath=/opt/log/mongo/replica_set_0_arbiter
logappend=true
journal = true
quiet = true
fork = true
port = 27020
dbpath=/opt/db/replica_set_0/adb
oplogSize = 512#<MB>
noprealloc = true

启动arbiter:mongod --config /etc/rds/arbiter.conf

(4)初始化replica_set
javascript脚本如下:
var conf = {
"_id" : "replica_set_0",
"members" : [
{
"_id" : 0,
"host" : "pri.example.net:27018"
},
{
"_id" : 1,
"host" : "sec.example.net:27019"
},
{
"_id" : 2,
"host" : "arb.example.net:27020",
"arbiterOnly": true
}
]
}
printjson(rs.initiate(conf));
printjson(rs.conf());

连接主库,运行js文件:mongo -h pri.example.com:27018 replica_set.js
2 配置Config Server
Config Server存储cluster的配置数据。生产环境下,应该配置3个。
配置文件如下:
logpath=/opt/log/mongo/configsvr2
dbpath=/opt/db/cdb2
port = 27042
configsvr = true
fork = true
journal = true
quiet = true
logappend=true
oplogSize = 512#<MB>

启动Config Server : mongod -f /etc/mongo/configsvr2.conf

3 配置mongos
配置文件如下:
configdb=conf1.example.net:27022,conf2.example.net:27032,conf3.example.net:27042
logpath=/opt/log/mongo/mongos
pidfilepath=/var/run/mongo/mongos.pid
logappend=true
fork = true
port = 27017
chunkSize = 200

4 add shard
shard可以是单独的mongodb,也可以是replica_set。
sh.addShard("replica_set_0/pri.example.net:27018,sec.example.net:27019,arb.example.net:27020");

5 Enable Sharding for a Database
db.runCommand({"enablesharding":"db_test"})

6 Enable Sharding for a Collection
db.runCommand({"shardcollection":"db_test.t_test", "key":{"myid":1}})

7 split chunk
划分chunk。
db.runCommand({ split : "db_test.t_test" , middle : { myid : 5000000 }})
db.runCommand({ split : "db_test.t_test" , middle : { myid : 10000000 }})
db.runCommand({ split : "db_test.t_test" , middle : { myid : 15000000 }})
db.runCommand({ split : "db_test.t_test" , middle : { myid : 20000000 }})

8 movechunk
在shard之间移动chunk。
db.runCommand( { moveChunk : <namespace> ,
find : <query> ,
to : <string>,
_secondaryThrottle : <Boolean>,
_waitForDelete : <Boolean> } )

 

转载于:https://www.cnblogs.com/yunfeichen119/p/3459871.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值