简体   繁体   English

MongoDB副本集,从mongoose连接时出错node.js

[英]MongoDB replica set, error when connecting from mongoose node.js

I am running mongodb replicaSet inside docker containers in Windows. This is docker compose file我在 Windows 中的 docker 容器内运行 mongodb replicaSet。这是 docker 组合文件

version: '3'

services:

  rs0:
    image: mongo:4.4
    ports:
      - "27018:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs0_data:/data/db
      - ./nmbprsdata0:/nmbpdata

  rs1:
    image: mongo:4.4
    ports:
      - "27019:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs1_data:/data/db
      - ./nmbprsdata1:/nmbpdata

  rs2:
    image: mongo:4.4
    ports:
      - "27020:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs2_data:/data/db
      - ./nmbprsdata2:/nmbpdata
      
  rs3:
    image: mongo:4.4
    ports:
      - "27021:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs3_data:/data/db
      - ./nmbprsdata3:/nmbpdata
      
  rs4:
    image: mongo:4.4
    ports:
      - "27022:27017"
    command: mongod --replSet rsnmbp
    volumes:
      - rs4_data:/data/db
      - ./nmbprsdata4:/nmbpdata
      
   

volumes:
  rs0_data:
  rs1_data:
  rs2_data:
  rs3_data:
  rs4_data:

Replica set is configured via副本集通过配置

rsconf = { 
  _id: "rsnmbp", 
  members: [ 
    { 
      _id: 0, 
      host: "rs0:27017" 
    }, 
    { 
      _id: 1, 
      host: "rs1:27017" 
    }, 
    { 
      _id: 2, 
      host: "rs2:27017" 
    },
    { 
      _id: 3, 
      host: "rs3:27017" 
    },
    { 
      _id: 4, 
      host: "rs4:27017" 
    },
  ] 
} 
rs.initiate(rsconf)

I am trying to connect to replica set via mongoose in node.js我正在尝试通过 node.js 中的 mongoose 连接到副本集

const DB_URI = 'mongodb://localhost:27018,localhost:27019,localhost:27020,localhost:27021,localhost:27022/test'
mongoose.connect(DB_URI)
    .then((result) =>console.log ("connected to database"))
    .catch((err) =>console.log (err))

but I am receiving following error但我收到以下错误

MongooseServerSelectionError: getaddrinfo ENOTFOUND rs0
 ...
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(5) {
      'rs0:27017' => [ServerDescription],
      'rs1:27017' => [ServerDescription],
      'rs2:27017' => [ServerDescription],
      'rs3:27017' => [ServerDescription],
      'rs4:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'rsnmbp',
    maxSetVersion: 1,
    maxElectionId: new ObjectId("7fffffff0000000000000003"),
    commonWireVersion: 9,
    logicalSessionTimeoutMinutes: undefined
  }
}

I have added following lines to etc/hosts on Windows我在 Windows 上的 etc/hosts 添加了以下几行

127.0.0.1 rs0
127.0.0.1 rs1
127.0.0.1 rs2
127.0.0.1 rs3    
127.0.0.1 rs4

and changed const DB_URI to并将 const DB_URI 更改为

const DB_URI = 'mongodb://rs0:27018,rs1:27019,rs2:27020,rs3:27021,rs4:27022/test'

but now I am receiving following error但现在我收到以下错误

MongooseServerSelectionError: Server selection timed out after 30000 ms
...
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(0) {},
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'rsnmbp',
    maxSetVersion: 1,
    maxElectionId: new ObjectId("7fffffff0000000000000003"),
    commonWireVersion: 9,
    logicalSessionTimeoutMinutes: undefined
  }
}

How can I connect to this replicaSet with mongoose in node.js Thanks in advance.我怎样才能用 node.js 中的 mongoose 连接到这个 replicaSet 在此先感谢。

You must use one Replica set & a Replica set can have multiple members.您必须使用一个副本集,一个副本集可以有多个成员。 and make sure that you have added Replica set nodes in the host machine in etc/hosts file.并确保您已经在 etc/hosts 文件中的主机中添加了副本集节点。 Just like the below example -就像下面的例子 -

127.0.0.1   mongoset1 mongoset2 mongoset3

Note - 127.0.0.1 is your host machine and mongoset1, mongoset2 and mongoset3 are the nodes (members) of the replicaset.注意 - 127.0.0.1 是您的主机,mongoset1、mongoset2 和 mongoset3 是副本集的节点(成员)。

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

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