node.js mysql 错误:ECONNREFUSED
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21206801/
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
node.js mysql error: ECONNREFUSED
提问by clarkk
Why can't I connect to the mysql server?
为什么我无法连接到mysql服务器?
On the same server an Apache/PHP server is running and it connects without problems!?
在同一台服务器上运行 Apache/PHP 服务器并且它连接没有问题!?
var mysql_link = {
host : 'localhost',
port : 3308,
database: 'nodetest',
user : 'root',
password : 'xxx'
};
var connection = mysql.createConnection(mysql_link);
connection.connect(function(err){
console.log(err);
if(err != null){
response.write('Error connecting to mysql:' + err+'\n');
}
});
connection.end();
error
错误
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }
update
更新
root@dyntest-amd-6000-8gb /var/www/node/dyntest # ps ax | grep mysqld
7928 pts/0 S+ 0:00 grep mysqld
28942 ? S 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid
29800 ? Sl 17:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/lib/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
采纳答案by Pez Cuckow
If this has worked before, my first guess would be that you've already got a copy of your node.js script running in the background which is holding the connection.
如果这以前有效,我的第一个猜测是您已经获得了在后台运行的 node.js 脚本的副本,该脚本持有连接。
I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets.
我相信连接被拒绝是一条 tcp/ip 错误消息,而不是来自 MySQL 的消息,它表明它没有运行或正在另一个端口或套接字上运行。
Could you try telnet'ing to port 3308? To see if the server is running on that port?
你能试试 telnet 到 3308 端口吗?查看服务器是否在该端口上运行?
telnet localhost 3308
Can you also try:
你也可以试试:
mysql -hlocalhost -uroot -pxxx
回答by Victor Dodon
I know this question has been answered, but for me the problem was that the mysql server listens on a Unix socket not on a tcp socket. So the solution was to add:
我知道这个问题已经得到解答,但对我来说问题是 mysql 服务器侦听 Unix 套接字而不是 tcp 套接字。所以解决方案是添加:
port: '/var/run/mysqld/mysqld.sock'
to the connection options.
到连接选项。
回答by wuno
Overview
概述
For anyone else having this problem and is running mamp. I suspected the problem had to do with the network and not MySQL
or Node.js
.
对于遇到此问题并正在运行mamp 的任何其他人。我怀疑问题与网络有关,而不是MySQL
或Node.js
。
Solution
解决方案
If you open MAMP
and click MySQL
in the left navigation panel it will pull up the MySQL options page. In the center of the page you will see a checkbox that says,
如果您打开MAMP
并单击MySQL
左侧导航面板,它将拉出 MySQL 选项页面。在页面中央,您会看到一个复选框,上面写着,
"Allow network access to
MySQL
".
“允许网络访问
MySQL
”。
Check this box and then restart your MAMP
. At this point you can now test your connection to MySQL with telnet
or a node.js
script.
选中此框,然后重新启动您的MAMP
. 此时,您现在可以使用telnet
或node.js
脚本测试与 MySQL 的连接。
Hint
暗示
Remember you can check which port
your MySQL
is running on by opening MAMP
and clicking the ports link on the left navigation panel.
记住,你可以检查哪些port
您MySQL
是通过打开运行MAMP
,然后点击左侧导航面板上的端口连接。
Visual Aid
视觉辅助
回答by dhan30
For some very odd reason, my computer only allowed me to have port 3306 as default port for my connection in order for it to work.
由于一些非常奇怪的原因,我的计算机只允许我将端口 3306 作为我的连接的默认端口,以使其正常工作。
回答by Montaser Almohasen
I have also this problem when i try to connect and disconnect many time, I was solved this problem by end connection and running the server, after connection closed, now i can connect without problems
当我多次尝试连接和断开连接时,我也遇到了这个问题,我通过结束连接并运行服务器解决了这个问题,连接关闭后,现在我可以毫无问题地连接了
As below :
如下 :
mysql.createConnection()
connection.end();
after the server running well, remove connection.end() function from your code and re-run the server
mysql.createConnection()
connection.end();
服务器运行良好后,从代码中删除 connection.end() 函数并重新运行服务器
回答by chick3n0x07CC
I wanted to comment my solution here, just in case there were people as newbie as me in databases.
我想在这里评论我的解决方案,以防万一数据库中有像我这样的新手。
I was getting this error because I had installed the mysql
NPM package correctly but I hadn't installed any implementation of MySQL on my computer(I didn't know I had to).
我收到这个错误是因为我已经mysql
正确安装了NPM 包,但我没有在我的计算机上安装任何 MySQL 实现(我不知道我必须这样做)。
I'm using Arch Linux so, in my case, with the NPM package already installed in my project, I did pacman -Syu mariadb
(MariaDB is the default implementationof MySQL in Arch Linux) and then configured it following the guide.
我正在使用 Arch Linux,所以,在我的情况下,我的项目中已经安装了 NPM 包pacman -Syu mariadb
(MariaDB 是Arch Linux 中 MySQL的默认实现),然后按照指南进行配置。
Then, you can use the root
user you just configured or create a new one to use in your project. For the latter:
然后,您可以使用root
刚刚配置的用户或创建一个新用户以在您的项目中使用。对于后者:
Enter
mysql
CLI by runningmysql -u root -p
.Enter the password for
root
user.Create a new databasewith
CREATE DATABASE mydatabase;
.Create a new userwith
CREATE USER test IDENTIFIED BY "testpass";
.Grant privileges to
test
user to use your new database withGRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass";
. Seefor more information on this.
mysql
通过运行进入CLImysql -u root -p
。输入
root
用户的密码。创建一个新的数据库与
CREATE DATABASE mydatabase;
。创建一个新用户使用
CREATE USER test IDENTIFIED BY "testpass";
。授予
test
用户使用新数据库的权限GRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass";
。有关这方面的更多信息,请参阅。
And then, in my project, I would have:
然后,在我的项目中,我会:
let connection = mysql.createConnection({
host: "localhost",
user: "test",
password: "testpass",
database: "mydatabase"
});