VPS Docker-compose 上的副本集 MongoDB

问题描述 投票:0回答:1

我在node.js 上有一个应用程序,需要带有副本集的MongoDB。我创建了 docker-compose.yml,它在本地运行得很好。

问题是当我在 VPS 上运行此文件时,我无法连接到它。 连接示例:

mongodb://6.255.60.110:27017,6.255.60.110:27018,6.255.60.110:27019/?replicaSet=rs0
(我更改了真实IP)

MongoDB Compass 抛出错误:

connect ECONNREFUSED 6.255.60.110:27017
Result command docker ps

version: "3.8"

services:
  mongo1:
    image: mongo:latest
    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',priority:1},{_id:1,host:'host.docker.internal:27018',priority:0.5},{_id:2,host:'host.docker.internal:27019',priority:0.5}]}) }" | mongosh --port 27017 --quiet
      interval: 5s
      timeout: 30s
      start_period: 0s
      start_interval: 1
      retries: 30
    volumes:
      - "mongo1_data:/data/db"
      - "mongo1_config:/data/configdb"

  mongo2:
    image: mongo:latest
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27018"]
    ports:
      - 27018:27018
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - "mongo2_data:/data/db"
      - "mongo2_config:/data/configdb"

  mongo3:
    image: mongo:latest
    command: ["--replSet", "rs0", "--bind_ip_all", "--port", "27019"]
    ports:
      - 27019:27019
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - "mongo3_data:/data/db"
      - "mongo3_config:/data/configdb"

volumes:
  mongo1_data:
  mongo2_data:
  mongo3_data:
  mongo1_config:
  mongo2_config:
  mongo3_config:

我也将端口从16797转发到27017,但没有帮助

当我在 VPS 上启动 1 个 mongoDB 服务器时,我可以连接到它 示例:

docker run mongo --port 27017

我也是按照网上的说明,手动添加的:

https://www.mongodb.com/compatibility/deploying-a-mongodb-cluster-with-docker

https://github.com/prisma/prisma/discussions/18958#discussioncomment-7269305

还有许多其他

mongodb docker docker-compose vps
1个回答
0
投票

我在 docker-compose 中为 MongoDB 副本集提供了一个有点不同但可行的解决方案。 (撰写文件中有一些有用的注释)

docker-compose.yml:

version: "3.8"

services:
  mongo1:
    image: mongo:4.4.5
    hostname: mongo1
    networks:
      - mongo_net
    # The following variables won't be used due to set entrypoint section
    # The replica-set sets automatically, but the admin user has to be set
    # only one time when you use new deploy.
    # Eg.:
    #    docker exec -it <docker-container-id> mongo
    #    use admin
    #    db.createUser({ user: "admin", pwd: "admin_pwd", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
    environment:
      - MONGO_INITDB_DATABASE=test_db
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin_pwd
      - MONGO_DATA_DIR=/data/db
    volumes:
      - mongo-1:/data/db
      - ./docker-entrypoint-mongo.sh:/data/docker-entrypoint.sh
    expose:
      - 27017
    entrypoint:
      - bash
      - -c
      - |
        chmod +x /data/docker-entrypoint.sh
        exec /data/docker-entrypoint.sh
    healthcheck:
      test: test $$(echo "rs.initiate({_id:'my-replica-set',members:[{_id:0,host:\"mongo1:27017\"},{_id:1,host:\"mongo2:27017\"}]}).ok || rs.status().ok" | mongo --quiet ) -eq 1
      interval: 10s
      start_period: 30s

  mongo2:
    image: mongo:4.4.5
    hostname: mongo2
    networks:
      - mongo_net
    environment:
      - MONGO_INITDB_DATABASE=test_db
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin_pwd
      - MONGO_DATA_DIR=/data/db
    volumes:
      - mongo-2:/data/db
      - ./docker-entrypoint-mongo.sh:/data/docker-entrypoint.sh
    expose:
      - 27017
    entrypoint:
      - bash
      - -c
      - |
        chmod +x /data/docker-entrypoint.sh
        exec /data/docker-entrypoint.sh

volumes:
  mongo-1:
  mongo-2:

networks:
  mongo_net: {}

docker-entrypoint-mongo.sh:

mongod "--enableMajorityReadConcern" "false" "--journal" "--dbpath" "/data/db" "--replSet" "my-replica-set" "--bind_ip_all"

我从同一网络中的另一个(Python)服务使用它:

self.__mongo_client = pymongo.MongoClient(
    host=["mongo1:27017", "mongo2:27017"],
    username="admin",
    password="admin_pwd",
    replicaset="my-replica-set",
)

但是“纯”连接字符串应该是这种情况:

mongodb://mongo1:27017,mongo2:27017/?replicaSet=my-replica-set
© www.soinside.com 2019 - 2024. All rights reserved.