gpt4 book ai didi

node.js - 无法从主机访问副本集mongodb(docker)

转载 作者:行者123 更新时间:2023-12-02 18:34:11 28 4
gpt4 key购买 nike

我的nodejs代码在主机(MACOS)上运行,并且正在尝试连接到在docker中运行的mongodb副本集。

version: "3"

services:
redis_master:
image: redis:2.8
volumes:
- "/Users/hiteshbaldaniya/docker-redis/master:/data/"
ports:
- "6379:6379"
networks:
- database

mongodb_primary:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27017/" --port 27017
ports:
- "27017:27017"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database

mongodb_secondary1:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27018/" --port 27018
ports:
- "27018:27018"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database

mongodb_secondary2:
build:
context: ./
dockerfile: DockerfileDB
command: mongod --replSet "hdbrs" --dbpath "/data/27019/" --port 27019
ports:
- "27019:27019"
volumes:
- "/Users/hiteshbaldaniya/docker-mongodb/:/data/"
networks:
- database

hdb_nginx:
build:
context: ./nginx/
dockerfile: DockerfileNginx.dev
ports:
- "8081:80"
volumes:
- "/Users/hiteshbaldaniya/logs/docker-nginx/:/var/log/nginx/"
networks:
- backend

networks:
backend:
driver: bridge
database:
driver: bridge
我的主机上所有3个端口都是打开的,我尝试使用telnet进行连接,我也能够连接到所有端口。
我的nodejs应用程序使用mongodb-node-driver并使用以下配置。
module.exports = {
servers: [{
host: 'localhost',
port: 27017,
},
{
host: 'localhost',
port: 27018,
},
{
host: 'localhost',
port: 27019,
},
],
database: 'mydatabase',
options: {
"raw": false,
"poolSize": 5,
"readPreference": "primaryPreferred",
"w": 1,
"wtimeout": 12000,
"replicaSet": "hdbrs"
}
};
连接到mongodb时,我的应用程序引发以下错误,有人可以在这里帮助我吗?
{ MongoNetworkError: failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]
at Pool.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/topologies/server.js:505:11)
at Pool.emit (events.js:198:13)
at Connection.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/pool.js:329:12)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Socket.<anonymous> (/Users/hiteshbaldaniya/Applications/contentstack-migration/node_modules/mongodb-core/lib/connection/connection.js:245:50)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
name: 'MongoNetworkError',
message:
'failed to connect to server [mongodb_primary:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND mongodb_primary mongodb_primary:27017]' }
谢谢,

最佳答案

我能够找出解决方案。通过在我的/ etc / hosts文件中添加主机条目。

127.0.0.1 mongodb_primary
127.0.0.1 mongodb_secondary1
127.0.0.1 mongodb_secondary2
我仍然不确定从主机连接到在docker中运行的mongodb时localhost有什么问题,我仍然需要了解它的行为。
仅针对该节点,我还尝试在运行mongodb的命令期间在命令行中添加--bind_ip_all。
让我知道是否有人知道。谢谢

关于node.js - 无法从主机访问副本集mongodb(docker),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63261941/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com