Mongodb Sharding+Replica Set

mongodb 集群一般是shard(分片)+ replicateSet(副本集)+mongos(路由)+config(配置服务)

mongos :负责路由。所有的对mongodb集群 请求操作都有它来负责分发。一个集群中至少有一台mongos ,当然看自己心情,你可以多弄几台。这样在项目中用mongo 做为datasource 你就可以多配几个地址。一个路由挂掉了可以自动选择其他路由.

config:  配置服务器。负责记录集群中的配置信息,如一个集群由哪里分片组成,分片管理权限用户等等。
 
iptables -I  INPUT -p tcp  --dport 27017 -j ACCEPT
iptables -I  INPUT -p tcp  --dport 27018 -j ACCEPT
iptables -I  INPUT -p tcp  --dport 27019 -j ACCEPT
 
mongodb 集群一般是shard(分片)+ replicateSet(副本集)+mongos(路由)+config(配置服务)
 
环境 3个分片
mongos 192.168.0.10:27019,192.168.0.11:27019,192.168.0.12:27019
config 192.168.0.1:27018,192.168.0.2:27018,192.168.0.3:27018
 
sharding:shard001、shard002、shard003
replSet:shard001  192.168.1.1:27017, 192.168.1.2:27017, 192.168.1.3:27017
replSet:shard002  192.168.1.4:27017, 192.168.1.5:27017, 192.168.1.6:27017
replSet:shard003  192.168.1.7:27017, 192.168.1.8:27017, 192.168.1.9:27017
 
一、配置-路由 mongos
#/opt/db/confg/mongo.conf
 
fork=true
quiet=true
configdb=192.168.0.1:27018,192.168.0.2:27018,192.168.0.3:27018
port=27019
pidfilepath=/opt/db/mongo/mongos.pid
logpath=/var/log/mongo/mongos.log
maxConns=20000
 
二、配置-配置服务器 config
#/opt/db/confg/mongo.conf
fork=true
quiet=true
port=27018
dbpath=/opt/db/mongo/
logpath=/var/log/mongo/mongoconfig.log
pidfilepath=/opt/db/mongo/mongoconfig.pid
logappend=true
configsvr=true
directoryperdb=true
maxConns=20000
 
三、配置每个分片的副本集的配置文件
可参见<副本集--MongoDB副本集(Linux)>
再在配置中加入 shardsvr=true
即:
#配置文件mongodb.conf存放路径为 /opt/confg/mongo/mongodb.conf
 
dbpath=/opt/mongodb/data/
logpath=/var/log/mongodb/mongodb.log
pidfilepath=/opt/mongodb/data/mongodb.pid
logappend=true
fork=true
port=27017
quiet=true
directoryperdb=true
replSet=shard001
oplogSize=10240 #(MB单位)
maxConns=20000
shardsvr=true
 
 
四、先启动 config (mongod)
runuser -c "/opt/db/mongo/bin/mongod -f /opt/db/confg/mongo.conf" mongod
五、再启动 mongos
runuser -c "/opt/db/mongo/bin/mongos -f /opt/db/confg/mongo.conf" mongod
 
六、登录mongs 初始化分片关系
mongos>use admin
mongos>db.adminCommand({"addShard":"shard001/192.168.1.1:27017,192.168.1.2:27017,192.168.1.3:27017"});
mongos>db.adminCommand({"addShard":"shard002/192.168.1.4:27017,192.168.1.5:27017,192.168.1.6:27017"});
mongos>db.adminCommand({"addShard":"shard003/192.168.1.7:27017,192.168.1.8:27017,192.168.1.9:27017"});
七、指定库 集合分片
mongos>use admin;
mongos>sh.enableSharding("dbname") 或 db.adminCommand({"enableSharding":"dbname"});
mongos>use dbname;
mongos>db.collectionName.ensureIndex({Date:1});
mongos>use admin;
mongos>sh.shardCollection("dbname.collectionName",{Date:1});
八、验证分片
sh.status();
九、设置Balancer时间窗口
查看均衡器在哪个机器则:
mongos>use config
mongos>db.locks.findOne({"_id":"balancer"});
启动均衡器
mongos>use config
mongos>sh.setBalancerState(true);
configsvr>use admin
configsvr>db.settings.update({_id:"balancer"},{$set:{stopped:false}},{upsert:true})
 
关闭均衡器
mongos>sh.setBalancerState(false);
mongos>sh.stopBalancer()
configsvr>use admin
configsvr>db.settings.update({_id:"balancer"},{$set:{stopped:true}},{upsert:true});
 
设置时间窗口
mongos>db.settings.update({"_id":"balancer"},{$set:{activeWindow:{start:"23:00",stop:"8:00"}}},true);
均衡器只是用块的数量来,非数据的大小,作为衡量分片间是否均衡的指标。
 
 
在开机服务中加入以上相关服务.例如
vim /etc/rc.local
runuser -c "/opt/mongo/bin/mongod -f /opt/mongo/conf/mongod.conf" mond #启动mongod 
runuser -c "/opt/sharding/mongo-conf/bim/mongod -f /opt/sharding/mongo-conf/conf/mongod.conf" mongod
runuser -c "/opt/sharding/mongo-mongos/bim/mongod -f /opt/sharding/mongo-mongos/conf/mongod.conf" mongod
 
分片中的不同复制集的端口可以不唯一;即可以使用不同的端口
posted @ 2016-01-27 17:00  Captains-Felix  阅读(1395)  评论(0编辑  收藏  举报