node.js 错误错误:getaddrinfo ENOTFOUND - mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25521755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
errorError: getaddrinfo ENOTFOUND - mysql
提问by Nathan
I'm running a server on c9.io using Node.js and trying to connect to Mysql I guess I get this error:
我正在使用 Node.js 在 c9.io 上运行服务器并尝试连接到 Mysql 我想我收到了这个错误:
Error: getaddrinfo ENOTFOUND
错误:getaddrinfo ENOTFOUND
because the connection to the db is wrong.
因为与数据库的连接是错误的。
I'm using this:
我正在使用这个:
var connection = mysql.createConnection({
host: "REMOTE_ADDR",
user: "MYUSERNAME", // this is replaced by my username
database: "c9",
port: 3306
});
Any idea what's wrong?
知道出了什么问题吗?
Thanks!
谢谢!
回答by Scott Graph
I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:
我知道自从被问到这个问题已经有一段时间了,但我今天大部分时间都在试图解决这个问题。检查什么:
Within your nodejs source:
在您的 nodejs 源中:
- Try it without the 'port' option unless on your db VM, MySQL is listening on a port besides 3306. I had this option specified but had connection problems until I removed it. And 3306 is already the default value for the option anyway.
- Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP address, no more errors. Also, I did not get this error while on Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in Azure, I got the problem.
- Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection
- 尝试不带“端口”选项,除非在您的数据库 VM 上,MySQL 正在侦听 3306 以外的端口。我指定了此选项,但在删除它之前遇到了连接问题。无论如何,3306 已经是该选项的默认值。
- 使用主机“选项”中的实际主机 IP 进行尝试。我使用的是实际域名,而且通常情况下,node-mysql 会抛出完全相同的“errorError: getaddrinfo ENOTFOUND”,当我使用 IP 地址时,不再有错误。此外,我在 AWS 中的 Ubuntu 实例上没有收到此错误。一旦我切换到 Azure 中的 Ubuntu VM,我就遇到了问题。
- 将“调试”连接选项设置为 true,这将为您详细跟踪连接期间发生的情况
On your db VM/Box/Instance:
在您的 db VM/Box/Instance 上:
- Ensure that MySQL is listening on the right port
- If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
- Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
- 确保 MySQL 正在侦听正确的端口
- 如果您在尝试使用池建立并发连接时看到错误,请检查您的 max_connections 和 max_user_connections 是否未更改 my.conf 中的默认设置
- 监视器“显示处理程序;” 在 MySQL 中查看是否有任何问题
回答by Jo?o Pimentel Ferreira
After one hour, and I don't know why, I found the issue.
一小时后,我不知道为什么,我发现了问题。
In my case I replaced
就我而言,我替换了
host: 'localhost'
by
经过
host: '127.0.0.1'
回答by daniel ernest
This code works for me.
这段代码对我有用。
var mysql = require('mysql');
var con = mysql.createConnection({
host: '127.0.0.1',
port: '3306',
user: 'yourUsername',
password: '**********'
});
con.connect(function(err){
if(err) throw err;
console.log('connected!');
});
the host and port name find them in your mysql server. in-place of '127.0.0.1' you can use 'localhost'
主机名和端口名可以在您的 mysql 服务器中找到它们。代替 '127.0.0.1' 你可以使用 'localhost'
回答by zshan4444
I was facing this issue. I did fix it simply you have to change your configuration of a connection stringlike if you are running on local machine try
我正面临这个问题。我确实修复了它,只是您必须更改连接字符串的配置,就像您在本地机器上运行一样尝试
host:'localhost' or host:'127.0.0.1'
主机:'localhost' 或主机:'127.0.0.1'
...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.
...并设置您的用户名,如果您希望它在服务器上发布您的代码,则根据服务器提供主机,如果在 docker 容器中,将其命名为host:'db'。
回答by Omore
I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd. I tried multiple ways e.g
差不多花了两个小时才弄明白mysql数据库有什么问题。我正在使用 Windows cmd。我尝试了多种方式,例如
npm install mysql2- Changing
localhost to 127.0.0.1 - Refreshing node
npm install mysql2- 改变
localhost to 127.0.0.1 - 刷新节点
so in order to resolve this issue
所以为了解决这个问题
[Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]
I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.
我打开 XAMPP 并启动 MySQL 数据库,然后手动检查凭据并且对我来说工作正常。
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'test',
password: 'test123',
});

