文章目录
- [一. Mac](#一. Mac)
-
-
- [1.1 创建目录](#1.1 创建目录)
- [1.2 docker-compose.yaml](#1.2 docker-compose.yaml)
- [1.3 部署](#1.3 部署)
- [1.4 卸载](#1.4 卸载)
- [1.5 replSet配置](#1.5 replSet配置)
-
-
- [1.5.1 初始化replSet](#1.5.1 初始化replSet)
- [1.5.2 创建超管用户](#1.5.2 创建超管用户)
- [1.5.3 验证用户](#1.5.3 验证用户)
- [1.5.4 查看replSet状态](#1.5.4 查看replSet状态)
-
-
- [二. Centos7](#二. Centos7)
-
-
- [2.1 创建目录](#2.1 创建目录)
- [2.2 docker-compose.yaml](#2.2 docker-compose.yaml)
- [2.3 部署](#2.3 部署)
- [2.4 卸载](#2.4 卸载)
- [2.5 replSet配置](#2.5 replSet配置)
-
-
- [2.5.1 初始化replSet](#2.5.1 初始化replSet)
- [2.5.2 创建超管用户](#2.5.2 创建超管用户)
- [2.5.3 验证用户](#2.5.3 验证用户)
- [2.5.4 查看replSet状态](#2.5.4 查看replSet状态)
-
-
参考 https://blog.csdn.net/a179583637/article/details/140816706
一. Mac
1.1 创建目录
mkdir -p /Users/wanfei/docker-compose/mongodb && cd /Users/wanfei/docker-compose/mongodb
1.2 docker-compose.yaml
默认不开启relSet
cat <<EOF> /Users/wanfei/docker-compose/mongodb/docker-compose.yaml
version: '3'
services:
mongodb:
restart: always
image: registry.cn-shanghai.aliyuncs.com/wanfei/mongo:7.0.12
container_name: mongodb
privileged: true
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
TZ: Asia/Shanghai
volumes:
- /Users/wanfei/docker-compose/mongodb/data:/data/db
ports:
- 27017:27017
command: mongod --auth
EOF
开启relSet(数据同步)
cat <<EOF> /Users/wanfei/docker-compose/mongodb/docker-compose.yaml
version: '3'
services:
mongodb:
restart: always
image: registry.cn-shanghai.aliyuncs.com/wanfei/mongo-relset:7.0.12
container_name: mongodb
volumes:
- /Users/wanfei/docker-compose/mongodb/data:/data/db
ports:
- 27017:27017
command: >
bash -c "
openssl rand -base64 756 > /etc/mongodb-keyfile &
chmod 400 /etc/mongodb-keyfile &
mongod --replSet rs0 --auth --keyFile /etc/mongodb-keyfile --bind_ip_all"
EOF
开启
replSet
不能配置用户信息,否则初始化replSet
报错
1.3 部署
docker-compose -f /Users/wanfei/docker-compose/mongodb/docker-compose.yaml up -d
1.4 卸载
docker-compose -f /Users/wanfei/docker-compose/mongodb/docker-compose.yaml down
1.5 replSet配置
1.5.1 初始化replSet
不能一开始创建账号密码,否则这一步报错
docker exec -it mongodb mongosh admin
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "127.0.0.1:27017" }
]
})
1.5.2 创建超管用户
db.createUser({
user: "root",
pwd: "root",
roles: [ { role: "root", db: "admin" } ]
})
1.5.3 验证用户
db.auth('root','root')
1.5.4 查看replSet状态
rs.status()
二. Centos7
2.1 创建目录
mkdir -p /root/i/docker-compose/mongodb && cd /root/i/docker-compose/mongodb
2.2 docker-compose.yaml
默认不开启relSet
cat <<EOF> /root/i/docker-compose/mongodb/docker-compose.yaml
version: '3'
services:
mongodb:
restart: always
image: registry.cn-shanghai.aliyuncs.com/wanfei/mongo:7.0.12
container_name: mongodb
privileged: true
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
TZ: Asia/Shanghai
volumes:
- /root/i/docker-compose/mongodb/data:/data/db
ports:
- 27017:27017
command: mongod --auth
EOF
开启relSet(数据同步)
cat <<EOF> /root/i/docker-compose/mongodb/docker-compose.yaml
version: '3'
services:
mongodb:
restart: always
image: registry.cn-shanghai.aliyuncs.com/wanfei/mongo-relset:7.0.12
container_name: mongodb
volumes:
- /root/i/docker-compose/mongodb/data:/data/db
ports:
- 27017:27017
command: >
bash -c "
openssl rand -base64 756 > /etc/mongodb-keyfile &
chmod 400 /etc/mongodb-keyfile &
mongod --replSet rs0 --auth --keyFile /etc/mongodb-keyfile --bind_ip_all"
EOF
开启
replSet
不能配置用户信息,否则初始化replSet
报错
2.3 部署
docker-compose -f /root/i/docker-compose/mongodb/docker-compose.yaml up -d
2.4 卸载
docker-compose -f /root/i/docker-compose/mongodb/docker-compose.yaml down
2.5 replSet配置
2.5.1 初始化replSet
不能一开始创建账号密码,否则这一步报错
docker exec -it mongodb mongosh admin
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "127.0.0.1:27017" }
]
})
2.5.2 创建超管用户
db.createUser({
user: "root",
pwd: "root",
roles: [ { role: "root", db: "admin" } ]
})
2.5.3 验证用户
db.auth('root','root')
2.5.4 查看replSet状态
rs.status()