node-mysql 错误:连接 ECONNREFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22924902/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:23:51  来源:igfitidea点击:

node-mysql error: connect ECONNREFUSED

mysqlnode.jsnginxnode-mysql

提问by Skoua

I'm trying to setup a remote connection between my database server and a client node app using node-mysql.

我正在尝试使用 node-mysql 在我的数据库服务器和客户端节点应用程序之间设置远程连接。

When I try to connect to the remote db, I get this error:

当我尝试连接到远程数据库时,出现此错误:

node.js:201
    throw e; // process.nextTick error, or 'error' event on first tick
          ^
Error: connect ECONNREFUSED
    at errnoException (net.js:646:11)
    at Object.afterConnect [as oncomplete] (net.js:637:18)

Connecting to a local db works ok (with the socketPort parameter).

连接到本地数据库工作正常(使用 socketPort 参数)。

I can connect to this remote db with PHP from my computer localhost as well as another server I own so I don't think there's something wrong with mysql conf.

我可以使用 PHP 从我的计算机本地主机以及我拥有的另一台服务器连接到这个远程数据库,所以我认为 mysql conf 没有问题。

For info, nodejs is running with nginx and I've setup a proxy to make node work on port 80, maybe this is the issue?

有关信息,nodejs 正在与 nginx 一起运行,我已经设置了一个代理以使节点在端口 80 上工作,也许这就是问题所在?

How can I check that?

我该如何检查?

Thanks.

谢谢。

EDIT

编辑

Here's my code, just in case:

这是我的代码,以防万一:

var express = require('express');
var mysql = require('mysql');
var app = express();
var connection = mysql.createConnection({
    debug: false,
    host: '12.34.56.67',
    user: 'user',
    password: 'pass'
});

回答by Zaman-A-Piri Pasa

Try using mysql socket:

尝试使用 mysql 套接字:

var connection = mysql.createConnection({
    user: 'user',
    password: 'pass',
    socketPath: 'mysql-socket-path', /*example: /Applications/MAMP/tmp/mysql/mysql.sock*/
    database: 'dbname'
});

回答by Lucian

Should one still look for an answer:

是否仍应寻找答案:

Check your MySql server's configuration.

检查您的 MySql 服务器的配置。

In my case running netstat -ln | grep mysql(as per Troubleshooting Problems Connecting to MySQL) revealed that my server is listening on the socket /tmp/mysql.sockso in createConnection's options I used socketPath: '/tmp/mysql.sock'instead of host:and port:

在我的情况下运行netstat -ln | grep mysql(根据故障排除问题连接到 MySQL)显示我的服务器正在侦听套接字,/tmp/mysql.sock因此在createConnection我使用的选项中socketPath: '/tmp/mysql.sock'而不是host:port:

回答by Brendan

This error has to do with the MySQL client not being able to connect to the host:portgiven. As defined in errno.h, ECONNREFUSED is an error that is thrown when a connection is refused - this is possibly caused by:

此错误与 MySQL 客户端无法连接到host:port给定的客户端有关。如 中所定义errno.h,ECONNREFUSED 是连接被拒绝时抛出的错误 - 这可能是由以下原因引起的:

  • a firewall such as iptablesblocking the port
  • no such process running on the port
  • the process is running on a different host
  • the host or port were incorrect
  • 防火墙,例如iptables阻止端口
  • 端口上没有运行这样的进程
  • 进程在不同的主机上运行
  • 主机或端口不正确

In my case, my MySQL server was not correctly bound to 0.0.0.0for external connections, instead being bound to 127.0.0.1, as it is by default; however you can change this.

就我而言,我的 MySQL 服务器没有正确绑定到0.0.0.0外部连接,而是绑定到127.0.0.1,默认情况下;但是你可以改变这一点

The error does not originate from the node-mysqlpackage, as doubly shown by the stacktrace, which only shows errors from net.js.

错误并非源自node-mysql包,如堆栈跟踪所双重显示,仅显示来自net.js.

回答by arupjbasu

I added port:3306, that solved the issue .

我添加了端口:3306,解决了这个问题。

var connection = mysql.createConnection
    ({
        user: 'root',
        password: 'root',
        server: 'localhost',
    port:3306,
        database: 'abcd',
        insecureAuth: true,
        dialect: 'mysql',   
        multipleStatements: true,
        pool: {
            max: 5,
            min: 0,
            acquire: 30000,
            idle: 10000
        }
    });

    connection.connect();

回答by Skoua

Ok so I checked with node-mysql dev and it seems that it's not a node-mysql bug, but it was hard to investigate more.

好的,所以我检查了 node-mysql dev,它似乎不是 node-mysql 错误,但很难进行更多调查。

Anyway, I found this lib which works, so I'll go with it.

无论如何,我发现这个库有效,所以我会去使用它。

https://github.com/mariano/node-db-mysql

https://github.com/mariano/node-db-mysql

回答by Marcel Ennix

i had the same problem. solved it, by set the right port in the createConnection function options.

我有同样的问题。通过在 createConnection 函数选项中设置正确的端口来解决它。

get your port by using following command: netstat -tlnp

使用以下命令获取您的端口:netstat -tlnp

search for program name mysqld or just named.

搜索程序名称 mysqld 或只是命名。

回答by Russo

Use the command: yarn run start, and DO NOT use: cd /folderX && yarn run start

使用命令:yarn run start,不要使用:cd /folderX && yarn run start