错误:访问mysql时连接ECONNREFUSED [英] Error: connect ECONNREFUSED while accessing mysql

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

问题描述

我是node.js的新手,正在尝试学习它.
我已经安装了node-mysql NPM模块. 在这里,我列出了访问mysql的代码:

I am new to node.js and trying to learn it.
I have installed node-mysql NPM module. Here i have listed my code to access mysql :

// Include http module, 
var http = require('http'), 
// And mysql module you've just installed. 
   mysql = require("mysql"); 

// Create the connection. 
// Data is default to new mysql installation and should be changed according to your configuration. 
var connection = mysql.createConnection({ 
   user: "root", 
   password: "", 
   database: "astitvabmt",
   host: '127.0.0.1',
   port: '3306',
   _socket: '/var/run/mysqld/mysqld.sock'
}); 

// Create the http server. 
http.createServer(function (req, res) {
   // Attach listener on end event. 

      // Query the database. 
      connection.query('SELECT * FROM tbluser;',function(err, results, fields) {
                if (err) throw err;
                console.log("sachin : " + results);
                var output = '<html><head></head><body><h1>Latest Posts</h1><ul><table border=1><tr>';
                for (var index in fields) {
                    output += '<td>' + fields[index].name + '</td>';
                }
                output += '</tr>';
                for (var index in results) {
                    output += '<tr><td>' + results[index].Recipe + '</td>';
                    output += '<td>' + results[index].Amount + '</td>';
                    output += '<td>' + results[index].Unit + '</td>';
                    output += '<td>' + results[index].Ingredient + '</td></tr>';
                }
                output += '</ul></body></html>';
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.end(output);
            });                       

// Listen on the 8080 port. 
}).listen(8080);
console.log('Server mysqlJS running at 8080');

但是我收到错误消息:

sachin@ubuntu:/opt/lampp/htdocs/1nodeapp$ node mysql.js
Server mysqlJS running at 8080

Error: connect ECONNREFUSED
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)
    --------------------
    at Handshake.Sequence (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:20)
    at new Handshake (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
    at Protocol.handshake (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/protocol/Protocol.js:42:50)
    at Connection.connect (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/Connection.js:72:18)
    at Connection._implyConnect (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/Connection.js:182:10)
    at Connection.query (/opt/lampp/htdocs/1nodeapp/node_modules/mysql/lib/Connection.js:97:8)
    at Server.<anonymous> (/opt/lampp/htdocs/1nodeapp/mysql.js:22:18)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2108:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)

我的mysql配置中没有跳过网络选项.
我已经在PC上安装了XAMPP.以前,我使用3306端口通过apache进行mysql连接.

I don't have skip-networking option in my mysql config.
I have installed XAMPP in my PC.Previously i use 3306 port for mysql connection using apache.

推荐答案

尝试使用socketPath参数代替_socket.

如此处所述 https://github.com/felixge/node-mysql#connection -选项

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

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