选定存放 mongodb.key的目录(任意有权限的目录)
#执行 openssl rand -base64 756 -out mongodb.key #或 openssl rand -base64 700 > mongodb.key
在与mongodb.key同级目录编写docker-compose.yml
vim docker-compose.yml
输入以下内容,保存
version: '3.1'
services:
mongodb1:
image: mongo:5.0
restart: always
container_name: mongo-master
privileged: true
volumes:
- ./data/db/mongo1/db:/data/db
- ./data/db/mongo1/configdb:/data/configdb
- ./mongodb.key:/data/mongodb.key
ports:
- 27018:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: qsxdr123!
command: mongod --replSet mongos --keyFile /data/mongodb.key
entrypoint:
- bash
- -c
- |
chmod 400 /data/mongodb.key
chown 999:999 /data/mongodb.key
exec docker-entrypoint.sh $$@
mongodb2:
image: mongo:5.0
restart: always
container_name: mongo-slave
privileged: true
volumes:
- ./data/db/mongo2/db:/data/db
- ./data/db/mongo2/configdb:/data/configdb
- ./mongodb.key:/data/mongodb.key
ports:
- 27019:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: qsxdr123!
command: mongod --replSet mongos --keyFile /data/mongodb.key
entrypoint:
- bash
- -c
- |
chmod 400 /data/mongodb.key
chown 999:999 /data/mongodb.key
exec docker-entrypoint.sh $$@
mongodb3:
image: mongo:5.0
restart: always
container_name: mongo-arbiter
privileged: true
volumes:
- ./data/db/mongo3/db:/data/db
- ./data/db/mongo3/configdb:/data/configdb
- ./mongodb.key:/data/mongodb.key
ports:
- 27020:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: qsxdr123!
command: mongod --replSet mongos --keyFile /data/mongodb.key
entrypoint:
- bash
- -c
- |
chmod 400 /data/mongodb.key
chown 999:999 /data/mongodb.key
exec docker-entrypoint.sh $$@
安装docker-compose工具
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
执行compose模板
#当前目录执行
docker-compose up -d
查看mongo容器启动情况
docker ps
进入mongo容器
docker exec -it mongo-master /bin/bash
初始化mongo集群
#以认证的方式打开mongo
mongo -u root -p qsxdr123!
#初始化集群
rs.initiate({_id:"mongos",members:[{_id:0,host:"192.168.1.86:27018"},{_id:1,host:"192.168.1.86:27019"},{_id:2,host:"192.168.1.86:27020",arbiterOnly:true}]})
#开启备机可读
rs.secondaryOk()
#查看集群状态
rs.status()
增加mongo-master权重
#依次执行以下命令
config = rs.conf() config.members[0].priority=2
#配置生效
rs.reconfig(config)
#新配置查看
rs.conf()
退出mongo链接、退出mongo容器
工具链接测试
Studio 3T、Robo 3T、Navicat 16 for MongoDB
e.g. Navicat 16 for MongoDB #配置链接
#链接成功
e.g. Studio 3T #配置链接1
#配置链接2
#链接成功
仲裁节点功能验证
1、当前情况
2、模拟主节点宕机
#关闭mongo-master容器
#通过 Studio 3T工具 查看主从节点切换情况
3、主节点宕机恢复演示
#启动mongo-master容器
##通过 Studio 3T工具 查看主从节点切换情况
|