我有一個nodejs(帶有express)openshift應用程序,它有一個MySQL數據庫(因爲這是我最熟悉的),我無法弄清楚如何連接到它在當地的環境。我的最後一個項目是我用一個php框架與mamp連接到本地數據庫。什麼是節點的等價物?這是我必須連接的代碼:無法連接到具有nodejs的MySQL數據庫(openshift應用程序)
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '127.0.0.1',
port : '3307',
database : 'test'
});
Error: connect ETIMEDOUT
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
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)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
我猜想那是因爲我想在本地連接到MySQL數據庫設立openshift,那我收到的錯誤:
connection.connect(function(err){
if (err){
throw err;
}
else {
console.log('Connected');
}
});
這裏嘗試在本地連接的時候是錯誤。我只是不知道還有什麼要嘗試在這裏。我已經嘗試過,沒有密碼。
如果我切換到本地主機的主機,這是我收到的錯誤:
Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database 'stembuds'
at Handshake.Sequence._packetToError (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Handshake.js:91:18)
at Protocol._parsePacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:202:24)
at Parser.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:72:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
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)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
對不起,這篇文章的長度,我只是不知道爲什麼它不是工作。
編輯:
我ssh'ed到openshift外殼看mysql的設置。當我執行'show grants \ g'命令時,這是我收到的內容:
Grants for [email protected]
GRANT ALL PRIVILEGES ON *.* TO 'adminMYXaSuf'@'127.11.28.130' IDENTIFIED BY PASSWORD '*D0CE5FA2AAD801A33C9190D0B615CC92F364BE6B' WITH GRANT OPTION
此密碼不同於openshift提供的密碼。
您確定連接對象是否正確?從npm包看'pass'應該是'密碼'。還要確保你有正確的端口號,默認爲3306.也許你應該有一個'debug:true',然後告訴我們輸出的結果。 – HexCoder