node. JS uses MySQL connection pool

Source: Internet
Author: User
Tags connection pooling mysql connection pool mysql in php and mysql

Using Nodejs+mysql is certainly better for server-side development than a combination of PHP and MySQL.

Using Nodejs you will benefit from his asynchronous behavior. For example, to improve performance, you don't have to get a performance boost from migrating from an existing MySQL database to another NoSQL database.

Nodejs How to use MySQL

Nodejs to connect to MySQL, you can use the Nodejs MySQL driver. For example, we use "node-mysql" to connect to the database. We use the following method to connect to the database:

First, we need to install the MySQL driver using Nodejs's package management tool (NPM). The command line is as follows:

NPM Install Musql


Now, to use MySQL in the JS file, add the following code to your file:

var mysql =  require (' MySQL ');


Next, we can use this module to connect to the MySQL database. Of course to connect to the database you need to specify the MySQL server's host name, user name and password, and so on. There are many other options that can be set, such as the time zone of the database, Socketpath, and local addresses.

   var connection =  Mysql.createconnection ({  host: "HostName",  User: "username",  password: "Password"   });


Then, the following code will create a new connection for you.

Connection.connect ();


Using this connection object, we can query the database as follows. We can use the Connection.escape () method to prevent SQL injection.

Connection.query ("Use Database1");  var strquery = "Select * FROM table1";    Connection.query (strquery, function (err, rows) {  if (err) {  throw err;  } else{  console.log (rows);  }  });

Finally, we can close the connection in two ways. With Connection.end or Connection.destroy.

The following expression ensures that queries in all queues will be executed before the database connection is closed. Please note that there is a callback function here.

Connection.end (ERR) {//do something after the connection is gracefully terminated.});

The following expression immediately closes the database connection. And there is no callback function or trigger any events.

Connection.destroy ();



Nodejs using MySQL's connection pool

Using connection pooling can help us better manage database connections. The database connection pool can limit the maximum number of connections, reuse existing connections, and so on.

First, we need to create a connection pool:

var mysql =  require (' MySQL ');                    var pool =  Mysql.createpool ({host: "HostName", User: "username", Password: "Password"  });


Second, we can get a connection we need from the connection pool we created:

Pool.getconnection (function (err, connection) {
});


Use the parameter connection of the callback function to query the database. Finally, the database connection is freed using the Connection.realease () method.

Pool.getconnection (function (err, connection) {  connection.query ("SELECT * FROM table1",  function (err, rows) {  if (err) {  throw err;  } else{  console.log (rows);  }  });    Connection.release ();});



Execute multiple query statements

For security reasons, it is not allowed to execute multiple query statements by default. To use the functionality of multiple query statements, you need to open this feature when you create a database connection:

var connection =  mysql.createconnection ({multiplestatements:true});

Once this feature is open, you can use multiple query statements in the same way as in the following example:

Connection.query (' select Column1; select Column2; select Column3; ', function (err, result) {  if (err) {  throw err;  }else{  Console.log (result[0]);       Column1 as a result  Console.log (result[1]);       Column2 as a result  Console.log (result[2]);       Column3 as a result  }});








node. JS uses MySQL connection pool

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.