Use of MySQL connection pool in node. js

Source: Internet
Author: User
Tags mysql connection pool install node

https://cnodejs.org/topic/58378543bde2b59e06141f5a

The first practice is to create a connection and then use, and later found that a period of time will appear after the card is not responding to the situation, only restart the Nodejs service, after searching for a connection pool, after testing, no longer the card is not responding to the situation.

Reference Link: https://www.npmjs.com/package/mysql http://blog.csdn.net/lovingshu/article/details/41721233

node. js MySQL Connection pool module

1, install node's MySQL module npm-install-g node-mysql
2, create a class library, called Mysql_pool.js Bar, then the content is as follows:

VarMysql=Require("MySQL"); VarPool=Mysql.CreatePool({Host: ' localhost ',User: ' User ',Password: ' Password ',Database: ' Database ',Port:Port}); VarQuery=function(Sql,Options,Callback){Pool.Getconnection(function(Err,Conn){ If(Err){Callback(Err,Null,Null); }Else{Conn.Query(Sql,Options,function(Err,results,fields) { Span class= "com" >//release connection  Conn. Release//event-driven callback  Callback (err , results,fields Span class= "pun" >});  } });  };  module.=query        

3, use the following in JS class

var query=require("./lib/mysql_pool"); query("select * from table where id=?", [1], function(err,results,fields){ //do something });

Use of MySQL connection pool in node. js

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.