Docker建立MongoDB集群

生成mongo.key文件:

openssl rand -base64 756 > mongo.key

KeyFile需要每个副本集服务启动时加载,在操作库时需要用户名和密码,而且KeyFile文件的内容必须一致。

KeyFile文件必须满足以下条件:
(1).至少6个字符,小于1024字节。
(2).认证的时候不考虑文件中的空白字符。
(3). 连接副本集成员的KeyFile和启动mongos进程的 KeyFile文件内容必须一致。
(4).必须是base64编码,但不能有等号。
(5).文件权限必须是 600(chmod 600),不能分配任何权限给group成员和其他成员。

Docer Compose文件:

version: '3'
networks:
  mongo:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/16
services:
  # ----------------------------------------------------------------
  mongodb1:
    image: registry.cn-guangzhou.aliyuncs.com/maxwoods/mongo:7.0.12
    container_name: mongodb1
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=joycode
      - MONGO_PORT=27117
    networks:
      mongo:
        ipv4_address: 172.25.0.11
    ports:
      - 27117:27017
    volumes:
      - D:/docker/container/mongodb1/db/:/data/db/
      - D:/docker/container/mongodb1/conf/:/data/configdb/
      - D:/docker/container/mongodb1/conf/mongo.key:/data/configdb/mongo.key:ro
    command: --replSet "mongo" --keyFile "/data/configdb/mongo.key"
  # ----------------------------------------------------------------
  mongodb2:
    image: registry.cn-guangzhou.aliyuncs.com/maxwoods/mongo:7.0.12
    container_name: mongodb2
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=joycode
      - MONGO_PORT=27217
    networks:
      mongo:
        ipv4_address: 172.25.0.12
    ports:
      - 27217:27017
    volumes:
      - D:/docker/container/mongodb2/db/:/data/db/
      - D:/docker/container/mongodb2/conf/:/data/configdb/
      - D:/docker/container/mongodb2/conf/mongo.key:/data/configdb/mongo.key:ro
    command: --replSet "mongo" --keyFile "/data/configdb/mongo.key"
  # ----------------------------------------------------------------
  mongodb3:
    image: registry.cn-guangzhou.aliyuncs.com/maxwoods/mongo:7.0.12
    container_name: mongodb3
    restart: always
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=joycode
      - MONGO_PORT=27317
    networks:
      mongo:
        ipv4_address: 172.25.0.13
    ports:
      - 27317:27017
    volumes:
      - D:/docker/container/mongodb3/db/:/data/db/
      - D:/docker/container/mongodb3/conf/:/data/configdb/
      - D:/docker/container/mongodb3/conf/mongo.key:/data/configdb/mongo.key:ro
    command: --replSet "mongo" --keyFile "/data/configdb/mongo.key"

集群配置:

rs.initiate({
  _id: "mongo",
  members: [
    { _id: 0, host: "192.168.56.1:27117" },
    { _id: 1, host: "192.168.56.1:27217" },
    { _id: 2, host: "192.168.56.1:27317" }
  ]
});

参考:

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享