我正在尝试设置ReplicaSet,但是初始化有问题。
我第一次跑步

db_1     | uncaught exception: Error: couldn't add user: not master
然后每次
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
我的设置是:
/ 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
/副本设置/ 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();
谢谢你的帮助!

最佳答案

您可以通过仅在docker-compose中使用基本mongo镜像来执行此操作
您的设置应如下所示:
/ 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 -保持不变
/ replicaSetup / setup.sh -保持不变
/ 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();

08-06 10:33