无法使用Node.js的MongoClient连接到MongoDB。

huangapple go评论69阅读模式
英文:

Unable to Connect to MongoDB using Node.js MongoClient

问题

我在尝试使用mongodb驱动程序从我的Node.js应用程序连接到MongoDB时遇到了问题。

我在本地计算机上运行了一个MongoDB服务器,可以使用MongoDB Compass和VS Code扩展成功连接到它。

但是,当我尝试从我的Node.js应用程序连接时,我遇到了一个错误。

const { MongoClient } = require('mongodb');

const uri = 'mongodb://localhost:27017/';
const client = new MongoClient(uri);

client.connect((err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log('Connected to MongoDB');

  // ... 继续我的代码
});

我收到的错误消息是:

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
  // 错误的其他详细信息...

我尝试从Node.js应用程序连接到MongoDB。

我已经检查了以下内容:

  • MongoDB服务器正在我的本地计算机上运行,可以使用MongoDB Compass和VS Code扩展连接。
  • 我已经使用npm安装了mongodb包,并尝试了不同版本的包。
  • 连接到我的MongoDB服务器不需要身份验证。
  • 我还尝试在安装过程中指定mongodb包的版本,但问题仍然存在。

为什么我无法从我的Node.js应用程序连接到MongoDB?我的代码或配置中是否漏掉了什么?

英文:

I'm facing an issue while trying to connect to MongoDB from my Node.js application using the mongodb driver.

I have a MongoDB server running on my local machine, and I can successfully connect to it using MongoDB Compass and the VS Code extension.

However, when I attempt to connect from my Node.js application, I encounter an error.

const { MongoClient } = require('mongodb');

const uri = 'mongodb://localhost:27017/';
const client = new MongoClient(uri);

client.connect((err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log('Connected to MongoDB');

  // ... continue with my code
});

The error message I receive is:

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
  // rest of the error details...

I was try to connect to MongoDB from a Node.js app.

I have already checked the following:

  • The MongoDB server is running on my local machine, and I can connect using MongoDB Compass and VS Code extension.
  • I have installed the mongodb package using npm, and I tried different versions of the package as well.
  • There is no authentication required to connect to my MongoDB server.
  • I have also tried specifying the version of the mongodb package during installation, but the issue persists.

Why I can't connect to MongoDB from my Node.js application? Am I missing something in my code or configuration?

答案1

得分: 1

尝试将 URI 中的 localhost 替换为显式的本地 IPv4 地址 127.0.0.1

const uri = 'mongodb://127.0.0.1:27017/';

如在 Node 的 MongoDB 驱动程序 中所解释的:

> 注意:解决 DNS 连接问题
>
> Node.js 18 更改了默认的 DNS 解析顺序,从始终优先使用 ipv4 更改为使用 DNS 提供程序返回的顺序。在某些环境中,这可能导致 localhost 解析为 ipv6 地址而不是 ipv4 地址,从而导致无法连接到服务器。
>
> 可以通过以下方式解决:[...]
>
> - 在 localhost 处使用主机 127.0.0.1

英文:

Try replacing localhost in the URI by explicit locsl IPv4 127.0.0.1:

const uri = 'mongodb://127.0.0.1:27017/';

As explained on mongodb driver for Node:

> NOTE: Resolving DNS Connection issues
>
> Node.js 18 changed the default DNS resolution ordering from always prioritizing ipv4 to the ordering returned by the DNS provider. In some environments, this can result in localhost resolving to an ipv6 address instead of ipv4 and a consequent failure to connect to the server.
>
> This can be resolved by: [...]
>
> - using a host of 127.0.0.1 in place of localhost

答案2

得分: 0

大家好,感谢大家提供的有用评论,
解决这个问题分为两步

  1. 本地主机别名解析为IPv6地址::1,而不是127.0.0.1,
    但是,net.ipv6默认为false。
    解决方法可以是:

A. 在mongod.conf文件中设置如下:

  ipv6: true
  bindIpAll: true

或者

  ipv6: true
  bindIp: localhost

B. 在启动Mongo服务器时只需添加以下命令:

mongod --bind_ip_all --ipv6

  1. 我没有mongod.conf文件,所以我不得不自己编写一个,
    以下是它应该看起来的样子:
# mongod.conf

# 数据存储位置。
storage:
  dbPath: C:\data\db

# IPv6支持。
net:
  ipv6: true
  bindIpAll: true

# 监听端口(默认是27017)。
# port: 27017

# 启用/禁用日志。
# storage:
#   journal:
#     enabled: true

# 设置日志文件。
# systemLog:
#   destination: file
#   path: C:\data\log\mongod.log

# 副本集选项。
# replication:
#   replSetName: "rs0"

# 认证选项。
# security:
#   authorization: enabled

# 根据需要添加更多选项和配置。

前两个是必须启用的,其他是可选的。

此外,我建议参考这个问题,因为它与我的问题相似:
https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver

英文:

hi everyone thank you all for your helpful comments,
the answer to this problem is in two steps

  1. the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1,
    However, net.ipv6 defaults to false.
    the solution to this is to either

A. set them in mongod.conf file

  ipv6: true
  bindIpAll: true

or

  ipv6: true
  bindIp: localhost

B. just add them to the command when starting Mongo server

mongod --bind_ip_all --ipv6

  1. I didn't have a mongod.conf file soo I had to write one,
    here is what it should look like
# mongod.conf

# Where to store data.
storage:
  dbPath: C:\data\db

# IPv6 support.
net:
  ipv6: true
  bindIpAll: true

# Port to listen on (default is 27017).
# port: 27017

# Enable/disable journaling.
# storage:
#   journal:
#     enabled: true

# Set log files.
# systemLog:
#   destination: file
#   path: C:\data\log\mongod.log

# Replica Set Options.
# replication:
#   replSetName: "rs0"

# Authentication Options.
# security:
#   authorization: enabled

# More options and configurations can be added as needed.

the first two and are enabled the others are optional

also I would refer to this problem as it's like mine
https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver

huangapple
  • 本文由 发表于 2023年7月18日 10:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定