以下是一个详细的Docker Compose配置示例,用于设置一个包含三个节点的MongoDB副本集,并确保安全性(使用账号密码进行认证)。所有节点都将设置在同一个Docker网络( py-mongo )下,以便于未来的扩展。
0.先下载mongo镜像。
-
创建密钥文件首先,您需要生成一个密钥文件,用于副本集成员之间的内部认证。可以使用以下命令生成密钥文件:
kt@kt-SYS-4028GR-TR2:~$ openssl rand -base64 756 > ~/mongodb-data/mongodb-keyfile.key
kt@kt-SYS-4028GR-TR2:~$ chmod 400 ~/mongodb-data/mongodb-keyfile.key
kt@kt-SYS-4028GR-TR2:~$
2.编写Docker Compose文件,注意这行的命令。找了好长时间。然后代码中,我无法复制出来。所以放图。
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ nano docker-compose.yml
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ cat docker-compose.yml
services:
mongo1:
image: mongo:latest
container_name: mongo1
restart: always
volumes:
- ~/mongodb-data/data1:/data/db
- ~/mongodb-data/mongodb-keyfile.key:/etc/mongodb-keyfile.key
ports:
- "27017:27017"
command: /bin/sh -c 'mongod --replSet rs0' --keyFile /etc/mongodb-keyfile.key --auth
networks:
- py-mongo
mongo2:
image: mongo:latest
container_name: mongo2
restart: always
volumes:
- ~/mongodb-data/data2:/data/db
- ~/mongodb-data/mongodb-keyfile.key:/etc/mongodb-keyfile.key
ports:
- "27018:27017"
command: /bin/sh -c 'mongod --replSet rs0' --keyFile /etc/mongodb-keyfile.key --auth
networks:
- py-mongo
mongo3:
image: mongo:latest
container_name: mongo3
restart: always
volumes:
- ~/mongodb-data/data3:/data/db
- ~/mongodb-data/mongodb-keyfile.key:/etc/mongodb-keyfile.key
ports:
- "27019:27017"
command: /bin/sh -c 'mongod --replSet rs0' --keyFile /etc/mongodb-keyfile.key --auth
networks:
- py-mongo
networks:
py-mongo:
driver: bridge
kt@kt-SYS-4028GR-TR2:~/mongodb-data$
-
启动服务在包含 docker-compose.yml 文件的目录中,运行以下命令来启动MongoDB副本集:
docker-compose up -d
状态:
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d17b8804557a mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo2
c93a51036b89 mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo3
8baf6759ce4f mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo1
4.如系统异常,请修改权限
sudo chown -R 1000:1000 ~/mongodb-data/data1
sudo chown -R 1000:1000 ~/mongodb-data/data2
sudo chown -R 1000:1000 ~/mongodb-data/data3
ID,使用命令:id获取:
bashid
这将输出当前用户的用户ID(UID)、组ID(GID)以及所属的其他组信息。例如:plaintextuid=1000(your_username) gid=1000(your_username) groups=1000(your_username),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
查看特定用户的ID如果您想查看某个特定用户的ID,可以使用 id 命令并指定用户名:
5.现在的状态,
docker-compose down
docker-compose up -d
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d17b8804557a mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo2
c93a51036b89 mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo3
8baf6759ce4f mongo:latest "docker-entrypoint.s..." 9 minutes ago Restarting (1) 47 seconds ago mongo1
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ docker-compose down
Stopping mongo2 ... done
Stopping mongo3 ... done
Stopping mongo1 ... done
Removing mongo2 ... done
Removing mongo3 ... done
Removing mongo1 ... done
Removing network mongodb-data_py-mongo
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ docker-compose up -d
Creating network "mongodb-data_py-mongo" with driver "bridge"
Creating mongo1 ... done
Creating mongo2 ... done
Creating mongo3 ... done
kt@kt-SYS-4028GR-TR2:~/mongodb-data$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24b43c939be0 mongo:latest "docker-entrypoint.s..." 4 seconds ago Up Less than a second 0.0.0.0:27019->27017/tcp, [::]:27019->27017/tcp mongo3
c0e15a4a763d mongo:latest "docker-entrypoint.s..." 4 seconds ago Up Less than a second 0.0.0.0:27018->27017/tcp, [::]:27018->27017/tcp mongo2
9ae68c68c59f mongo:latest "docker-entrypoint.s..." 4 seconds ago Up Less than a second 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongo1
kt@kt-SYS-4028GR-TR2:~/mongodb-data$
-
初始化副本集启动容器后,连接到其中一个MongoDB实例(例如 mongo1 ),并执行以下命令初始化副本集:
docker exec -it mongo1 mongosh
7.使用管理员进入。
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" },
{ _id: 2, host: "mongo3:27017" }
]
});
执行过程• 连接到MongoDB实例:使用 docker exec 命令连接到容器中的MongoDB实例。• 切换到 admin 数据库:在MongoDB shell中输入 use admin ,然后按回车键。• 执行初始化命令:输入 rs.initiate(...) 命令,并按回车键执行。MongoDB会处理副本集的初始化过程,并返回初始化结果。检查状态
检查状态初始化完成后,您可以检查副本集的状态,以确保所有节点都已正确加入副本集:
rs.status()
未完等