node 安装mysql 报错,Node.js MySQL-错误:连接ECONNREFUSED

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.

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'

});

}

I start the node.js server with:

$ node server.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)

解决方案

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

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

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

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

At the end you have:

var connection = mysql.createConnection({

host : config.host,

user : config.user,

password : config.pass,

database : config.db,

socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值