Node.js MySQL - 错误:连接 ECONNREFUSED [英] Node.js MySQL - Error: connect ECONNREFUSED

查看:54
本文介绍了Node.js MySQL - 错误:连接 ECONNREFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Node.js 服务器端.我在本地主机上尝试了我的代码,一切正常.我购买了一台服务器并在其上安装了 Apache 和 node.js 并在那里测试了我的 Web 应用程序.我正确地将 MySQL 连接配置从 localhost 更改为服务器配置.

I use Node.js server side. I tried my code on localhost and everything works fine. I bought a server and installed Apache and node.js on it and test my web application there. I correctly changed the MySQL connection configurations from localhost to the server configurations.

我使用此配置测试我的 Web 应用程序:

I test my web application with this configuration:

var mysql      = require('mysql');
var mysqlConnection;
function new_mysqlConnection() {
    mysqlConnection = mysql.createConnection({
      host     : 'myurl.at',
      user     : 'myusername',
      database : 'mydatabase',
      password : 'mypassword'
    });
}

我使用以下命令启动 node.js 服务器:

I start the node.js server with:

$ node server.js

当我加载页面时,它正确显示,但是当 Node.js 尝试连接到数据库时,我总是收到以下错误:

When I load the page, it's correctly displayed, but when Node.js try to connect to the database I always got the following error:

Error: connect ECONNREFUSED
    at errnoException (net.js:905:11)
    at Object.afterConnect [as oncomplete] (net.js:896:19)
    --------------------
    at Protocol._enqueue (/var/www/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/var/www/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/var/www/node_modules/mysql/lib/Connection.js:119:18)
    at reconnectDb (/var/www/server.js:319:18)
    at app.get.email (/var/www/server.js:109:2)
    at Layer.handle [as handle_request] (/var/www/node_modules/express/lib/router/layer.js:82:5)
    at trim_prefix (/var/www/node_modules/express/lib/router/index.js:302:13)
    at /var/www/node_modules/express/lib/router/index.js:270:7
    at Function.proto.process_params (/var/www/node_modules/express/lib/router/index.js:321:12)
    at next (/var/www/node_modules/express/lib/router/index.js:261:10)

推荐答案

需要在config对象中添加socket path的值:

You need to add the value of socket path to the config object:

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

在 MAMP 中,您转到 http://localhost:8888/MAMP,您会发现:>

In MAMP, you go to http://localhost:8888/MAMP, and you find:

/Applications/MAMP/tmp/mysql/mysql.sock

最后你有:

var connection = mysql.createConnection({
  host     : config.host,
  user     : config.user,
  password : config.pass,
  database : config.db,
  socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
});

这篇关于Node.js MySQL - 错误:连接 ECONNREFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆