docker-compose mongodb 副本

生成keyfile

//生成keyfile
openssl rand -base64 756 > mongo-keyfile.jks
chmod 400 keyfile/keyfile.jks

//如果报error opening file: keyfile/keyfile.jks: bad file
chown 999 keyfile/keyfile.jks

启动docker

docker-compose up -d

version: "3.7"
services:
  mgomaster:
    image: mongo:4.4.9
    container_name: mgomaster
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    command: mongod --auth --keyFile /keyfile/keyfile.jks --bind_ip_all --replSet rs0
    volumes:
      - ./master:/data/db
      - ./keyfile:/keyfile
    ports:
      - "27017:27017"
    networks:
      - default

  mgoslaver:
    image: mongo:4.4.9
    container_name: mgoslaver
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    command: mongod --auth --keyFile /keyfile/keyfile.jks --bind_ip_all --replSet rs0
    volumes:
      - ./slaver:/data/db
      - ./keyfile:/keyfile
    ports:
      - "27018:27017"
    networks:
      - default

networks:
  default:
    external:
      name: xxx

初始化副本

docker exec -it mgomaster /bin/bash
进入 docker 以后, mongo -u <用户名> -p <密码>
rs 初始化

rs.initiate(
  {
    _id : 'rs0',
    members: [
      { _id : 0, host : "mgomaster:27017" },
      { _id : 1, host : "mgoslaver:27017" },
    ]
  }
)

验证状态

通过 rs.status() 查看状态即可。

上一篇:Keystore介绍


下一篇:nomn的使用