解决node-mysql初始化错误?
我试图使用它可以发现here节点-mysql的模块。我以前用过这个版本没有任何问题,但是最近的一个项目出现了一个奇怪的问题,它永远无法连接到数据库。我知道我的登录凭据是正确的,因为他们通过正常的MySQL命令行工作。解决node-mysql初始化错误?
然而,当node-mysql
模块试图连接我总是会收到错误。这是迄今为止的代码,启用了debugging
。
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
database : 'Coachletic',
pool : { maxConnections: 50, maxIdleTime: 30},
debug : true
});
connection.connect(function(err){
if(!err) {
console.log("MySQL Database is connected.");
} else {
console.log(err);
console.log("Error connecting to MySQL Database.");
}
});
global.connection = connection;
这是从控制台输出:
<-- HandshakeInitializationPacket
HandshakeInitializationPacket {
protocolVersion: 10,
serverVersion: '5.6.19-0ubuntu0.14.04.1',
threadId: 64,
scrambleBuff1: <Buffer 65 3f 48 2e 4d 78 38 49>,
filler1: <Buffer 00>,
serverCapabilities1: 63487,
serverLanguage: 8,
serverStatus: 2,
serverCapabilities2: 32895,
scrambleLength: 21,
filler2: <Buffer 00 00 00 00 00 00 00 00 00 00>,
scrambleBuff2: <Buffer 47 68 54 4e 6b 4e 25 6a 6a 54 46 42>,
filler3: <Buffer 00>,
pluginData: 'mysql_native_password',
protocol41: true }
--> ClientAuthenticationPacket
ClientAuthenticationPacket {
clientFlags: 455631,
maxPacketSize: 0,
charsetNumber: 33,
filler: undefined,
user: 'root',
scrambleBuff: <Buffer 40 de 4c 94 3b c4 6f 15 0d 1c 49 12 04 46 af 64 d9 4d 41 76>,
database: 'Coachletic',
protocol41: true }
{ [Error: Handshake inactivity timeout]
code: 'PROTOCOL_SEQUENCE_TIMEOUT',
fatal: true,
timeout: undefined }
Error connecting to MySQL Database.
试图寻找此错误的解决方案,但没有拿出任何有用的东西。
做得更多一些挖后看来这是与节点4.2.0关联的问题。这是参考线:https://github.com/felixge/node-mysql/issues/1236
的修复参与lib/protocol/sequences/Sequence.js
文件从undefined
改变this._idleTimeout
到-1
。
这是为我工作的代码:
初始化
var mysql = require('mysql');
var pool = mysql.createPool({
host: 'XXX.XXX.XXX.XX',
user: 'xxxxx_xxx',
password: 'xxxxxxxxx',
database: 'xxxxxxx'
});
使用
pool.getConnection(function (err, connection) {
connection.query('MY SQL QUERY', function (err, result) {
connection.release();
});
让我知道它是否适合你。
感谢您的回复,并且此代码有效。我最终发现了Node.js本身的问题。 – Actively
的问题是与节点的MySQL节点4.2.0。
要提一个快速简单的修复,为那些谁拥有这个问题,谁不关心节点版本,并希望速战速决,只要迫使前一个节点的版本(4.1.2)为您的机器这么件事才能赢得不会中断。对于用户的Heroku,例如,这可以通过向作为的package.json作为主要JSON对象的一部分来实现的: “引擎”:{ “节点”:“4.1.2” }
我不真的不知道发生了什么,但你有没有试过泳池?如果你想我可以复制我用于我的MySQL连接的代码使用node-mysql模块。 –
使用池似乎正常连接,但在试图查询时,我收到了类似的错误:'{[错误:查询闲置超时] 代码:“PROTOCOL_SEQUENCE_TIMEOUT”, 致命的:真实, 超时:未定义} 不确定 ' – Actively