使用docker-compose快速搭建Mongo副本集(4.4.3)集群手册

该文详细描述了如何创建MongoDB的主从仲裁节点目录,设置权限,生成集群通信keyfile,通过Docker-compose配置MongoDB服务,初始化复制集,创建用户,以及执行数据备份和恢复操作。
摘要由CSDN通过智能技术生成

提前创建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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值