Docker容器

docker-compose mongodb 副本

本文主要是介绍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() 查看状态即可。

这篇关于docker-compose mongodb 副本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!