Docker搭建MongoDB集群
副本集模式(Replica Set)
-
是一種互為主從的關(guān)系, Replica Set 將數(shù)據(jù)復(fù)制多份保存,不同服務(wù)器保存同一份數(shù)據(jù),在出現(xiàn)故障時自動切換,實(shí)現(xiàn)故障轉(zhuǎn)移。 -
此集群擁有一個主節(jié)點(diǎn)和多個從節(jié)點(diǎn),這一點(diǎn)與主從復(fù)制模式類似,且主從節(jié)點(diǎn)所負(fù)責(zé)的工作也類似,但是副本集與主從復(fù)制的區(qū)別在于:當(dāng)集群中主節(jié)點(diǎn)發(fā)生故障時,副本集可以自動投票,選舉出新的主節(jié)點(diǎn),并引導(dǎo)其余的從節(jié)點(diǎn)連接新的主節(jié)點(diǎn),而且這個過程對應(yīng)用是透明的。
準(zhǔn)備:離線環(huán)境安裝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
內(nèi)網(wǎng)打包鏡像成tar
docker save -o <導(dǎo)出的文件名.tar> <鏡像名稱:標(biāo)簽>
docker save -o mongo.tar mongo:5.0.5
將tar包放入/opt/docker/下
導(dǎo)入鏡像:
docker load -i myimage.tar
查看:
docker images
(2)建立集群網(wǎng)絡(luò)和集群
#!/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)配置集群
設(shè)置MongoDB的復(fù)制集(Replica Set)的配置信息
1. 進(jìn)入master容器中的mongo:docker exec -it mongo-master mongo
2. 配置集群:IP為部署數(shù)據(jù)庫的服務(wù)器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()后查看已經(jīng)有配置,但未實(shí)現(xiàn)主從,則可嘗試執(zhí)行下面的代碼,重新配置(force -> 非主節(jié)點(diǎn)強(qiáng)制請求重新分配主節(jié)點(diǎn)。)
[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. 驗(yàn)證集群配置:
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)創(chuàng)建數(shù)據(jù)庫用戶
1. 進(jìn)入master容器中的mongo:docker exec -it mongo-master mongo
2. 創(chuàng)建用戶:
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)備份原數(shù)據(jù)庫文件
1.進(jìn)入原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.拷貝備份文件(從容器內(nèi)拷貝到本機(jī)):
【 docker cp <container_id>:/data/db/. /path/to/local/directory 】
/data/db/.表示拷貝/data/db目錄及其所有內(nèi)容,包括子目錄和文件
docker cp 70161373fcef:/data/backup/. /opt/mongo/mongo-master/backup(此路徑為創(chuàng)建容器時設(shè)置的容器、本機(jī)共享賬戶)
5. 進(jìn)入容器內(nèi)可查看到已經(jīng)將文件共享進(jìn)來了
ls -lh
6. 進(jìn)入master容器中:docker exec -it mongo-master bash
7. 加載備份數(shù)據(jù):
mongorestore --host 10.9.21.12 --port 27018 /data/backup/
8. 退出:exit
二、Docker部署MongoDB集群(Replica Set模式-多臺)
假設(shè)一共有兩臺機(jī)器:10.9.21.13(主)、10.9.21.11(從)
(0)鏡像打包 導(dǎo)入
docker pull mongo:5.0.5
內(nèi)網(wǎng)打包鏡像成tar
docker save -o <導(dǎo)出的文件名.tar> <鏡像名稱:標(biāo)簽>
docker save -o mongo.tar mongo:5.0.5
將tar包放入/opt/docker/下,
導(dǎo)入鏡像:
docker load -i myimage.tar
查看:
docker images
(1)建立數(shù)據(jù)庫集群網(wǎng)絡(luò)
假設(shè)一共有兩臺機(jī)器:10.9.21.13(主)、10.9.21.11(從),兩臺機(jī)器均要創(chuàng)建 mongo-network
docker network ls
docker network create mongo-network
——————————————————————————
docker network rm network_id //刪除
(2)建立集群
假設(shè)一共有兩臺機(jī)器:10.9.21.13(主)、10.9.21.11(從)
啟動容器,并配好參數(shù)(整理為shell腳本 執(zhí)行腳本)
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 mongoreplset
10.9.21.11(從)
拷貝備份文件進(jìn)入-v /opt/mongo/mongo-slave/backup(如果需要)
可參考:
進(jìn)入備份文件夾backup:zip -r ttt.zip *
跨機(jī)器拷貝: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. 進(jìn)入master容器:docker exec -it mongo-master bash
2. 進(jìn)入mongo:mongo
3. 配置集群:IP為部署數(shù)據(jù)庫的服務(wù)器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. 加載舊庫的備份數(shù)據(jù)(如果需要,將3.1備份的數(shù)據(jù)拷貝到主機(jī)相應(yīng)的掛載目錄):
mongorestore --host <mongo主Ip> --port 27017 /data/backup/
6. 退出容器: exit
7. 驗(yàn)證集群:
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)導(dǎo)入備份數(shù)據(jù)
1. 進(jìn)入master容器中:docker exec -it mongo-master bash
2. 進(jìn)入容器內(nèi)能看到共享文件夾內(nèi)的備份文件
cd /data/backup
ls -lh
3. 加載備份數(shù)據(jù):
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
|