1. Create a connection pool:
Why was it created? Because the database is expensive to create connections to the server each time, if a lot of people want to connect to the database, each user creates a connection and the performance is greatly degraded, so create a connection pool. In a server application, you typically create and maintain a connection pool for multiple databases. When the connection is no longer needed, these connections can be slowed down to the connection pool, and when the next client request is received, the connection is removed from the connection pool and re-utilized without establishing a new connection.
1.1 Syntax: Create CreatePool method:
var Pool=mysql.createpool ({
1 {2 ' localhost ' , 3 User ' Root ' , 4 ' 123456 ' , 5 Database ' TestDB ' 6 }
});
1.2 Remove the connection from the connection pool. Getconnection method.
1 pool.getconnection (function (err, connection) {2 function ( Err, rows) {3 callback (err, rows); 4 Connection.release (); // Release link 5 }); 6 });
1.3 When the connection is not in use, it is returned to the connection pool with the release method of the Connection object. connection.release ();//Release link
1.4 Remove a connection from the connection pool, using the destroy of the connection object. Connection.destroy ();
1.5 When a connection pool is no longer in use, close the connection pool with the end method of the connection pool object. pool.end ();
Content reference: http://blog.csdn.net/qq_31280709/article/details/52297957
Nodejs from 0 to 1 (MySQL)