MongoDB分片集群配置
配置服务器配置
dbpath = /mongodb_shard01_config/data
configsvr = true
port = 32000
logpath = /mongodb_shard01_config/log/mongodb.logs
replSet = dakj_mongodb
// 192.168.1.101
sudo docker run -itd --name mongodb_shard01_config -p 32000:32000 -v /mongodb_shard01_config:/mongodb_shard_config mongo mongod --config /mongodb_shard_config/config.conf
// 192.168.1.102
sudo docker run -itd --name mongodb_shard01_config -p 32000:32000 -v /mongodb_shard01_config:/mongodb_shard_config mongo mongod --config /mongodb_shard_config/config.conf
// 192.168.1.103
sudo docker run -itd --name mongodb_shard01_config -p 32000:32000 -v /mongodb_shard01_config:/mongodb_shard_config mongo mongod --config /mongodb_shard_config/config.conf
登录任意配置节点
./mongo 192.168.1.101:32000
use admin
var rsc = {_id:'dakj_mongodb',members:[
{_id:0,host:'192.168.1.101:32000',priority:1},
{_id:1,host:'192.168.1.102:32000',priority:2},
{_id:2,host:'192.168.1.103:32000',priority:3}
]};
rs.initiate(rsc);
路由配置
logpath = /mongodb_shard01_mongos/log/mongodb.logs
port = 31000
configdb = dakj_mongodb/192.168.1.101:32000,192.168.1.102:32000,192.168.1.103:32000
// 192.168.1.101
sudo docker run -itd --name mongos01 -p 31000:31000 -v /mongodb_shard01_mongos:/mongodb_shard01_mongos mongo mongos --port=31000 --config /mongodb_shard01_mongos/mongos.conf
// 192.168.1.102
sudo docker run -itd --name mongos01 -p 31000:31000 -v /mongodb_shard01_mongos:/mongodb_shard01_mongos mongo mongos --port=31000 --config /mongodb_shard01_mongos/mongos.conf
// 192.168.1.103
sudo docker run -itd --name mongos01 -p 31000:31000 -v /mongodb_shard01_mongos:/mongodb_shard01_mongos mongo mongos --port=31000 --config /mongodb_shard01_mongos/mongos.conf
分片+副本集配置
port = 27017
shardsvr=true
#fork = true
#keyFile = /mongodb_conf/autokey
replSet = mongodb_shard02
logpath = /mongodb_shard_config/logs/dakj_mongodb_log.log
logappend = true
dbpath=/data/db
bind_ip_all=true
sudo docker run -itd --name mongodb_shard01 -p 30010:27017 -v /mongodb_shard01:/data/db -v /mongodb_shard_config:/mongodb_shard_config mongo mongod -f /mongodb_shard_config/mongodb.conf
sudo docker run -itd --name mongodb_shard02 -p 30011:27017 -v /mongodb_shard02:/data/db -v /mongodb_shard02_config:/mongodb_shard_config mongo mongod -f /mongodb_shard_config/mongodb.conf
sudo docker run -itd --name mongodb_shard03 -p 30012:27017 -v /mongodb_shard03:/data/db -v /mongodb_shard03_config:/mongodb_shard_config mongo mongod -f /mongodb_shard_config/mongodb.conf
连接各副本,分别配置
//shard01
config = {"_id" : "mongodb_shard01","members" : [{"_id" : 0,"host" : "192.168.1.101:30010"},{"_id" : 1,"host" : "192.168.1.102:30010"},{"_id" : 2,"host" : "192.168.1.103:30010"}]}
//shard02
config = {"_id" : "mongodb_shard02","members" : [{"_id" : 0,"host" : "192.168.1.101:30011"},{"_id" : 1,"host" : "192.168.1.102:30011"},{"_id" : 2,"host" : "192.168.1.103:30011"}]}
//shard03
config = {"_id" : "mongodb_shard03","members" : [{"_id" : 0,"host" : "192.168.1.101:30012"},{"_id" : 1,"host" : "192.168.1.102:30012"},{"_id" : 2,"host" : "192.168.1.103:30012"}]}
rs.initiate(config)
sh.addShard("mongodb_shard01/192.168.1.101:30010,192.168.1.102:30010,192.168.1.103:30010")
sh.addShard("mongodb_shard02/192.168.1.101:30011,192.168.1.102:30011,192.168.1.103:30011")
sh.addShard("mongodb_shard03/192.168.1.101:30012,192.168.1.102:30012,192.168.1.103:30012")
配置数据库和集合启用及分片
use dakj_yqxt
db.yqxt_article.ensureIndex({'url_md5':1}, {'unique': true})
db.category_article.ensureIndex({'url_md5':'hashed'})
db.category_article.ensureIndex({'category_id':'hashed', 'ctime':-1})
db.warn_article.ensureIndex({'url_md5':'hashed'})
db.warn_article.ensureIndex({'category_id':'hashed', 'ctime':-1})
sh.enableSharding('dakj_yqxt')
sh.shardCollection('dakj_yqxt.yqxt_article', {'url_md5':'hashed'})
sh.shardCollection('dakj_yqxt.category_article', {'url_md5':'hashed'})
sh.shardCollection('dakj_yqxt.warn_article', {'url_md5':'hashed'})