node-mysql池遇到ETIMEDOUT [英] node-mysql pool experiences ETIMEDOUT
问题描述
我有一个节点-mysql池配置
I have a node-mysql pool configuration of
var db_init={
host : 'ip_address_of_GCS_SQL',
user : 'user_name_of_GCS_SQL',
password : 'password here',
database : 'db here',
supportBigNumbers: true,
connectionLimit:100
};
使用以下方法创建池
GLOBAL.db_foobar = mysql.createPool(db_init);
我基本上只是将连接保持了几个小时,并且看到了我的connection.query请求(当然是在getConnection之后)报告的错误:
I basically just left the connection on for a couple of hours and I saw this error reported by my connection.query Request (after getConnection of course):
prodAPI-104 (out): { status: 'Error',
prodAPI-104 (out): details: '[foobar_function]Error in query',
prodAPI-104 (out): err: '{ [Error: read ETIMEDOUT]\n code: \'ETIMEDOUT\',\n errno: \'ETIMEDOUT\',\n syscall: \'read\',\n fatal: true }',
prodAPI-104 (out): query: 'SELECT * FROM `foobar_table`;' }
为什么会这样? Google-Cloud-SQL中的MySQL并没有报告创建查询所花费的时间太长,所以我不知道为什么会这样.
Why is this happening? The MySQL in Google-Cloud-SQL didn't report a query taking too long to create so I dunno why this happened.
推荐答案
我怀疑原因是在与MySQL服务器的连接上未启用keepalive.
I suspect the reason is that keepalive is not enabled on the connection to the MySQL server.
node-mysql
没有启用keepalive的选项,node-mysql2
也没有,但是node-mysql2
提供了一种提供自定义函数的方法来创建套接字,我们可以使用它来启用keepalive:
node-mysql
does not have an option to enable keepalive and neither does node-mysql2
, but node-mysql2
provides a way to supply a custom function for creating sockets which we can use to enable keepalive:
var mysql = require('mysql2');
var net = require('net');
var pool = mysql.createPool({
connectionLimit : 100,
host : '123.123.123.123',
user : 'foo',
password : 'bar',
database : 'baz',
stream : function(opts) {
var socket = net.connect(opts.config.port, opts.config.host);
socket.setKeepAlive(true);
return socket;
}
});
这篇关于node-mysql池遇到ETIMEDOUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!