https://www.cnblogs.com/hahaha111122222/p/13937264.html

https://www.runoob.com/mongodb/mongodb-replication.html

https://www.runoob.com/mongodb/mongodb-replication.html

  1. 创建网络与容器
mkdir -p /opt/db/mongo/data/mongo1
mkdir -p /opt/db/mongo/data/mongo2
mkdir -p /opt/db/mongo/data/mongo3

mkdir -p /opt/db/mongo/logs/mongo1
mkdir -p /opt/db/mongo/logs/mongo2
mkdir -p /opt/db/mongo/logs/mongo3

docker pull mongo
docker network create mongo-rs
docker run --name mongo1 -p 27027:27027 --net mongo-rs -d -v /opt/db/mongo/data/mongo1:/data/db -v /opt/db/mongo/logs/mongo1:/var/log/mongodb mongo mongod --replSet "rs0" --port 27027
docker run --name mongo2 -p 27028:27028 --net mongo-rs -d -v /opt/db/mongo/data/mongo2:/data/db -v /opt/db/mongo/logs/mongo2:/var/log/mongodb  mongo mongod --replSet "rs0" --port 27028
docker run --name mongo3 -p 27029:27029 --net mongo-rs -d -v /opt/db/mongo/data/mongo3:/data/db -v /opt/db/mongo/logs/mongo3:/var/log/mongodb  mongo mongod --replSet "rs0" --port 27029
docker run --name mongo1 -p 27027:27027 --net mongo-rs -d -v /opt/db/mongo/data/mongo1:/data/db -v /opt/db/mongo/logs/mongo1:/var/log/mongodb mongo mongod --replSet "rs0" --port 27027
docker run --name mongo2 -p 27028:27028 --net mongo-rs -d -v /opt/db/mongo/data/mongo2:/data/db -v /opt/db/mongo/logs/mongo2:/var/log/mongodb  mongo mongod --replSet "rs0" --port 27028
docker run --name mongo3 -p 27029:27029 --net mongo-rs -d -v /opt/db/mongo/data/mongo3:/data/db -v /opt/db/mongo/logs/mongo3:/var/log/mongodb  mongo mongod --replSet "rs0" --port 27029
  1. 分别进入每个容器执行
docker exec -it mongo1 /bin/bash

docker exec -it mongo2 /bin/bash

docker exec -it mongo3 /bin/bash

echo "127.0.0.1 mongo1 mongo2 mongo3" >> /etc/hosts

在 linux 下执行这句在重启后 hosts 文件又还原了, 有一种简单的解决方式,
如下:

echo 'echo "127.0.0.1 mongo1 mongo2 mongo3" >> /etc/hosts' >> /root/.bashrc
  1. 重启每个容器
docker restart mongo1 mongo2 mongo3

docker stop mongo1 mongo2 mongo3

docker start mongo1 mongo2 mongo3
  1. 添加本地环境 hosts:
Linux 用户在本地 
vim /etc/hosts
文件中添加 

127.0.0.1 mongo1 mongo2 mongo3

Windows 用户在 hosts 加一行:

127.0.0.1 mongo1 mongo2 mongo3
  1. 进入一个容器初始化副本集:
docker exec -it mongo1 mongo --port 27027
rs.initiate({"_id": "rs0", "members": [{"_id":0, "host": "mongo1:27027"}, {"_id": 1, "host": "mongo2:27028"}, {"_id":2, "host": "mongo3:27029"}]})

# 如果mongo无效果,使用mongod
docker exec -it mongo1 mongod --port 27027
rs.initiate({"_id": "rs0", "members": [{"_id":0, "host": "mongo1:27027"}, {"_id": 1, "host": "mongo2:27028"}, {"_id":2, "host": "mongo3:27029"}]})


# 如果上面执行无效果,通过下面语句执行(稳健)
mongo --host 127.0.0.1 --port 27027

rs.initiate({"_id": "rs0", "members": [{"_id":0, "host": "mongo1:27027"}, {"_id": 1, "host": "mongo2:27028"}, {"_id":2, "host": "mongo3:27029"}]})

  1. Application 连接
mongodb://127.0.0.1:27027,127.0.0.1:27028,127.0.0.1:27029/databaseName?slaveOk=true&replicaSet=rs0&write=1&connectTimeoutMS=30000&readPreference=primary

mongo --host 127.0.0.1 --port 27027
rs.status()

mongo --host 127.0.0.1 --port 27028
rs.status()


mongo --host 127.0.0.1 --port 27029
rs.status()

文档更新时间: 2023-09-24 21:28   作者:admin