How to use the createPool function from mysql2
Find comprehensive JavaScript mysql2.createPool code examples handpicked from public code repositorys.
mysql2.createPool is a function that creates a connection pool for a MySQL database, allowing multiple connections to be managed and reused efficiently.
458 459 460 461 462 463 464 465 466 467
queryablePromise.end() queryablePromise = undefined } } queryable = mysql.createPool(connectionOptions) queryablePromise = mysqlPromise.createPool(connectionOptions) cb() })
+ 7 other calls in file
73 74 75 76 77 78 79 80 81 82
this.uniqueKeyErrorCode = 'ER_DUP_ENTRY'; this.uniqueKeyErrorRegExp = /for key \'(.+)\'/; if (config) { this.config = toolkit.noNullOrWhiteSpace(config); this.client = mysql.createPool(getConfig(this.config)); } else { if (!CLIENT) { CLIENT_CONFIG = toolkit.noNullOrWhiteSpace({
+ 11 other calls in file
How does mysql2.createPool work?
mysql2.createPool
works by creating a connection pool to a MySQL database, which can be used to handle multiple client connections to the database in an efficient manner.
When you call mysql2.createPool
, you pass in an object that contains connection configuration options, such as the host, user, password, and database name.
The createPool
method returns a pool object that has methods for querying the database and managing connections.
When you need to execute a query, you call a method on the pool object such as pool.query
, pool.execute
, or pool.getConnection
, passing in the SQL statement and any parameters.
The pool manages the connections, distributing queries among available connections, and creating new connections as needed.
When a connection is no longer needed, it is returned to the pool for reuse.
The pool can be configured with various options such as the maximum number of connections to be created, the maximum idle time for a connection, and the timeout for acquiring a connection.
Using mysql2.createPool
can help improve the performance of your application by enabling efficient management and reuse of connections to a MySQL database.
115 116 117 118 119 120 121 122 123 124
if (dbconfig.ws != undefined) { this.EXTERNAL_WS_PROVIDER_KEY = dbconfig.ws.key; this.EXTERNAL_WS_PROVIDER_URL = dbconfig.ws.url; } this.pool = mysql.createPool(this.convert_dbconfig(dbconfig.client)); // Ping WRITABLE database to check for common exception errors. this.pool.getConnection((err, connection) => { if (err) { if (err.code === 'PROTOCOL_CONNECTION_LOST') {
+ 9 other calls in file
GitHub: Taehoya/Adventure-Audit
2 3 4 5 6 7 8 9 10 11 12
const dbConfig = async () => { const result = await configure(); let pool; if (global.poolObject === undefined) { pool = mysql.createPool({ host: result.dbHost, password: result.dbPassword, user: result.dbUsername, database: result.dbName,
+ 4 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const mysql = require("mysql2"); const pool = mysql.createPool({ host: "localhost", user: "myuser", password: "mypassword", database: "mydatabase", connectionLimit: 10, }); pool.query("SELECT * FROM mytable", function (error, results, fields) { if (error) throw error; console.log("The result is: ", results); });
In this example, we first import the mysql2 module. We then create a connection pool by calling mysql.createPool and passing in an object containing the connection configuration options. In this example, we specify the host, user, password, and database options, and also set the connectionLimit option to 10 to specify the maximum number of connections to be created in the pool. We then execute a SQL query on the pool by calling pool.query and passing in the SQL statement as a string. The callback function is executed when the query completes, and receives the error (if any), the results of the query, and the fields object. In this example, we simply log the results to the console. By using mysql2.createPool, we can efficiently manage connections to a MySQL database, reuse connections, and improve the performance of our application.
28 29 30 31 32 33 34 35 36 37
this.createPool(); } createPool() { // Create the pool this.pool = mysql.createPool(this.dbConfig); // Now get a Promise wrapped instance of that pool this.promisePool = this.pool.promise(); }
18 19 20 21 22 23 24 25 26 27 28
const { config } = require('./src/_config') const port = config.port const KEY_API = config.secretKey const API_PREFIX = config.prefix const connectionMain = mysql.createPool(config.db_primary) const connectionHos = mysql.createPool(config.db_hos) connectionMain.getConnection(function (err, conn) { if (err) { console.log(`Error connention >>> ${err}`)
+ 7 other calls in file
1 2 3 4 5 6 7 8 9 10 11
class MiniOrm { constructor() { } getConnectionPool() { if (global.db == undefined) { global.db = mysql2.createPool({ host: 'localhost', database: 'admin', user: 'root', password: 'root',
+ 4 other calls in file
GitHub: sabumi2002/Bit
19 20 21 22 23 24 25 26 27 28
user: 'root', password: '1111', database: 'scottDB', connectionLimit: 10 }; let pool = mysql.createPool(conn); // pool.getConnection((err, conn)=>{ // let sql = "SELECT * FROM MEMBERS"; // conn.query(sql, (err, result, fileds)=>{ // if(!err){
+ 4 other calls in file
36 37 38 39 40 41 42 43 44 45 46 47 48 49
if (is_heroku) { database = mysql.createPool(dbConfigHeroku).promise(); } else { database = mysql.createPool(dbConfigLocal).promise(); } /***** Functions *****/
+ 9 other calls in file
52 53 54 55 56 57 58 59 60 61 62
} // Function that starts the database connection depending on the environment and host function startDatabaseConnection(db_host) { if (environment == "DOCKER") { con = mysql.createPool({ connectionLimit : 90, connectTimeout: 1000000, host: db_host, user: secretConfig.DB_USER,
+ 2 other calls in file
22 23 24 25 26 27 28 29 30 31 32 33
idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000 queueLimit: 0 } console.log(config) const pool = mysql.createPool(config); // pool.query("SHOW DATABASES", function(err, rows, fields) { // if (err) return console.error(err); // console.log(rows);
+ 2 other calls in file
GitHub: mbobic1/sports_event
12 13 14 15 16 17 18 19 20 21 22
credentials: true })); app.use(express.json()); app.use(bodyParser.json()); const db = mysql.createPool({ host: '127.0.0.1', user: 'root', password: 'uma6bobic', database: 'kosarka'
+ 14 other calls in file
545 546 547 548 549 550 551 552 553 554
const { createMigrationTable } = require(path.resolve( projectDir, 'node_modules/@evershop/evershop/bin/install/createMigrationTable' )); const pool = mysql.createPool({ host: 'db.cloud.evershop.io', port: 3306, user: db.databaseUser, database: db.databaseName,
+ 13 other calls in file
GitHub: Selenium39/nodejs-github
13 14 15 16 17 18 19 20 21 22
* @param {string} option.database 数据库名称 * @param {boolean} [option.logging=false] 是否输出SQL */ constructor(option, logger) { this.logger = logger; this.pool = mysql.createPool({ host: option.host, port: option.port, user: option.user, charset: option.charset,
GitHub: MaXLab-OVGU/MTV
80 81 82 83 84 85 86 87 88 89 90 91 92
var meetingDetails = {}; // console.log("App listening on port " + config.PORT); logger.info("App listening on port " + config.PORT); var connection = mysql.createPool(config.DB_CONNECTION); /* Session API */ app.get("/meeting/:session", (req, res) => { if (req.params.session != "style.css") {
+ 15 other calls in file
GitHub: LuisCasta/botHM
3 4 5 6 7 8 9 10 11 12 13 14 15
async function getConnectionLocal(){ if(poolLocal == undefined || poolLocal == null){ poolLocal = mysql.createPool({ host: process.env.DB_HOST, user: process.env.DB_USER, password : process.env.DB_PW, database: process.env.DB_NAME,
11 12 13 14 15 16 17 18 19 20 21
// NEW const mysql2 = require('mysql2'); const nodemon = require('nodemon'); // Create the connection pool. The pool-specific settings are the defaults const pool = mysql2.createPool({ host: '212.227.30.12', user: 'expressdb', password: 'Exp20pr23ess!', database: 'express',
+ 2 other calls in file
mysql2.createConnection is the most popular function in mysql2 (458 examples)