node mysql无法脱机工作

时间:2016-03-18 20:36:45

标签: node.js node-mysql

我有一个在node.js服务器下工作的应用程序,我的问题是当有互联网时节点mysql工作正常,但它不能脱机工作。

mysql代码:

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'database'
});
connection.connect();

错误是:

Error: getaddrinfo ENOENT localhost:3306
at Object.exports._errnoException (util.js:870:11)
at errnoException (dns.js:31:15)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:77:26)
--------------------
at Protocol._enqueue (C:\Users\sam\workspace\autocar\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Users\sam\workspace\autocar\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Users\sam\workspace\autocar\node_modules\mysql\lib\Connection.js:125:18)
at Object.<anonymous> (C:\Users\sam\workspace\autocar\server.js:18:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)

1 个答案:

答案 0 :(得分:2)

尝试使用127.0.0.1代替localhost,似乎解析名称时出现问题。如果localhost解析为::1(IPv6),但MySQL仅监听127.0.0.1(IPv4),这也会有所帮助。