简体   繁体   English

node.js mysql 错误:ECONNREFUSED

[英]node.js mysql error: ECONNREFUSED

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

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.我知道这个问题已经得到解答,但对我来说问题是 mysql 服务器侦听 Unix 套接字而不是 tcp 套接字。 So the solution was to add:所以解决方案是添加:

port: '/var/run/mysqld/mysqld.sock'

to the connection options.到连接选项。

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?你能试试 telnet 到 3308 端口吗? To see if the server is running on that port?查看服务器是否在该端口上运行?

telnet localhost 3308

Can you also try:你也可以试试:

mysql -hlocalhost -uroot -pxxx

Overview概述

For anyone else having this problem and is running .对于遇到此问题并正在运行任何其他人。 I suspected the problem had to do with the network and not MySQL or Node.js .我怀疑问题与网络有关,而不是MySQLNode.js

Solution解决方案

If you open MAMP and click MySQL in the left navigation panel it will pull up the MySQL options page.如果打开MAMP ,并点击MySQL左侧导航面板中就会拉起了MySQL选项页。 In the center of the page you will see a checkbox that says,在页面中央,您会看到一个复选框,上面写着,

"Allow network access to MySQL ". “允许网络访问MySQL ”。

Check this box and then restart your MAMP .选中此框,然后重新启动您的MAMP At this point you can now test your connection to MySQL with telnet or a node.js script.此时,您现在可以使用telnetnode.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.请记住,您可以通过打开MAMP并单击左侧导航面板上的端口链接来检查您的MySQL正在哪个port上运行。

Visual Aid视觉辅助

在此处输入图片说明

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 作为我的连接的默认端口,以使其正常工作。

截屏

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 implementation of 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.然后,您可以使用刚刚配置的root用户或创建一个新用户以在您的项目中使用。 For the latter:对于后者:

  • Enter mysql CLI by running mysql -u root -p .通过运行mysql -u root -p进入mysql CLI。

  • Enter the password for root user.输入root用户的密码。

  • Create a new database with CREATE DATABASE mydatabase;使用CREATE DATABASE mydatabase;创建一个新数据库CREATE DATABASE mydatabase; . .

  • Create a new user with CREATE USER test IDENTIFIED BY "testpass";使用CREATE USER test IDENTIFIED BY "testpass";创建一个新用户CREATE USER test IDENTIFIED BY "testpass"; . .

  • Grant privileges to test user to use your new database with GRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass";授予test用户使用新数据库的权限,并GRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass";授予所有权限GRANT ALL PRIVILEGES ON mydatabase.* TO test@localhost IDENTIFIED BY "testpass"; . . See for more information on this.有关这方面的更多信息,请参阅

And then, in my project, I would have:然后,在我的项目中,我会:

let connection = mysql.createConnection({
    host: "localhost",
    user: "test",
    password: "testpass",
    database: "mydatabase"
});

I also had this problem when I tried to connect and disconnect many times.当我多次尝试连接和断开连接时,我也遇到了这个问题。 I solved this problem by ending the connection and running the server after the connection closed, and now I can connect without problem.我通过结束连接并在连接关闭后运行服务器解决了这个问题,现在我可以毫无问题地连接了。

See below :见下文 :

mysql.createConnection()
connection.end();

After you prove the server runs well, remove connection.end() re-run the server.证明服务器运行良好后,删除connection.end()重新运行服务器。

If you are using MAMP please note that mysql default db_port is set to 8889 so you for this purpose I had to change it to 3306 (or whatever your app mysql db_port is set to).如果您使用 MAMP,请注意 mysql 默认 db_port 设置为 8889,因此您为此目的我必须将其更改为 3306(或您的应用程序 mysql db_port 设置为任何值)。

My issue was that node server was connecting using port 3306, so it was giving the error below then crashing but mysql was up and seemed to establishing a connection through localhost, although I couldnt test it because node server was down.我的问题是节点服务器正在使用端口 3306 进行连接,所以它给出了下面的错误然后崩溃但 mysql 已启动并且似乎通过本地主机建立连接,尽管我无法测试它,因为节点服务器已关闭。

errno: -61, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3306, fatal: true错误号:-61,代码:'ECONNREFUSED',系统调用:'connect',地址:'127.0.0.1',端口:3306,致命:true

Once I changed the port on MAMP mysql from 8889 to 3306, node server established connection through port 3000 and giving the statement below:一旦我将 MAMP mysql 上的端口从 8889 更改为 3306,节点服务器通过端口 3000 建立连接并给出以下语句:

server running on port 3000 The solution is: 2服务器运行在端口 3000 解决方案是: 2

I use Windows 10 and I have Windows Subsystem For Linux (WSFL).我使用 Windows 10 并且我有 Windows Subsystem For Linux (WSFL)。 I can execute my project from the WSFL console, but my MySQL is installed in Windows and they can not connect, however when I execute my project from a Windows console then it works without any problems.我可以从 WSFL 控制台执行我的项目,但是我的 MySQL 安装在 Windows 中并且它们无法连接,但是当我从 Windows 控制台执行我的项目时,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM