简体   繁体   English

MongoDB 错误:连接到 mongo 副本集时无法访问集 [set_name] 的主服务器

[英]MongoDB Error: Unable to reach primary for set [set_name] when connect to mongo replica set

I'm trying to connect to MongoDB replica set consists of 3 mongo docker-container, but Error Message: Unable to reach primary for set rs0 occurs.我正在尝试连接到由 3 个 mongo docker-container 组成的 MongoDB 副本集,但出现错误消息: Unable to reach primary for set rs0

Git Repository: https://github.com/frontalnh/mongodb-replica-set Git 仓库: https : //github.com/frontalnh/mongodb-replica-set

I made docker swarm consists of 3 MongoDB docker-container and map each port to localhost: 27017, 27018, 27019我让 docker swarm 由 3 个 MongoDB docker-container 组成,并将每个端口映射到本地主机:27017、27018、27019

Connecting to a single mongo docker-container is possible by below command可以通过以下命令连接到单个 mongo docker-container

mongo localhost:27017

But When I try to connect to Replica set consists of 3 by below command,但是当我尝试通过以下命令连接到由 3 个组成的副本集时,

Error Message: Unable to reach primary for set rs0 occurs错误消息:发生Unable to reach primary for set rs0

Command命令

mongo "mongodb://localhost:27017,localhost:27018,localhost:27019/testdb?replicaSet=rs0"

Configuration配置

cfg = {
  _id: 'rs0',
  members: [
    { _id: 0, host: 'mongo-rs0-1:27017' },
    { _id: 1, host: 'mongo-rs0-2:27017' },
    { _id: 2, host: 'mongo-rs0-3:27017' }
  ]
};
cfg.protocolVersion = 1;
rs.reconfig(cfg, { force: true });

Docker Compose Docker 撰写

version: '3'

services:
  mongo-rs0-1:
    image: 'mongo-start'
    build: ./mongo-rs0-1
    ports:
      - '27017:27017' # left is computer's port right side is docker internal port
    volumes:
      - ./mongo-rs0-1/data:/data/db
    depends_on:
      - 'mongo-rs0-2'
      - 'mongo-rs0-3'

  mongo-rs0-2:
    image: 'mongo'
    command: --replSet rs0
    command: --config ./conf/mongo.conf
    ports:
      - '27018:27017'
    volumes:
      - ./mongo-rs0-2/data:/data/db
      - ./mongo-rs0-2/conf:/conf

  mongo-rs0-3:
    image: 'mongo'
    command: --replSet rs0
    command: --config ./conf/mongo.conf
    ports:
      - '27019:27017'
    volumes:
      - ./mongo-rs0-3/data:/data/db
      - ./mongo-rs0-2/conf:/conf

  setup-rs:
    image: 'setup-rs'
    build: ./setup
    depends_on:
      - 'mongo-rs0-1' # mongo-rs0-1 서비스가 실행중이어야 해당 서비스가 실행될 수 있다.

  adminmongo:
    image: 'mrvautin/adminmongo'
    ports:
      - '1234:1234'

I solved it by register host name in localhost我通过在 localhost 中注册主机名解决了它

Anyone who needs mongo replica set in localhost can setup replica set with docker-compose.任何需要在 localhost 中设置 mongo 副本的人都可以使用 docker-compose 设置副本集。

https://github.com/frontalnh/mongodb-replica-set https://github.com/frontalnh/mongodb-replica-set

You were maybe running the other node of the replica set in fork mode and you turned off your computer at some point.您可能正在以 fork 模式运行副本集的另一个节点,并且在某个时候关闭了计算机。 Restarting those nodes in fork mode by doing mongod --config [your_config_file.conf] --fork fixed the problem for me.通过执行mongod --config [your_config_file.conf] --fork以 fork 模式重新启动这些节点为我解决了问题。

What happens when all other secondary nodes are unreachable is that the primary node becomes secondary therefore you will be unable to elect a new primary.当所有其他辅助节点都无法访问时,主节点变为辅助节点,因此您将无法选择新的主节点。

Try to restart the other nodes using their configuration files and you're good to go.尝试使用其他节点的配置文件重新启动它们,您就可以开始了。

Just restart the current primary node.只需重新启动当前的主节点。 Repeat this until the desired node become primary.重复此操作,直到所需节点成为主节点。 After that, you are able to run mongos.之后,您就可以运行 mongos。

Note: Make sure that there isn't traffic to the database.注意:确保没有流量到数据库。

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

相关问题 MongoDB 无法到达主要设置'<new replica set name> '</new> - MongoDB Unable to reach primary for set '<new replica set name>' MongoDB:当 Primary 关闭时,MongoClient 无法连接到 Secondary 副本集 - MongoDB : MongoClient Unable to connect to Secondary replica set when Primary is down 尝试连接到Mongo中的副本集时出错 - Error when try to connect to Replica Set in Mongo 连接到 kubernetes 上的主副本集 mongo - Connect to primary replica set mongo on kubernetes 无法通过NodeJS连接到MongoDB-在副本集中找不到主数据库错误 - Cannot connect to MongoDB via NodeJS - No primary found in replica set error mongodb辅助副本无法连接到主要集 - mongodb secondary replica cannot connect to primary set 使用docker在MongoDB中设置的副本集,主副本出现错误,并且在将另一个成员添加到副本集中时不再是主副本 - replica set in MongoDB using docker, primary has error and stops being primary when another member is added to the set Mongo DB无法连接到副本集的主节点 - Mongo DB Could not connect to a primary node for replica set 当使用连接字符串连接到 MongoDB 副本集时,我们如何仅连接到主集? - How do we connect to only Primary Set when connected to MongoDB replica set using connection string? MongoDB副本集仅具有主副本 - MongoDB replica set with primary only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM