跳到主要内容

createPool

信息

For queries please see the Simple Queries and Prepared Statements examples.

createPool(connectionUri)

createPool(connectionUri: string)

import mysql from 'mysql2/promise';

try {
const pool = mysql.createPool('mysql://root:password@localhost:3306/test');
const connection = await pool.getConnection();
// ... some query

connection.release();

// Close the pool
await pool.end();
} catch (err) {
console.log(err);
}
注意

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

createPool(config)

createPool(config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
const connection = await pool.getConnection();
// ... some query

connection.release();

// Close the pool
await pool.end();
} catch (err) {
console.log(err);
}
注意

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

createPool(config) — SHA1

createPool(config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const pool = mysql.createPool({
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
const connection = await pool.getConnection();
// ... some query

connection.release();

// Close the pool
await pool.end();
} catch (err) {
console.log(err);
}
注意

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

createPool(config) — SSL

createPool(config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const pool = mysql.createPool({
// ...
ssl: {
// key: fs.readFileSync('./certs/client-key.pem'),
// cert: fs.readFileSync('./certs/client-cert.pem')
ca: fs.readFileSync('./certs/ca-cert.pem'),
},
});
const connection = await pool.getConnection();
// ... some query

connection.release();

// Close the pool
await pool.end();
} catch (err) {
console.log(err);
}
注意

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()

createPool(config) — RDS SSL

createPool(config: PoolOptions)

You can use Amazon RDS string as value to ssl property to connect to Amazon RDS MySQL over SSL.

In that case https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA cert is used:

npm install --save aws-ssl-profiles
import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';

try {
const pool = mysql.createPool({
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
const connection = await pool.getConnection();
// ... some query

connection.release();

// Close the pool
await pool.end();
} catch (err) {
console.log(err);
}
信息

For detailed instructions, please follow the AWS SSL Profiles documentation.

注意

Don't forget to release the connection when finished by using:

  • pool.releaseConnection(connection)
  • connection.release()
Testing
try {
const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
await pool.end();

console.log(res);
} catch (err) {
console.log(err);
}

createPool(config) — Socks

createPool(config: PoolOptions)

const mysql = require('mysql2');
const SocksConnection = require('socksjs');

const socksProxy = new SocksConnection({ port: 3306 });
const pool = mysql.createPool({
stream: socksProxy,
});

// Close the pool
pool.end();
Testing
pool.execute('SELECT SLEEP(1.1) AS `www`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

pool.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

pool.execute('SELECT SLEEP(1) AS `qqq`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

console.log(rows, fields);
});

Escaping in a Pool

pool.escape(value) · pool.escapeId(identifier) · pool.format(sql, values)

A Pool exposes the same escaping helpers as a single Connection, so there is no need to acquire a connection just to escape a value or an identifier.

import mysql from 'mysql2';

const pool = mysql.createPool('mysql://root:password@localhost:3306/test');

pool.escape(1); // 1
pool.escapeId('users'); // `users`
pool.format('SELECT * FROM ?? WHERE `id` = ?', ['users', 1]);

Promise Wrapper in a Pool

connection.promise()

Calling .promise() on a pooled connection returns a promise-based pooled connection, not a promise-based Pool. It keeps release(), so the connection still returns to the pool when you are done with it.

import mysql from 'mysql2';

const pool = mysql.createPool('mysql://root:password@localhost:3306/test');

pool.getConnection(async (err, connection) => {
if (err instanceof Error) {
console.log(err);
return;
}

const promiseConnection = connection.promise();
const [rows, fields] = await promiseConnection.query('SELECT 1');

console.log(rows, fields);
promiseConnection.release();
});

Glossary

PoolOptions

PoolOptions extends all options from ConnectionOptions:

ConnectionOptions Specification
PoolOptions Specification