副本集花式(Replica Set)反差 匿名咸鱼
是一种互为主从的谈判, Replica Set 将数据复制多份保存,不同作事器保存吞并份数据,在出现故障时自动切换,终了故障滚动。
此集群领有一个主节点和多个从节点,这小数与主从复制花式肖似,且主从节点所认的确责任也肖似,然而副本集与主从复制的分手在于:当集群中主节点发生故障时,副本集不错自动投票,选举出新的主节点,并陶冶其余的从节点结合新的主节点,何况这个经过对行使是透明的。
准备:离线环境装配docker参考:
李月 反差 一 、Docker部署MongoDB集群(Replica Set花式-单台)mongodb版块信息: MongoDB shell version v5.0.5 MongoDB server version: 5.0.5 单台的集成了一下shell剧本,多台的是分开的,一步一步的。(1)镜像下载
docker pull mongo:5.0.5 内网打包镜像成tar docker save -o <导出的文献名.tar> <镜像称呼:标签> docker save -o mongo.tar mongo:5.0.5 将tar包放入/opt/docker/下 导入镜像: docker load -i myimage.tar 稽察: docker images(2)缔造集群聚积和集群
#!/bin/bash for conf in 27018_mongo-master 27019_mongo-slave 27020_mongo-arbiter; do echo "conf len= ${#conf}, port=${conf:0:5}, name=${conf:6:${#conf}}"; if [ ${conf:0:5} == 27018 ] then docker network create mongo-network echo "create mongo-network" fi docker run -d --restart=always -p ${conf:0:5}:27017 --name ${conf:6:${#conf}} -v /opt/mongo/${conf:6:${#conf}}/configdb/:/data/configdb -v /opt/mongo/${conf:6:${#conf}}/db/:/data/db -v /opt/mongo/${conf:6:${#conf}}/backup:/data/backup --net=mongo-network mongo:5.0.5 mongod --dbpath /data/db --replSet mongoreplset done(3)配置集群
确立MongoDB的复制集(Replica Set)的配置信息
1. 参预master容器中的mongo:docker exec -it mongo-master mongo 2. 配置集群:IP为部署数据库的作事器IP,举例IP是10.9.21.12 (防火墙端口要放开) config = {_id:"mongoreplset", version:1, members:[{_id:0, host:"10.9.21.12:27018", priority:5}, {_id:1, host:"10.9.21.12:27019", priority:2}, {_id:2, host:"10.9.21.12:27020", priority:3}]} rs.initiate(config) 要是指示乌有,且rs.config()后稽察仍是有配置,但未终了主从,则可尝试履行底下的代码,再行配置(force -> 非主节点强制申请再行分派主节点。) [errmsg” : “New config is rejected :: caused by :: replSetReconfig should only be run on a writable PRIMARY. Current state REMOVED;”] rs.reconfig({_id:"mongoreplset", version:1, members:[{_id:0, host:"10.9.21.12:27018", priority:5}, {_id:1, host:"10.9.21.12:27019", priority:2}, {_id:2, host:"10.9.21.12:27020", priority:3}]},{force:true}) 3. 退出: exit 4. 考证集群配置: docker exec -it mongo-master mongo 会有如下指示:mongoreplset:PRIMARY> docker exec -it mongo-slave mongo 会有如下指示:mongoreplset:SECONDARY> docker exec -it mongo-arbiter mongo 会有如下指示:mongoreplset:SECONDARY>(4)创建数据库用户
1. 参预master容器中的mongo:docker exec -it mongo-master mongo 2. 创建用户: use admin db.createUser( { user: "root", pwd: "db_root_password", roles: ["root"] } ) db.auth("root", "Synjones2021") //测试一下 db.createUser( { user: "user01", pwd: "db_user01_password", roles: ["readWriteAnyDatabase"] } ) exit(5)mongodb集群的结合地址
如下: mongodb://user01:db_user01_password@10.9.21.12:27018,10.9.21.12:27019,10.9.21.12:27020(6)删除集群
docker ps -a docker stop id1/id2/id3 三个王人罢手 docker rm -f id1/id2/id3 docker network ls docker network rm network_id rm -rf /opt/mongo/..(缔造集群时配置的目次)(7)备份原数据库文献
1.参预原mongo容器: docker exec -it mongodb bash 2.备份原mongo库:mongodump --host 10.9.21.12 --port 27017 -u username -p password -o /data/backup --authenticationDatabase admin 3.停掉原mongo容器(要是部署在吞并台端口突破可先停掉): docker stop mongodb 4.拷贝备份文献(沉着器内拷贝到本机): 【 docker cp <container_id>:/data/db/. /path/to/local/directory 】 /data/db/.示意拷贝/data/db目次过甚统共骨子,包括子目次和文献 docker cp 70161373fcef:/data/backup/. /opt/mongo/mongo-master/backup(此旅途为创建容器时确立的容器、本机分享账户) 5. 参预容器内可稽察到仍是将文献分享进来了 ls -lh 6. 参预master容器中:docker exec -it mongo-master bash 7. 加载备份数据: mongorestore --host 10.9.21.12 --port 27018 /data/backup/ 8. 退出:exit二、Docker部署MongoDB集群(Replica Set花式-多台)
假定一共有两台机器:10.9.21.13(主)、10.9.21.11(从)
(0)镜像打包 导入docker pull mongo:5.0.5 内网打包镜像成tar docker save -o <导出的文献名.tar> <镜像称呼:标签> docker save -o mongo.tar mongo:5.0.5 将tar包放入/opt/docker/下, 导入镜像: docker load -i myimage.tar 稽察: docker images(1)缔造数据库集群聚积
假定一共有两台机器:10.9.21.13(主)、10.9.21.11(从),两台机器均要创建 mongo-network docker network ls docker network create mongo-network —————————————————————————— docker network rm network_id //删除(2)缔造集群
假定一共有两台机器:10.9.21.13(主)、10.9.21.11(从)
早先容器反差 匿名咸鱼,并配好参数(整理为shell剧本 履行剧本)
10.9.21.13(主)#!/bin/bash docker run -d --restart=always -p 27017:27017 --name mongo-master -v /opt/mongo/mongo_master/configdb/:/data/configdb -v /opt/mongo/mongo_master/db/:/data/db -v /opt/mongo/mongo_master/backup:/data/backup --net=mongo-network mongo:5.0.5 mongod --dbpath /data/db --replSet mongoreplset10.9.21.11(从)
拷贝备份文献参预-v /opt/mongo/mongo-slave/backup(要是需要) 可参考: 参缱绻份文献夹backup:zip -r ttt.zip * 跨机器拷贝:scp /opt/x/backup/tt.zip @10.9.21.13:/opt/mongo/mongo_master/backup 解压:unzip tt.zip #!/bin/bash docker run -d --restart=always -p 27018:27017 --name mongo-slave -v /opt/mongo/mongo-slave/configdb/:/data/configdb -v /opt/mongo/mongo-slave/db/:/data/db -v /opt/mongo/mongo-slave/backup:/data/backup --net=mongo-network mongo:5.0.5 mongod --dbpath /data/db --replSet mongoreplset #!/bin/bash docker run -d --restart=always -p 27019:27017 --name mongo-arbiter -v /opt/mongo/mongo-arbiter/configdb/:/data/configdb -v /opt/mongo/mongo-arbiter/db/:/data/db -v /opt/mongo/mongo-arbiter/backup:/data/backup --net=mongo-network mongo:5.0.5 mongod --dbpath /data/db --replSet mongoreplset(3)配置集群
1. 参预master容器:docker exec -it mongo-master bash 2. 参预mongo:mongo 3. 配置集群:IP为部署数据库的作事器IP config = {_id:"mongoreplset", version:1, members:[{_id:0, host:"10.9.21.13:27017", priority:5}, {_id:1, host:"10.9.21.11:27018", priority:2}, {_id:2, host:"10.9.21.11:27019", priority:3}]} rs.initiate(config) 4. 退出mongo:exit 5. 加载旧库的备份数据(要是需要,将3.1备份的数据拷贝到主机相应的挂载目次): mongorestore --host <mongo主Ip> --port 27017 /data/backup/ 6. 退出容器: exit 7. 考证集群: 10.9.21.13(主) docker exec -it mongo-master mongo 会有如下指示:mongoreplset:PRIMARY> 10.9.21.11(从) docker exec -it mongo-slave mongo 会有如下指示:mongoreplset:SECONDARY> 10.9.21.11(从) docker exec -it mongo-arbiter mongo 会有如下指示:mongoreplset:SECONDARY>(4)导入备份数据
1. 参预master容器中:docker exec -it mongo-master bash 2. 参预容器内能看到分享文献夹内的备份文献 cd /data/backup ls -lh 3. 加载备份数据: mongorestore --host 10.9.21.13 --port 27018 /data/backup/(5)聚首稽察
use admin db.auth("username", "password") //测试一下 聚首: mongodb://user01:db_user01_password@10.9.21.12:27018,10.9.21.12:27019,10.9.21.12:27020本站仅提供存储作事,统共骨子均由用户发布,如发现存害或侵权骨子,请点击举报。