简体   繁体   English

Node.js MySQL错误:有时仅ECONNREFUSED

[英]Node.js MySQL error: ECONNREFUSED only sometimes

I have read through many similar questions such as node.js mysql error: ECONNREFUSED , but none of their solutions have solved my problem. 我已经阅读了许多类似的问题,例如node.js mysql错误:ECONNREFUSED ,但是他们的解决方案都无法解决我的问题。 For example, I ran mysql -hlocalhost -P 3306 -p*** on the command line and it properly connected to my database. 例如,我在命令行上运行了mysql -hlocalhost -P 3306 -p*** ,它已正确连接到我的数据库。 It seems that the problem is this specific script. 似乎问题在于此特定脚本。 I have another script with the exact same dbconnection.js file and it is connecting just fine, like the command line is. 我有另一个脚本,该脚本具有完全相同的dbconnection.js文件,并且可以很好地连接,就像命令行一样。

dbconnection.js dbconnection.js

var mysql = require('mysql');
var connection = mysql.createPool ({
  host: '127.0.0.1',
  user: 'root',
  password: ****,
  database: 'test_db',
  dateStrings: true,
  port: 3306
});
module.exports = connection;

And this works in one program, but not another. 这在一个程序中有效,但在另一个程序中无效。 The problem area of the problem script uses the following code: 问题脚本的问题区域使用以下代码:

Notifications.js Notifications.js

var db = require('../dbconnection');
var express = require('express');
var router = express.Router();

router.post('/', function(req,res,next) {
    handleNotification(req.body.value[0].resource);
    res.status(202).end();
});

async function handleNotification(resource) {

    db.query('SELECT of.name, po.PurchaseOrderID FROM order_files AS of \
    INNER JOIN purchase_orders AS po ON po.PurchaseOrderID = of.purchaseOrderID \
    INNER JOIN quotes ON po.PurchaseOrderID = quotes.PurchaseOrderID \
    WHERE quotes.QuoteID=10000', function(err, rows) {
      if (err) console.log(err);
      else console.log(rows);
    });
}

Yet I can run from bash, mysql -hlocalhost -P 3306 -p**** and then run the exact query shown and get the results. 但是我可以从bash运行mysql -hlocalhost -P 3306 -p**** ,然后运行显示的确切查询并获得结果。 I can also run this query from another script. 我也可以从另一个脚本运行此查询。

The error received is as follows: Error: connect ECONNREFUSED 127.0.0.1:3306 收到的错误如下: Error: connect ECONNREFUSED 127.0.0.1:3306

Is there a reason why this specific script would not be able to connect to the database? 为什么有这个特定脚本无法连接到数据库的原因?

The one that is failing is a TCP/IP connection attempt, the one that is succeeding is using a local unix socketfile. 失败的是TCP / IP连接尝试,成功的是使用本地unix套接字文件。

Reference: https://dev.mysql.com/doc/refman/5.7/en/connecting.html 参考: https : //dev.mysql.com/doc/refman/5.7/en/connecting.html

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. 在Unix上,MySQL程序特别对待主机名localhost ,与其他基于网络的程序相比,它可能与您期望的不同。 For connections to localhost , MySQL programs attempt to connect to the local server by using a Unix socket file. 对于与localhost连接,MySQL程序尝试使用Unix套接字文件连接到本地服务器。 This occurs even if a --port or -P option is given to specify a port number. 即使给--port-P选项指定端口号,也会发生这种情况。 To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1 , or the IP address or name of the local server. 为确保客户端与本地服务器建立TCP / IP连接,请使用--host-h指定主机名值127.0.0.1或本地服务器的IP地址或名称。 You can also specify the connection protocol explicitly, even for localhost , by using the --protocol=TCP option. 您也可以使用--protocol=TCP选项显式指定连接协议,即使是localhost也是如此。


That is to say, we can get the same behavior from the MySQL command line client (connection refused), attempting a TCP/IP connection with either 也就是说,我们可以从MySQL命令行客户端(连接被拒绝)获得相同的行为,尝试通过以下任一方式进行TCP / IP连接

mysql -h 127.0.0.1 -P 3306 -p****

-or- -要么-

mysql -h localhost -P 3306 -p**** --protocol=TCP

MySQL identifies a user by both user and host . MySQL通过userhost识别用户。

That is, 'root'@'localhost' is a different user than 'root'@'%' . 也就是说, 'root'@'localhost'是与'root'@'%' 不同的用户。 These users have separate passwords, separate permissions, etc. 这些用户具有单独的密码,单独的权限等。

To get a list of users defined in MySQL Server, we can execute a query like this: 要获取在MySQL Server中定义的用户列表,我们可以执行如下查询:

SELECT u.user
     , u.host
     , u.password
     , u.plugin
     , u.password_expired 
  FROM mysql.user u
 ORDER BY u.user, u.host

Note that if MySQL Server is started without --skip-name-resolve option, MySQL will do a reverse lookup of the IP address into a hostname. 请注意,如果不使用--skip-name-resolve选项启动MySQL Server,则MySQL会将IP地址反向查找为主机名。 That means users will need to be defined as 'user'@'db01.mydomain' and not 'user'@'127.0.0.1' 这意味着需要将用户定义为'user'@'db01.mydomain'而不是'user'@'127.0.0.1'

Reference: https://dev.mysql.com/doc/refman/5.7/en/host-cache.html 参考: https : //dev.mysql.com/doc/refman/5.7/en/host-cache.html


If that's not the problem, then it's likely a firewall rule preventing the connection, or the SELinux / AppArmor configuration. 如果这不是问题,则可能是防火墙规则阻止了该连接或SELinux / AppArmor配置。

Looking at the reported error message again, 再次查看报告的错误消息,

Error: connect ECONNREFUSED 127.0.0.1:3306

That doesn't look like the error message we would expect from MySQL Server. 这看起来不像我们期望从MySQL Server得到的错误消息。 That looks more like the network connection was refused, before we even got to MySQL Server. 看起来更像是网络连接被拒绝,甚至在我们进入MySQL Server之前。

If MySQL server was not allowing access, we'd expect to be getting an error like this from MySQL Server: 如果MySQL服务器不允许访问,我们期望从MySQL服务器收到这样的错误:

ERROR 1045 (28000): Access denied for user 'root'@'hostname' (using password: YES)

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

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