docker搭建mongodb分片集群
2021/5/1 2:25:19
本文主要是介绍docker搭建mongodb分片集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
准备好mongodb镜像,网络,keyfile文件,在基础镜像mongo的基础上创建包含keyfile文件的镜像mongo:v1
目标是搭建一个6节点的分片集群,该集群由两个副本集构成,包括:一个配置服务器副本集(三节点),一个分片副本集(三节点),一个mongos
创建配置服务器副本集节点:
docker run -d --name mongo-cfg01 --network 10.0.0.0/24 --ip 10.0.0.211 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --configsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile docker run -d --name mongo-cfg02 --network 10.0.0.0/24 --ip 10.0.0.212 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --configsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile docker run -d --name mongo-cfg03 --network 10.0.0.0/24 --ip 10.0.0.213 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --configsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile
进入任一节点,比如,mongo-cfg01
连接数据库
mongo --port 27019 -u admin
初始化配置服务器副本集
rs.initiate( { _id: "cluster01", configsvr: true, members: [ { _id : 0, host : "10.0.0.211:27019" }, { _id : 1, host : "10.0.0.212:27019" }, { _id : 2, host : "10.0.0.213:27019" } ] } )
结果:
{ "ok" : 1, "$gleStats" : { "lastOpTime" : Timestamp(1619790210, 1), "electionId" : ObjectId("000000000000000000000000") }, "lastCommittedOpTime" : Timestamp(0, 0) }
然后,使用rs.conf(),rs.status()等等查看副本集状态
创建分片副本集:
docker run -d --name mongo-rep01 --network 10.0.0.0/24 --ip 10.0.0.221 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --shardsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile docker run -d --name mongo-rep02 --network 10.0.0.0/24 --ip 10.0.0.222 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --shardsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile docker run -d --name mongo-rep03 --network 10.0.0.0/24 --ip 10.0.0.223 -v /root/keyfile:/etc/keyfile -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123.com mongo:v1 mongod --shardsvr --replSet "cluster01" --bind_ip_all --keyFile /etc/keyfile
进入任一节点,比如:mongo-rep01,连接数据库
mongo --port 27018 -u admin
初始化分片副本集
rs.initiate( { _id : "cluster01", members: [ { _id : 0, host : "10.0.0.221:27018" }, { _id : 1, host : "10.0.0.222:27018" }, { _id : 2, host : "10.0.0.223:27018" } ] } )
结果:
{ "ok" : 1 }
为集群启动一个mongos
mongos --configdb cfg/10.0.0.211:27019,10.0.0.212:27019,10.0.0.213:27019 --keyFile /etc/keyfile
结果滚动显示如下内容:
{"t":{"$date":"2021-04-30T15:00:27.378+00:00"},"s":"I", "c":"SH_REFR", "id":24105, "ctx":"ConfigServerCatalogCacheLoader-1","msg":"Collection has found to be unsharded after refresh","attr":{"namespace":"config.system.sessions","durationMillis":0}} {"t":{"$date":"2021-04-30T15:00:27.379+00:00"},"s":"I", "c":"CONTROL", "id":20714, "ctx":"LogicalSessionCacheRefresh","msg":"Failed to refresh session cache, will try again at the next refresh interval","attr":{"error":"NamespaceNotSharded: Collection config.system.sessions is not sharded."}} {"t":{"$date":"2021-04-30T15:00:27.379+00:00"},"s":"I", "c":"SH_REFR", "id":24105, "ctx":"ConfigServerCatalogCacheLoader-1","msg":"Collection has found to be unsharded after refresh","attr":{"namespace":"config.system.sessions","durationMillis":0}} {"t":{"$date":"2021-04-30T15:00:27.380+00:00"},"s":"I", "c":"CONTROL", "id":20712, "ctx":"LogicalSessionCacheReap","msg":"Sessions collection is not set up; waiting until next sessions reap interval","attr":{"error":"NamespaceNotSharded: Collection config.system.sessions is not sharded."}} {"t":{"$date":"2021-04-30T15:00:35.473+00:00"},"s":"I", "c":"SHARDING", "id":20997, "ctx":"Uptime-reporter","msg":"Refreshed RWC defaults","attr":{"newDefaults":{}}} {"t":{"$date":"2021-04-30T15:00:45.994+00:00"},"s":"I", "c":"SHARDING", "id":20997, "ctx":"Uptime-reporter","msg":"Refreshed RWC defaults","attr":{"newDefaults":{}}} {"t":{"$date":"2021-04-30T15:00:57.478+00:00"},"s":"I", "c":"SHARDING", "id":20997, "ctx":"Uptime-reporter","msg":"Refreshed RWC defaults","attr":{"newDefaults":{}}}
添加分片集到集群
通过mongo连接到运行mongos的节点,连接数据库,并执行如下命令添加分片集到集群:
sh.addShard( "cluster01/10.0.0.221:27018,10.0.0.222:27018,10.0.0.223:27018")
结果:
mongos> sh.addShard( "cluster01/10.0.0.221:27018,10.0.0.222:27018,10.0.0.223:27018") { "shardAdded" : "cluster01", "ok" : 1, "operationTime" : Timestamp(1619794806, 3), "$clusterTime" : { "clusterTime" : Timestamp(1619794806, 3), "signature" : { "hash" : BinData(0,"LfNdXcSRkS5TyA1rh9qJrdZzOt0="), "keyId" : NumberLong("6956960684298993673") } } }
查看分片集状态:
mongos> sh.status() --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("608c16e2c27b8b7491ff6915") } shards: { "_id" : "cluster01", "host" : "cluster01/10.0.0.221:27018,10.0.0.222:27018,10.0.0.223:27018", "state" : 1 } active mongoses: "4.4.5" : 1 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: cluster01 1024 too many chunks to print, use verbose if you want to force print
这篇关于docker搭建mongodb分片集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-19Docker-Compose容器集群化项目实战:新手入门指南
- 2024-11-19Docker镜像仓库项目实战:新手入门教程
- 2024-11-19Docker容器化部署项目实战:新手入门教程
- 2024-11-19Docker-Compose容器集群化资料入门教程
- 2024-11-19Docker镜像仓库资料详解:新手入门教程
- 2024-11-19Docker容器化部署资料:新手入门指南
- 2024-11-19Docker-Compose容器集群化教程:从入门到实践
- 2024-11-19Docker镜像仓库教程:新手入门指南
- 2024-11-19Docker容器化部署教程:初学者指南
- 2024-11-18Docker-Compose容器集群化入门教程