使用node.js和MySQL时连接ECONNREFUSED时出错

时间:2014-11-07 06:17:04

标签: mysql node.js

我试图在node.js和mysql之间创建一个链接。连接ECONNREFUSED时出错。如何解决?

CODE:

var mysql = require('mysql');



var conn = mysql.createConnection({

        host : "192.164.0.114",
        user : "supriya",
        password : "root123@",
        database: "student"
}
);


var queryString = "SELECT * FROM studata";

conn.query("someurlhere",queryString, function(error, results)
{

    if(error)
    {
        throw error;
    }
    else {
        console.log(results);
    }

})

conn.end();

错误是:

Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)
    --------------------
    at Protocol._enqueue (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:135:48)
    at Protocol.handshake (/home/supriya/node_modules/mysql/lib/protocol/Protocol.js:52:41)
    at Connection.connect (/home/supriya/node_modules/mysql/lib/Connection.js:109:18)
    at Connection._implyConnect (/home/supriya/node_modules/mysql/lib/Connection.js:387:10)
    at Connection.query (/home/supriya/node_modules/mysql/lib/Connection.js:174:8)
    at Object.<anonymous> (/home/supriya/node_js/example2.js:19:6)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我调用了node server.js,其中说的是

  

在PORT 3000上启动MySQLError的问题:连接   ECONNREFUSED

通过第一行,我首先假设它启动了mysql服务器。但事实并非如此。它启动节点服务器,尝试连接到mysql服务器,节点服务器说ECONNREFUSED。显然,mysql服务器没有运行。当我以root身份登录mysql workbench并尝试执行简单查询时,我发现了这一点。它在角落里说,&#34; ..在3306端口,它没有连接&#34;或者......那样的。 所以,结论是在某个端口上启动server.js启动节点服务器而不是mysql服务器。