[Database] Mongodb 分片集群

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

分片+副本集配置

  • 分片配置文件(mongodb.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'})

posted @ 2021-11-15 13:49  ABeen  阅读(51)  评论(0编辑  收藏  举报