Open webstorm new project and select express to create an express project.
After the creation is successful, the page will be as follows:
In order to connect to the mysql database, you need to import the mysql module.
Create a config file in the project, create a congfigdb.js file in the config file to connect to the database, and write in the file :
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
After completion, add in the app.js file:
1 2 |
|
Test in Postman as follows:
The database connection pool establishes a sufficient number of database connection objects when the program starts, and connects these connection objects A pool is formed, and the program dynamically applies for, uses, and releases connection objects in the pool.
The database connection pool is responsible for allocating, managing and releasing database connection objects. It allows an application to reuse an existing database connection object. rather than recreating one.
1 |
|
Create a connection pool
1 |
|
options parameter is an object that contains many attribute configurations. The object is used to specify various options that are used uniformly by connections in the connection pool.
1 2 3 4 5 6 7 8 9 10 11 |
|
connectionLimit
: used to specify the maximum number of links in the connection pool, the default attribute value is 10.queueLimit
: used to specify the allowed The maximum number of pending connections. If the number of pending connections exceeds this value, an error will be thrown immediately. The default attribute value is 0. This represents the maximum number of pending connections that are not allowed.multipleStatements
: Whether to allow the execution of multiple sql statements, the default value is falsehost
: The address of the database serveruser
: Connection Database usernamepassword
: Password to connect to the databasedatabase
: Database name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
Result test:
The above is the detailed content of Example analysis of Express connecting to MySQL and database connection pool. For more information, please follow other related articles on the PHP Chinese website!