docker部署mongo4.2集群,复制和分片
无聊,自建轮子
共两部分 1复制集 2分片集
1复制集
创建mongo镜像,dockerfile如下
from centos:7
RUN yum install net-tools vim -y
RUN mkdir -p /mongodb/{bin,data,log,conf} \
&& touch /mongodb/log/mongodb.log \
&& chmod 755 /mongodb -R
ENV MONGOD_HOME /mongodb
ENV PATH $MONGOD_HOME/bin:$PATH
COPY ./mongodb/bin/ /mongodb/bin/
COPY mongod.conf /mongodb/conf/mongod.conf
COPY run.sh /mongodb/bin/run.sh
RUN chmod a+x /mongodb/bin/run.sh
CMD ["/mongodb/bin/run.sh"]
run.sh如下
#!/bin/bash echo "######启动程序##########" mongod -f /mongodb/conf/mongod.conf echo "######查看端口##########" netstat -ntulp echo "######查看进程##########" ps -ef |grep -v grep |grep mongod while true do sleep 1h done
mongod.conf如下
systemLog: destination: file path: /mongodb/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/data directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: # cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true net: bindIpAll: true port: 27017 replication: oplogSizeMB: 2048 replSetName: my_repl
docker-compose.yml如下,ip说明:我单机创建的虚拟网
version: '3.4' x-logging: &default-logging options: max-size: '12m' max-file: '5' driver: json-file services: mongo1: image: mongo:v15 logging: *default-logging restart: always hostname: mongo1 networks: doufy: ipv4_address: 172.19.0.71 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/1/data:/mongodb/data - /data/mongo/1/log:/mongodb/log mongo2: image: mongo:v15 logging: *default-logging restart: always hostname: mongo2 networks: doufy: ipv4_address: 172.19.0.72 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/2/data:/mongodb/data - /data/mongo/2/log:/mongodb/log mongo3: image: mongo:v15 logging: *default-logging restart: always hostname: mongo3 networks: doufy: ipv4_address: 172.19.0.73 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/3/data:/mongodb/data - /data/mongo/3/log:/mongodb/log networks: doufy: external: true
dockefile中的mongodb为官网下载,版本为4.2。下载后解压拷贝至相应目录
链接 https://www.mongodb.com/download-center/enterprise
具体配置参考配置文件。
开启mongo数据库
[root@dou mongo]# docker-compose up -d Creating mongo_mongo3_1 ... done Creating mongo_mongo2_1 ... done Creating mongo_mongo1_1 ... done [root@dou mongo]# docker ps |grep mongo_mongo 031d13d5c978 mongo:v15 "/mongodb/bin/run.sh" 14 seconds ago Up 13 seconds mongo_mongo2_1 199ed80ae7bb mongo:v15 "/mongodb/bin/run.sh" 14 seconds ago Up 13 seconds mongo_mongo3_1 0fd2fc8e73cc mongo:v15 "/mongodb/bin/run.sh" 14 seconds ago Up 13 seconds mongo_mongo1_1 [root@dou mongo]#
登陆数据库,配置mongodb复制集
shell> mongo --port 27017 config = {_id: 'my_repl', members: [ {_id: 0, host: '172.19.0.71:27017'}, {_id: 1, host: '172.19.0.72:27017'}, {_id: 2, host: '172.19.0.73:27017'}] }
初始化这个配置
> rs.initiate(config)
查看状态,数据库已变为 PRIMARY或者SECONDARY(下面标红部分),rs.status可查看具体配置信息
[root@mongo2 /]# mongo MongoDB shell version v4.2.2 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("93bc73e6-19fa-4661-9d6f-9ab55cecd631") } MongoDB server version: 4.2.2 Server has startup warnings: 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-12-16T16:53:23.351+0800 I CONTROL [initandlisten] 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user,which is not recommended. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- my_repl:PRIMARY> rs.status() { "set" : "my_repl", "date" : ISODate("2019-12-16T09:17:56.194Z"), "myState" : 1, "term" : NumberLong(3), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "lastCommittedWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "readConcernMajorityWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "appliedOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "lastAppliedWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "lastDurableWallTime" : ISODate("2019-12-16T09:17:47.805Z") }, "lastStableRecoveryTimestamp" : Timestamp(1576487837, 1), "lastStableCheckpointTimestamp" : Timestamp(1576487837, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2019-12-16T08:58:41.635Z"), "electionTerm" : NumberLong(3), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1576486664, 1), "t" : NumberLong(2) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1576486664, 1), "t" : NumberLong(2) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2019-12-16T08:58:47.761Z"), "wMajorityWriteAvailabilityDate" : ISODate("2019-12-16T08:58:48.557Z") }, "electionParticipantMetrics" : { "votedForCandidate" : true, "electionTerm" : NumberLong(2), "lastVoteDate" : ISODate("2019-12-16T08:53:34.481Z"), "electionCandidateMemberId" : 0, "voteReason" : "", "lastAppliedOpTimeAtElection" : { "ts" : Timestamp(1576486389, 1), "t" : NumberLong(1) }, "maxAppliedOpTimeInSet" : { "ts" : Timestamp(1576486389, 1), "t" : NumberLong(1) }, "priorityAtElection" : 1 }, "members" : [ { "_id" : 0, "name" : "172.19.0.71:27017", "ip" : "172.19.0.71", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1138, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "optimeDurableDate" : ISODate("2019-12-16T09:17:47Z"), "lastHeartbeat" : ISODate("2019-12-16T09:17:55.713Z"), "lastHeartbeatRecv" : ISODate("2019-12-16T09:17:55.705Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.19.0.73:27017", "syncSourceHost" : "172.19.0.73:27017", "syncSourceId" : 2, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 1, "name" : "172.19.0.72:27017", "ip" : "172.19.0.72", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1477, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1576486721, 1), "electionDate" : ISODate("2019-12-16T08:58:41Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "172.19.0.73:27017", "ip" : "172.19.0.73", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1158, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "optimeDurableDate" : ISODate("2019-12-16T09:17:47Z"), "lastHeartbeat" : ISODate("2019-12-16T09:17:55.714Z"), "lastHeartbeatRecv" : ISODate("2019-12-16T09:17:54.613Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.19.0.72:27017", "syncSourceHost" : "172.19.0.72:27017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1576487867, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1576487867, 1) } my_repl:PRIMARY>
常用命令
查看复制集状态: rs.status(); //查看整体复制集状态 rs.isMaster(); // 查看当前是否是主节点 添加删除节点 rs.remove("ip:port"); // 删除一个节点 rs.add("ip:port"); // 新增从节点 rs.addArb("ip:port"); // 新增仲裁节点
检查复制集状态,首先在master节点插入数据
my_repl:PRIMARY> show tables; #查看表,插入数据,并查找数据
my_repl:PRIMARY> db.doufy.insert({title: 'MongoDB 集群测试', description: 'MongoDB 是一个 Nosql 数据库', by: 'doufy', url: 'https://www.cnblogs.com/doufy/p/12050182.html', tags: ['mongodb', 'database', 'NoSQL'], likes: 200})
WriteResult({ "nInserted" : 1 }) #插入成功 my_repl:PRIMARY> show tables;
doufy my_repl:PRIMARY> db.doufy.find().pretty(){ "_id" : ObjectId("5df840efae0e90ae5aa605d6"), "title" : "MongoDB 集群测试", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "doufy", "url" : "https://www.cnblogs.com/doufy/p/12050182.html", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 200 } my_repl:PRIMARY>
从节点检查数据
my_repl:SECONDARY> rs.slaveOk() my_repl:SECONDARY> show tables; doufy my_repl:SECONDARY> db.doufy.find().pretty() { "_id" : ObjectId("5df840efae0e90ae5aa605d6"), "title" : "MongoDB 集群测试", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "doufy", "url" : "https://www.cnblogs.com/doufy/p/12050182.html", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 200 }
#由此可见数据正常同步,此时我们在主库(PRIMARY)执行删除表 db.doufy.drop(),然后从库查询,发现数据被删除了
my_repl:SECONDARY> show tables;
my_repl:SECONDARY> db.doufy.find().pretty()
my_repl:SECONDARY>
#注可自己测试,停止主节点,查看从节点状态,是否变为主节点
主节点关闭,然后去从节点看下。
use admin
db.shutdownServer()
2分片集
分片集群部署图
IP地址 | 路由服务 | 配置服务 | shard1 | shard2 | shard3 |
172.19.0.81 | 27017 | 27018 | 27011 | 27012 | 27013 |
172.19.0.82 | 27017 | 27018 | 27011 | 27012 | 27013 |
172.19.0.83 | 27017 | 27018 | 27011 | 27012 | 27013 |
相应的配置文件如下,过长我折叠了
#dockerfile 如下 from centos:7 RUN yum install net-tools vim -y RUN mkdir -p /mongodb/{bin,conf,data/{share{1,2,3},conf,mongos},log} \ && touch /mongodb/log/{share{1,2,3}.log,mongos.log,conf.log} \ && chmod 755 /mongodb -R ENV MONGOD_HOME /mongodb ENV PATH $MONGOD_HOME/bin:$PATH COPY ./mongodb/bin/ /mongodb/bin/ COPY config /mongodb/conf/ COPY run.sh /mongodb/bin/run.sh RUN chmod a+x /mongodb/bin/run.sh ------------------------分割线--------------------------------- #run.sh如下 #!/bin/bash echo "######启动程序##########" mongod -f /mongodb/conf/mongod1.yml mongod -f /mongodb/conf/mongod2.yml mongod -f /mongodb/conf/mongod3.yml mongod -f /mongodb/conf/conf.yml mongos -f /mongodb/conf/mongos.yml --fork echo "######查看端口##########" netstat -ntulp echo "######查看进程##########" ps -ef |grep -v grep |grep mongod while true do sleep 1h done ------------------------分割线--------------------------------- #docker-compose.yml 挂在目录有点问题 version: '3.4' x-logging: &default-logging options: max-size: '12m' max-file: '5' driver: json-file services: mongo1: image: mongo:v25 logging: *default-logging restart: always hostname: mongo1 networks: doufy: ipv4_address: 172.19.0.81 volumes: - /etc/localtime:/etc/localtime:ro # - /data/mongoshare/1/log:/mongodb/log # - /data/mongoshare/1/data:/mongodb/data mongo2: image: mongo:v25 logging: *default-logging restart: always hostname: mongo2 networks: doufy: ipv4_address: 172.19.0.82 volumes: - /etc/localtime:/etc/localtime:ro #- /data/mongoshare/2/log:/mongodb/log #- /data/mongoshare/2/data:/mongodb/data mongo3: image: mongo:v25 logging: *default-logging restart: always hostname: mongo3 networks: doufy: ipv4_address: 172.19.0.83 volumes: - /etc/localtime:/etc/localtime:ro # - /data/mongoshare/3/log:/mongodb/log # - /data/mongoshare/3/data:/mongodb/data networks: doufy: external: true ------------------------分割线--------------------------------- #conf.yml systemLog: destination: file path: /mongodb/log/conf.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/data/conf/ directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: # cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true sharding: clusterRole: configsvr net: port: 27018 bindIpAll: true replication: oplogSizeMB: 2048 replSetName: configReplSet ------------------------分割线--------------------------------- #mongod1.yml #mongod2.yml 和mongod3.yml修改path和dbPath replSetName字段 systemLog: destination: file path: /mongodb/log/share1.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/data/share1/ directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: # cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true sharding: clusterRole: shardsvr net: port: 27011 bindIpAll: true replication: oplogSizeMB: 2048 replSetName: sh1 ------------------------分割线--------------------------------- #mongos.yml systemLog: destination: file path: /mongodb/log/mongos.log logAppend: true processManagement: fork: true sharding: configDB: configReplSet/172.19.0.81:27018,172.19.0.82:27018,172.19.0.83:27018 net: port: 27017 bindIpAll: true
开启服务
[root@dou ]# docker-compose up -d Creating mongoshare_mongo3_1 ... done Creating mongoshare_mongo1_1 ... done Creating mongoshare_mongo2_1 ... done [root@dou ]#
配置share1复制集
[root@dou ]# docker exec -it mongoshare_mongo1_1 bash -c "echo 'rs.initiate({_id: \"sh1\", members: [{_id: 0, host: \"172.19.0.81:27011\"},{_id: 1, host: \"172.19.0.82:27011\"},{_id: 2, host: \"172.19.0.83:27011\"}]})' | mongo --port 27011" #显示如下 MongoDB shell version v4.2.2 connecting to: mongodb://127.0.0.1:27011/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("3a647817-fc00-4ffd-aede-39899244e541") } MongoDB server version: 4.2.2 { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1576633685, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1576633685, 1) } bye [root@dou ]#
依次配置share2和share3
docker exec -it mongoshare_mongo1_1 bash -c "echo 'rs.initiate({_id: \"sh2\", members: [{_id: 0, host: \"172.19.0.81:27012\"},{_id: 1, host: \"172.19.0.82:27012\"},{_id: 2, host: \"172.19.0.83:27012\"}]})' | mongo --port 27012" docker exec -it mongoshare_mongo1_1 bash -c "echo 'rs.initiate({_id: \"sh3\", members: [{_id: 0, host: \"172.19.0.81:27013\"},{_id: 1, host: \"172.19.0.82:27013\"},{_id: 2, host: \"172.19.0.83:27013\"}]})' | mongo --port 27013"
配置conf配置服务
docker exec -it mongoshare_mongo1_1 bash -c "echo 'rs.initiate({_id: \"configReplSet\",configsvr: true, members: [{_id: 0, host: \"172.19.0.81:27018\"},{_id: 1, host: \"172.19.0.82:27018\"},{_id: 2, host: \"172.19.0.83:27018\"}]})' | mongo --port 27018"
配置路由服务
docker exec -it mongoshare_mongo1_1 bash -c "echo 'db.runCommand({addshard: \"sh1/172.19.0.81:27011,172.19.0.82:27011,172.19.0.83:27011\",name:\"shard1\"} )'|mongo 127.0.0.1:27017/admin " docker exec -it mongoshare_mongo1_1 bash -c "echo 'db.runCommand({addshard: \"sh2/172.19.0.81:27012,172.19.0.82:27012,172.19.0.83:27012\",name:\"shard2\"} )'|mongo 127.0.0.1:27017/admin " docker exec -it mongoshare_mongo1_1 bash -c "echo 'db.runCommand({addshard: \"sh3/172.19.0.81:27013,172.19.0.82:27013,172.19.0.83:27013\",name:\"shard3\"} )'|mongo 127.0.0.1:27017/admin "
查看状态
docker exec -it mongoshare_mongo1_1 bash -c "echo 'db.runCommand({ listshards: 1})'|mongo 127.0.0.1:27017/admin " MongoDB shell version v4.2.2 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("42e88dc3-e52b-4ab5-98de-a2ed3f88ad0e") } MongoDB server version: 4.2.2 { "shards" : [ { "_id" : "shard1", "host" : "sh1/172.19.0.81:27011,172.19.0.82:27011,172.19.0.83:27011", "state" : 1 }, { "_id" : "shard2", "host" : "sh2/172.19.0.81:27012,172.19.0.82:27012,172.19.0.83:27012", "state" : 1 }, { "_id" : "shard3", "host" : "sh3/172.19.0.81:27013,172.19.0.82:27013,172.19.0.83:27013", "state" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1576635328, 1), "$clusterTime" : { "clusterTime" : Timestamp(1576635328, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } bye
至此分片集群部署完毕,开始使用吧