Skip to main content

createPoolCluster

info

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

add(group, connectionUri)โ€‹

add(group: string, connectionUri: string)

import mysql from 'mysql2/promise';

try {
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');

const connection = await poolCluster.getConnection('clusterA');
// ... some query

connection.release();
} catch (err) {
console.log(err);
}
warning

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

  • connection.release()

add(group, config)โ€‹

add(group: string, config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// poolCluster.add('clusterB', '...');

const connection = await poolCluster.getConnection('clusterA');
// ... some query

connection.release();
} catch (err) {
console.log(err);
}
warning

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

  • connection.release()

add(group, config) โ€” SHA1โ€‹

add(group: string, config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// poolCluster.add('clusterB', '...');

const connection = await poolCluster.getConnection('clusterA');
// ... some query

connection.release();
} catch (err) {
console.log(err);
}
warning

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

  • connection.release()

add(group, config) โ€” SSLโ€‹

add(group: string, config: PoolOptions)

import mysql from 'mysql2/promise';

try {
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
// ...
ssl: {
// key: fs.readFileSync('./certs/client-key.pem'),
// cert: fs.readFileSync('./certs/client-cert.pem')
ca: fs.readFileSync('./certs/ca-cert.pem'),
},
});
// poolCluster.add('clusterB', '...');

const connection = await poolCluster.getConnection('clusterA');
// ... some query

connection.release();
} catch (err) {
console.log(err);
}
warning

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

  • connection.release()

add(group, config) โ€” RDS SSLโ€‹

add(group: string, 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 poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');

const connection = await poolCluster.getConnection('clusterA');
// ... some query

connection.release();
} catch (err) {
console.log(err);
}
info

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

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

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

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

  • connection.release()

add(group, config) โ€” Socksโ€‹

add(group: string, config: PoolOptions)

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

const socksProxy = new SocksConnection({ port: 3306 });
const poolCluster = mysql.createPoolCluster();

poolCluster.add('clusterA', {
stream: socksProxy,
});
// poolCluster.add('clusterB', '...');

const poolNamespace = poolCluster.of('clusterA');
Testing
poolNamespace.execute('SELECT SLEEP(1.1) AS `www`', (err, rows, fields) => {
if (err instanceof Error) {
console.log(err);
return;
}

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

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

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

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

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

Glossaryโ€‹

PoolOptionsโ€‹

PoolOptions extends all options from ConnectionOptions:

ConnectionOptions Specification
PoolOptions Specification