简体   繁体   中英

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.

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

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

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

net: 
  port: 34000
  bindIpAll : true

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

replication: 
  oplogSizeMB: 1024
  replSetName: amsdb

/ 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

sleep 10 | echo Sleeping
mongo mongodb://database:34000 -u "user" -p "xxxx" 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

Your setup should look like:

/ 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 / setup.sh - stays the same

/ 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. The answer is for that version of 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. It appears that the current implementation does not support replica set configuration in .conf file but only through command line arguments.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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