我是Node.js的新手,但我确实对mysql和其他一些语言有一些经验。
我正在尝试在Node.js和MySQL之间建立数据库连接,但是,我的终端一直在给我错误。它失败了,所以我尝试使用文档中所述的“error.stack”进行调试,但也失败了,所以我现在完全迷失了。有人可以帮我弄清楚这个错误。
此外,任何人都可以告诉我如何告诉Node.js打印出无法连接到数据库的原因,如PHP中的函数“mysql_error()”。
我原来的Node.js代码:
var MySQL = require('mysql');
var DatabaseConnection = MySQL.createConnection({
host: '10.0.0.25',
user: 'Admin',
password: 'Password'
});
DatabaseConnection.connect(function(error) {
if(!error) {
console.log("Database is connected");
return;
} else {
console.log("Database is not connected: ");
console.log(error.stack);
}
});
我的结果:
Database is not connected:
Error: connect ECONNREFUSED 10.0.0.25:3306
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
--------------------
at Protocol._enqueue (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/Users/marc/Documents/JSON_MySQL_Database_Test/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/marc/Documents/JSON_MySQL_Database_Test/Database_Test.js:8:20)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)