2

我是 node.js 的新手并试图学习它。
我已经安装了 node-mysql NPM 模块。在这里,我列出了访问 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 连接。

4

4 回答 4

5

尝试使用socketPath参数而不是_socket.

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

于 2013-11-12T18:47:33.297 回答
2

为 MySQL指定主机名/端口 ( localhost/ 3306) 和套接字路径 ( /var/run/mysqld/mysql.sock) 没有意义 - 它一次只能使用其中一个。选择适合您安装的任何一个。

于 2013-10-27T05:38:41.790 回答
0

“连接被拒绝”套接字错误通常 == 防火墙阻止了 mySQL 端口

有问题的端口通常是 3306。以下是 Windows 服务器的说明:

http://www.activeservers.com/page3610203.aspx

以下是 Linux 服务器的说明:

http://www.cyberciti.biz/tips/linux-iptables-18-allow-mysql-server-incoming-request.html

问:您是在尝试连接到同一服务器上的 mySQL(例如,Apache 与 MySQL 位于同一主机上),还是尝试从不同的远程主机连接到 mySQL?

于 2013-10-27T04:52:49.627 回答
0

如果您使用 MAMP,默认端口是 8889,它对我有用。

于 2018-12-30T07:49:46.490 回答