nodejs解决mysql和连接池(pool)自动断开问题

2017-07-14 13:53:02

  最近在做一个个人项目,数据库尝试使用了mongodb、sqlite和mysql。分享一下关于mysql的连接池用法。项目部署于appfog,项目中我使用连接池链接数据库,本地测试一切正常。上线以后,经过几次请求两个数据接口总是报503。一直不明就里,今天经过一番排查终于顺利解决了。
1.mysql 链接普通模式

[javascript] view plain copy

  1. var mysql = require('mysql'),  

  2.     env = {  

  3.       host : 'localhost',  

  4.       user : 'root',  

  5.       password : '2212',  

  6.       database : 'image_marker'  

  7.     };  

  8.   

  9.   db = mysql.createConnection(env);  

  10.   db.connect();  

  11.   

  12.   exports.do = function (sql, callback) {  

  13.   

  14.     db.query(sql, callback);  

  15.   

  16.   }  


MySQL中有一个名叫wait_timeout的变量,表示操作超时时间,当连接超过一定时间没有活动后,会自动关闭该连接,这个值默认为28800(即8小时)。对于这种普通连接的方式,在正式线上可能会遇到连接丢失的问题(No reconnection after connection lost错误日志),上github上看了下文档和issues,上面说到连接丢失后不会自动重新连接,会触发error事件。 所以可以使用下面这种方法来避免连接对视问题:


[javascript] view plain copy

  1. function handleError (err) {  

  2.   if (err) {  

  3.     // 如果是连接断开,自动重新连接  

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

  5.       connect();  

  6.     } else {  

  7.       console.error(err.stack || err);  

  8.     }  

  9.   }  

  10. }  

  11.   

  12. // 连接数据库  

  13. function connect () {  

  14.   db = mysql.createConnection(config);  

  15.   db.connect(handleError);  

  16.   db.on('error', handleError);  

  17. }  

  18.   

  19. var db;  

  20. connect();  


 
2.使用连接池
 
对于丢失连接的问题,可以使用连接池(最新版mysql模块,用mysql.createPool()来创建的pool,当触发了connection的error事件时,会把该connection对象从连接池中移除。)

[javascript] view plain copy

  1. var mysql = require('mysql');  

  2. var pool  = mysql.createPool(config);  

  3.   

  4. pool.getConnection(function(err, connection) {  

  5.   // Use the connection  

  6.   connection.query( 'SELECT something FROM sometable'function(err, rows) {  

  7.     // And done with the connection.  

  8.     connection.end();  

  9.   

  10.     // Don't use the connection here, it has been returned to the pool.  

  11.   });  

  12. });  


  • 2017-02-09 09:02:26

    两列布局——左侧宽度固定,右侧宽度自适应的两种方法

     关于左侧宽度固定,右侧宽度自适应两列布局的一种很常用的方法我相信大家都知道。就是利用左侧元素浮动,或者绝对定位的方式使其脱离常规文档流,让两个块级元素能够在同一行显示。然后右侧元素 margin-left 的值等于左侧元素宽度,这时右侧元素将紧挨着左侧元素

  • 2017-02-10 15:19:51

    Git:代码冲突常见解决方法

    如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突:

  • 2017-02-10 15:24:14

    linux学习之——vim简明教程

    学习 vim 并且其会成为你最后一个使用的文本编辑器。没有比这个更好的文本编辑器了,非常地难学,但是却不可思议地好用。 我建议下面这四个步骤: 存活 感觉良好 觉得更好,更强,更快 使用VIM的超能力

  • 2017-02-10 16:22:13

    git历史记录查询

    查看提交历史:git log 查看提交历史并显示版本间的差异:git log -p 查看指定历史:git log xxx(sha1值) -p 查看提交历史(指定时间):

  • 2017-02-13 17:50:05

    cURL error 60: SSL certificate problem: unable to get local issuer certificate

    Drupal 8 version uses Guzzle Http Client internally, but under the hood it may use cURL or PHP internals. If you installed PHP cURL on your PHP server it typically uses cURL and you may see an exception with error Peer certificate cannot be authenticated with known CA certificates or error code CURLE_SSL_CACERT (60).