Error: getaddrinfo ENOTFOUND when trying to connect to remote database with correct credentials

Error: getaddrinfo ENOTFOUND when trying to connect to remote database with correct credentials

我想连接到我的数据库。我正在使用带有 mysql 数据库的 node.js 服务器。 Localhost 有效,但是当我尝试连接到我的数据库时出现此错误:getaddrinfo ENOTFOUND

const pool = mysql.createPool({
  connectionLimit: 10,
  password: "****",
  user: "***",
  host: "***",
  port: "3306",
  database: "***",
});

正如我所说 - 使用 localhsot 设置它可以工作但是当我用我的 ionos 凭据填写它时它不会工作

您在上面的评论中提到您的数据库由 IONOS 托管,并且您的代码是 运行 外部的。

不幸的是,IONOS 似乎阻止了从外部来源与其数据库产品的连接。 Heroku 绝对是一个外部资源。这个事实是 clearly documented on their help site:

Access to your IONOS MySQL database from your desktop computer or any other non-IONOS source is not possible:

In principle, you can only access your MySQL database through applications installed on your IONOS web space, e.g. with your own WordPress installation. Other access paths have been blocked for security reasons.

他们完成此限制的方式很可能是不发布 public 他们数据库的 DNS 记录,这就是为什么您的代码抱怨无法为其解析地址。

如果没有特殊安排,该供应商无法满足您的要求。如果这是 绝对 要求,您应该寻找替代数据库主机。