我找不出爲什麼腳本無法連接到MySQL實例。爲什麼節點腳本無法通過本地主機連接到MySQL
我可以在命令行上使用完全相同的憑據從mysql客戶端連接到數據庫。 我可以使用相同的憑據從遠程客戶端連接到數據庫。
如果我將參數設置爲遠程數據庫,數據庫連接腳本可以正常工作。
主機或套接字參數不通過腳本工作。 我有時犯了愚蠢的錯誤,但如果這是另一個,我看不到它。
一位外部觀察員會幫助我。
==================== DB連接代碼
var db = require('./config');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: db.db_parms.host,
port: db.db_parms.port,
user: db.db_parms.user,
password: db.db_parms.password,
database: db.db_parms.context,
debug:true
});
connection.connect(function(err) {
if(err){
console.log("SQL CONNECT ERROR: " + err);
} else {
console.log("SQL CONNECT SUCCESSFUL.");
}
});
connection.query("SELECT now()", function(error,results,fields) {
console.log("Results:" + results);
console.log("Fields:" + fields);
});
exports.connection = connection;
===錯誤堆棧返回:
SQL CONNECT ERROR: Error: connect ECONNREFUSED 127.0.0.1:3307
at Object.exports._errnoException (util.js:1036:11)
at exports._exceptionWithHostPort (util.js:1059:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
== =======與主機config.js = 'localhost' 的:
var db_parms = {
host: 'localhost',
port: 3307,
user:'foo',
password:'bar',
database:'thisone'
}
exports.db_parms = db_parms;
與主機=插座========= config.js:
host: '/var/run/mysqld/mysqld.sock',
===錯誤堆棧返回:
SQL CONNECT ERROR: Error: getaddrinfo ENOTFOUND /var/run/mysqld/mysqld.sock /var/run/mysqld/mysqld.sock:3307
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
--------------------
at Protocol._enqueue (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/kingram/node/apps/fss_register/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/kingram/node/apps/fss_register/db/db_connect.js:19:12)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
我不知道,如果從插槽中的錯誤是權限問題或不和,如果這是相關的。
還檢查MySQL服務器是否真的啓動,使用'mysql'命令行util來測試。 – Populus
正如我在我的問題中指出的,除了通過腳本之外,我可以用任何方式連接到數據庫。 –