MongoDB. BadValue: security.keyFile is required when authorization is enabled with replica sets

in Docker with 0 comment

MongoDB 复制集是一个包含多个 MongoDB 实例的集群,其中有一个主节点(Primary)和多个备份节点(Secondary)。主节点负责处理所有的写请求,并将写入的数据复制到备份节点上。备份节点通过从主节点同步数据来保持数据的一致性。如果主节点发生故障,备份节点中会选出一个节点成为新的主节点,保持系统的高可用性。
下面是一个 MongoDB 复制集的官网示意图:

图片描述...

mongo:6 版本起,如果您禁用身份验证且未指定密钥文件,MongoDB 则不需要密钥文件

在 Docker 中,移除掉

environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password123

Run

docker-compose down
docker-compose up -d

完成

version: "3.8"

services:
  mongo:
    image: mongo:7.0
    
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27017"]
    ports:
      - 27017:27017
    extra_hosts:
      - "host.docker.internal:host-gateway"
    healthcheck:
      test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'host.docker.internal:27017'}]}) }" | mongosh --port 27017 --quiet
      interval: 5s
      timeout: 30s
      start_period: 0s
      start_interval: 1s
      retries: 30
    volumes:
      - "mongo_data:/data/db"
      - "mongo_config:/data/configdb"

volumes:
  mongo_data:
  mongo_config:

参考文章

https://stackoverflow.com/questions/73222424/mongodb-badvalue-security-keyfile-is-required-when-authorization-is-enabled-wi

https://blog.csdn.net/dougsu/article/details/130692610

Responses