node请求mysql需要联网吗,Node.js MySQL需要持久连接

I need a persistent MySQL connection for my Node web app. The problem is that this happens about a few times a day:

Error: Connection lost: The server closed the connection.

at Protocol.end (/var/www/n/node_modules/mysql/lib/protocol/Protocol.js:73:13)

at Socket.onend (stream.js:79:10)

at Socket.EventEmitter.emit (events.js:117:20)

at _stream_readable.js:895:16

at process._tickCallback (node.js:415:13)

error: Forever detected script exited with code: 8

error: Forever restarting script for 2 time

info: socket.io started

Here is my connection code:

// Yes I know multipleStatements can be dangerous in the wrong hands.

var sql = mysql.createConnection({

host: 'localhost',

user: 'my_username',

password: 'my_password',

database: 'my_database',

multipleStatements: true

});

sql.connect();

function handleDisconnect(connection) {

connection.on('error', function(err) {

if (!err.fatal) {

return;

}

if (err.code !== 'PROTOCOL_CONNECTION_LOST') {

throw err;

}

console.log('Re-connecting lost connection: ' + err.stack);

sql = mysql.createConnection(connection.config);

handleDisconnect(sql);

sql.connect();

});

}

handleDisconnect(sql);

As you can see, the handleDisconnect code does not work..

解决方案

Use the mysql connection pool. It will reconnect when a connection dies and you get the added benefit of being able to make multiple sql queries at the same time. If you don't use the database pool, your app will block database requests while waiting for currently running database requests to finish.

I usually define a database module where I keep my queries separate from my routes. It looks something like this...

var mysql = require('mysql');

var pool = mysql.createPool({

host : 'example.org',

user : 'bob',

password : 'secret'

});

exports.getUsers = function(callback) {

pool.getConnection(function(err, connection) {

if(err) {

console.log(err);

callback(true);

return;

}

var sql = "SELECT id,name FROM users";

connection.query(sql, [], function(err, results) {

connection.release(); // always put connection back in pool after last query

if(err) {

console.log(err);

callback(true);

return;

}

callback(false, results);

});

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值