提前创建mongo文件夹
创建Master目录&修改目录权限
mkdir -p /data/mongo_m_01/etc
mkdir -p /data/mongo_m_01/configdb
mkdir -p /data/mongo_m_01/db
mkdir -p /data/mongo_m_01/logs
sudo chmod -R 777 /data/mongo_m_01
创建Slave目录&修改目录权限
mkdir -p /data/mongo_s_01/etc
mkdir -p /data/mongo_s_01/configdb
mkdir -p /data/mongo_s_01/db
mkdir -p /data/mongo_s_01/logs
sudo chmod -R 777 /data/mongo_s_01
创建Arbiter目录&修改目录权限
mkdir -p /data/mongo_a_01/etc
mkdir -p /data/mongo_a_01/configdb
mkdir -p /data/mongo_a_01/db
mkdir -p /data/mongo_a_01/logs
sudo chmod -R 777 /data/mongo_a_01
生成集群通信文件
openssl rand -base64 756 > /data/mongo_m_01/etc/keyfile
400权限是要保证安全性,否则mongod启动会报错
chmod 400 keyfile
创建docker-compose.yml文件
version: "3"
services:
mongo-master:
image: mongo:4.4.3
container_name: mongo_master
restart: always
volumes:
- /data/mongo_m_01/configdb:/data/configdb
- /data/mongo_m_01/db:/data/db
- /data/mongo_m_01/etc/keyfile:/etc/mongo/keyfile
- /data/mongo_m_01/logs:/data/logs
- /etc/localtime:/etc/localtime
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=123456
- TZ=Asia/Shanghai
command: mongod --config /data/configdb/mongod.conf --replSet elc-new-mongo --keyFile /etc/mongo/keyfile
entrypoint:
- bash
- -c
- |
chmod 400 /etc/mongo/keyfile
chown 999:999 /etc/mongo/keyfile
exec docker-entrypoint.sh $$@
mongo-slave:
image: mongo:4.4.3
container_name: mongo_slave
restart: always
volumes:
- /data/mongo_s_01/configdb:/data/configdb
- /data/mongo_s_01/db:/data/db
- /data/mongo_s_01/etc/keyfile:/etc/mongo/keyfile
- /data/mongo_s_01/logs:/data/logs
- /etc/localtime:/etc/localtime
ports:
- 27019:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=123456
- TZ=Asia/Shanghai
command: mongod --config /data/configdb/mongod.conf --replSet elc-new-mongo --keyFile /etc/mongo/keyfile
entrypoint:
- bash
- -c
- |
chmod 400 /etc/mongo/keyfile
chown 999:999 /etc/mongo/keyfile
exec docker-entrypoint.sh $$@
mongo-arbiter:
image: mongo:4.4.3
container_name: mongo_arbiter
restart: always
volumes:
- /data/mongo_a_01/configdb:/data/configdb
- /data/mongo_a_01/db:/data/db
- /data/mongo_a_01/etc/keyfile:/etc/mongo/keyfile
- /data/mongo_a_01/logs:/data/logs
- /etc/localtime:/etc/localtime
ports:
- 27020:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=123456
- TZ=Asia/Shanghai
command: mongod --config /data/configdb/mongod.conf --replSet elc-new-mongo --keyFile /etc/mongo/keyfile
entrypoint:
- bash
- -c
- |
chmod 400 /etc/mongo/keyfile
chown 999:999 /etc/mongo/keyfile
exec docker-entrypoint.sh $$@
mongod.conf配置文件
# mongod.conf
# http://docs.mongodb.org/manual/reference/configuration-options/
# https://www.mongodb.com/docs/v4.0/reference/configuration-options/
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
systemLog:
destination: file
logAppend: true
path: "/data/logs/mongod.log"
net:
port: 27017
bindIp: 0.0.0.0
processManagement:
timeZoneInfo: /usr/share/zoneinfo
replication:
oplogSizeMB: 1024
replSetName: elc-new-mongo
进入容器
docker-compose up -d
docker exec -it mongo bash
mongo
use admin
rs.initiate(
{
_id: "elc-uat-mongo",
members: [
{ _id: 0, host : "172.16.1.59:27018" },
{ _id: 1, host : "172.16.0.126:27021" }
]
}
)
创建用户
db.createUser({
user: "root",
pwd: "123456",
roles: [
{ role: "root", db: "admin" }
]
})
添加仲裁节点
mongo 127.0.0.1:27017 -u root -p 123456 --authenticationDatabase 'admin'
rs.add("172.16.1.59:27020", true);
数据备份
mongodump --host 127.0.0.1 --port 27017 --db crm --username root --password 123456 --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-1 --out /data
数据恢复
mongorestore --host 127.0.0.1 --port 27017 --db crm --username root --password 123456 --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-1 /data/logs/data/crm