website/docs/examples/connections/createPoolCluster.mdx
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import { FAQ } from '@site/src/components/FAQ'; import { ExternalCodeEmbed } from '@site/src/components/ExternalCodeEmbed';
:::info For queries please see the Simple Queries and Prepared Statements examples. :::
<Tabs> <TabItem value='promise.js' default>add(group: string, connectionUri: string)
import mysql from 'mysql2/promise';
try {
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');
const connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
await poolCluster.end();
} catch (err) {
console.log(err);
}
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='callback.js'>const mysql = require('mysql2');
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
poolCluster.end();
});
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='await using'>import mysql from 'mysql2/promise';
{
// highlight-start
// .end() is called automatically when leaving the scope
await using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');
// .release() is called automatically when leaving the scope
await using connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
const mysql = require('mysql2');
{
// highlight-start
// .end() is called automatically when leaving the scope
using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, _connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// highlight-start
// .release() is called automatically when leaving the scope
using connection = _connection;
// highlight-end
// ... some query
});
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
<Tabs> <TabItem value='promise.js' default>add(group: string, config: PoolOptions)
import mysql from 'mysql2/promise';
try {
// highlight-start
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');
// highlight-end
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
await poolCluster.end();
} catch (err) {
console.log(err);
}
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='callback.js'>const mysql = require('mysql2');
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
poolCluster.end();
});
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='await using'>import mysql from 'mysql2/promise';
{
// highlight-start
// .end() is called automatically when leaving the scope
await using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// poolCluster.add('clusterB', '...');
// .release() is called automatically when leaving the scope
await using connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
const mysql = require('mysql2');
{
// highlight-start
// .end() is called automatically when leaving the scope
using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, _connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// highlight-start
// .release() is called automatically when leaving the scope
using connection = _connection;
// highlight-end
// ... some query
});
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
<Tabs> <TabItem value='promise.js' default>add(group: string, config: PoolOptions)
import mysql from 'mysql2/promise';
try {
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// poolCluster.add('clusterB', '...');
const connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
await poolCluster.end();
} catch (err) {
console.log(err);
}
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='callback.js'>const mysql = require('mysql2');
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex'),
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
poolCluster.end();
});
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='await using'>import mysql from 'mysql2/promise';
{
// highlight-start
// .end() is called automatically when leaving the scope
await using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// poolCluster.add('clusterB', '...');
// .release() is called automatically when leaving the scope
await using connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
const mysql = require('mysql2');
{
// highlight-start
// .end() is called automatically when leaving the scope
using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from(
'8bb6118f8fd6935ad0876a3be34a717d32708ffd',
'hex'
),
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, _connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// highlight-start
// .release() is called automatically when leaving the scope
using connection = _connection;
// highlight-end
// ... some query
});
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
<Tabs> <TabItem value='promise.js' default>add(group: string, config: PoolOptions)
import mysql from 'mysql2/promise';
try {
// highlight-start
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');
// highlight-end
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
await poolCluster.end();
} catch (err) {
console.log(err);
}
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='callback.js'>const mysql = require('mysql2');
// highlight-start
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', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
poolCluster.end();
});
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
</TabItem> <TabItem value='await using'>import mysql from 'mysql2/promise';
{
// highlight-start
// .end() is called automatically when leaving the scope
await using 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', '...');
// .release() is called automatically when leaving the scope
await using connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
const mysql = require('mysql2');
{
// highlight-start
// .end() is called automatically when leaving the scope
using 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', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, _connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// highlight-start
// .release() is called automatically when leaving the scope
using connection = _connection;
// highlight-end
// ... some query
});
}
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
- See [ssl/certs](https://github.com/sidorares/node-mysql2/tree/master/test/fixtures/ssl/certs).
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 {
// highlight-start
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');
// highlight-end
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
await poolCluster.end();
} catch (err) {
console.log(err);
}
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
:::info For detailed instructions, please follow the AWS SSL Profiles documentation. :::
:::tip Testing
try {
const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
await poolCluster.end();
console.log(res);
} catch (err) {
console.log(err);
}
:::
</TabItem> <TabItem value='callback.js'>const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
// highlight-next-line
connection.release();
// Close the pool cluster
poolCluster.end();
});
:::warning
Don't forget to release the connection when finished by using:
connection.release():::
:::info For detailed instructions, please follow the AWS SSL Profiles documentation. :::
:::tip Testing
connectionquery('SHOW `status` LIKE "Ssl_cipher"', function (err, res) {
poolCluster.end();
if (err instanceof Error) {
console.log(err);
return;
}
console.log(res);
});
:::
</TabItem> <TabItem value='await using'>import mysql from 'mysql2/promise';
import awsCaBundle from 'aws-ssl-profiles';
{
// highlight-start
// .end() is called automatically when leaving the scope
await using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');
// .release() is called automatically when leaving the scope
await using connection = await poolCluster.getConnection('clusterA');
// highlight-end
// ... some query
}
:::info For detailed instructions, please follow the AWS SSL Profiles documentation. :::
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');
{
// highlight-start
// .end() is called automatically when leaving the scope
using poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');
// highlight-end
poolCluster.getConnection('clusterA', function (err, _connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// highlight-start
// .release() is called automatically when leaving the scope
using connection = _connection;
// highlight-end
// ... some query
});
}
:::info For detailed instructions, please follow the AWS SSL Profiles documentation. :::
:::tip
await using and using leverage Explicit Resource Management to automatically call .end() or .release() when the variable goes out of scope, so you never forget to clean up connections.
:::
<Tabs> <TabItem value='A.js'>add(group: string, config: PoolOptions)
const mysql = require('mysql2');
const SocksConnection = require('socksjs');
const socksProxy = new SocksConnection({ port: 3306 });
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
stream: socksProxy,
});
// poolCluster.add('clusterB', '...');
const poolNamespace = poolCluster.of('clusterA');
// highlight-end
// Close the pool cluster
poolCluster.end();
const mysql = require('mysql2');
const SocksConnection = require('socksjs');
// highlight-start
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
debug: 1,
stream: function () {
return new SocksConnection({ port: 3306 });
},
});
// poolCluster.add('clusterB', '...');
const poolNamespace = poolCluster.of('clusterA');
// highlight-end
// Close the pool cluster
poolCluster.end();
:::tip 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);
});
:::
<hr />