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
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
连接各副本,分别配置
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"}]}
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"}]}
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'})
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理