简体   繁体   English

Docker 中的 Mongo ReplicaSet - 无法添加用户:不是主

[英]Mongo ReplicaSet in Docker - couldn't add user: not master

I am trying to set up a ReplicaSet but I'm having problem with the initialisation.我正在尝试设置一个 ReplicaSet,但我在初始化时遇到了问题。

The FIRST time I run我第一次跑步

db_1     | uncaught exception: Error: couldn't add user: not master 

And each time after每次之后

db_1     | {"t":{"$date":"2020-09-16T16:06:05.341+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn1","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":"user","authenticationDatabase":"admin","client":"172.18.0.5:37916","result":"UserNotFound: Could not find user \"user\" for db \"admin\""}}
db_1     | {"t":{"$date":"2020-09-16T16:06:05.342+00:00"},"s":"I",  "c":"ACCESS",   "id":20249,   "ctx":"conn1","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","principalName":"user","authenticationDatabase":"admin","client":"172.18.0.5:37916","result":"UserNotFound: Could not find user \"user\" for db \"admin\""}}
db_1     | {"t":{"$date":"2020-09-16T16:06:05.349+00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn1","msg":"connection ended","attr":{"remote":"172.18.0.5:37916","connectionCount":0}}
setup_1  | Error: Authentication failed. :
setup_1  | connect@src/mongo/shell/mongo.js:362:17
setup_1  | @(connect):2:6
setup_1  | exception: connect failed
setup_1  | exiting with code 1
docker_setup_1 exited with code 1

my setup is:我的设置是:

/ docker-compose.yml / docker-compose.yml

version: "3"
services:
  db:
    image: db
    build: 
      context: .
      dockerfile: DockerfileDb
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    ports:
      - "34000:34000"
    volumes:
      - mongodata:/data/db
      - ./mongologs:/data/logs
  db2:
    image: db
    ports:
      - "34001:34000"
    volumes:
      - mongodata2:/data/db
  db3:
    image: db
    ports:
      - "34002:34000"
    volumes:
      - mongodata3:/data/db
  setup:
    image: setup
    build: ./replicaSetup
    depends_on:
      - db
      - db2
      - db3
    links:
      - "db:database"
      - "db2:database2"
      - "db3:database3"
volumes:
    mongodata:
    mongodata2:
    mongodata3:

/ DockerfileDb / Dockerfile 数据库

FROM mongo
        
WORKDIR /usr/src/config

COPY replicaSetup/mongod.conf .
COPY replicaSetup/shared.key .

EXPOSE 34000

RUN chmod 700 shared.key
RUN chown 999:999 shared.key

CMD ["--config", "./mongod.conf"]

/ replicaSetup / mongod.conf /replicaSetup/mongod.conf

net: 
  port: 34000
  bindIpAll : true

security:  
  authorization: enabled 
  keyFile: ./shared.key

replication: 
  oplogSizeMB: 1024
  replSetName: amsdb

/ replicaSetup / Dockerfile /replicaSetup/Dockerfile

FROM mongo

# Create app directory
WORKDIR /usr/src/configs

# Install app dependencies
COPY replicaSet.js .
COPY setup.sh .

CMD ["./setup.sh"]

/ replicaSetup / setup.sh /replicaSetup/setup.sh

sleep 10 | echo Sleeping
mongo mongodb://database:34000 -u "user" -p "xxxx" replicaSet.js

/ replicaSetup / replicaSet.js /replicaSetup/replicaSet.js

rsconf = {
  _id : "amsdb",
  members: [
    { _id : 0, host : "database:34000"},
    { _id : 1, host : "database2:34001" },
    { _id : 2, host : "database3:34002" }
  ]
}

rs.initiate(rsconf);

rs.conf();

Thanks for any help!谢谢你的帮助!

You can do this by just using the base mongo image in docker-compose您只需使用 docker-compose 中的基本 mongo 映像即可完成此操作

Your setup should look like:您的设置应如下所示:

/ docker-compose.yml / docker-compose.yml

version: "3.0"

services:
  # Worker 1
  mongo1:
    image: mongo:latest
    volumes:
      - ./replicaSetup:/opt/keyfile
      - mongodata:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    ports:
      - 27017:27017
    command: 'mongod --auth --keyFile /opt/keyfile/shared.key --replSet amsdb'

  # Worker 2
  mongo2:
    image: mongo:latest
    volumes:
      - ./replicaSetup:/opt/keyfile
      - mongodata2:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    ports:
      - 27018:27017
    command: 'mongod --auth --keyFile /opt/keyfile/shared.key --replSet amsdb'

  # Worker 3
  mongo3:
    image: mongo:latest
    volumes:
      - ./replicaSetup:/opt/keyfile
      - mongodata3:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: xxxx
    ports:
      - 27019:27017
    command: 'mongod --auth --keyFile /opt/keyfile/shared.key --replSet amsdb'
volumes:
    mongodata:
    mongodata2:
    mongodata3:

/ replicaSetup / Dockerfile - stays the same / replicaSetup / Dockerfile - 保持不变

/ replicaSetup / setup.sh - stays the same /replicaSetup/setup.sh - 保持不变

/ replicaSetup / replicaSet.js /replicaSetup/replicaSet.js


rsconf = {
  _id : "amsdb",
  members: [
    { _id : 0, host : "172.17.0.1:27017", priority:1 },
    { _id : 1, host : "172.17.0.1:27018", priority:1 },
    { _id : 2, host : "172.17.0.1:27019", priority:1 }
  ]
}

rs.initiate(rsconf);

rs.conf();

At the time of writing "mongo:latest" resolves to v4.4.1.在撰写本文时,“mongo:latest”解析为 v4.4.1。 The answer is for that version of entrypoint.sh https://github.com/docker-library/mongo/blob/master/4.4/docker-entrypoint.sh答案是针对那个版本的 entrypoint.sh https://github.com/docker-library/mongo/blob/master/4.4/docker-entrypoint.sh

In order to process MONGO_INITDB_ROOT_* environment variables and add the user to the database, the database should be started in standalone mode.为了处理 MONGO_INITDB_ROOT_* 环境变量并将用户添加到数据库,数据库应该以独立模式启动。 It appears that the current implementation does not support replica set configuration in .conf file but only through command line arguments.似乎当前的实现不支持 .conf 文件中的副本集配置,而只能通过命令行参数。

Either pass arguments in Dockerfile command: "--bind_ip_all --replSet amsdb --port 34000 ... etc" or create a PR for docker-entrypoint.sh to support docker.conf在 Dockerfile command: "--bind_ip_all --replSet amsdb --port 34000 ... etc"传递参数command: "--bind_ip_all --replSet amsdb --port 34000 ... etc"或为 docker-entrypoint.sh 创建 PR 以支持 docker.conf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM