createPoolCluster
For queries please see the Simple Queries and Prepared Statements examples.
add(group, connectionUri)
add(group: string, connectionUri: string)
- promise.js
- callback.js
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);
}
const mysql = require('mysql2');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', 'mysql://root:password@localhost:3306/test');
// poolCluster.add('clusterB', '...');
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
connection.release();
});
Don't forget to release the connection when finished by using:
connection.release()
add(group, config)
add(group: string, config: PoolOptions)
- promise.js
- callback.js
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);
}
const mysql = require('mysql2');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
host: 'localhost',
user: 'root',
database: 'test',
// port: 3306,
// password: '',
});
// poolCluster.add('clusterB', '...');
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
connection.release();
});
Don't forget to release the connection when finished by using:
connection.release()
add(group, config) — SHA1
add(group: string, config: PoolOptions)
- promise.js
- callback.js
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);
}
const mysql = require('mysql2');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
passwordSha1: Buffer.from('8bb6118f8fd6935ad0876a3be34a717d32708ffd', 'hex'),
});
// poolCluster.add('clusterB', '...');
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
connection.release();
});
Don't forget to release the connection when finished by using:
connection.release()
add(group, config) — SSL
add(group: string, config: PoolOptions)
- promise.js
- callback.js
- certs/ca-cert.pem
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);
}
const mysql = require('mysql2');
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', '...');
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
connection.release();
});
- See ssl/certs.
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
- promise.js
- callback.js
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);
}
For detailed instructions, please follow the AWS SSL Profiles documentation.
try {
const [res] = await connection.query('SHOW `status` LIKE "Ssl_cipher"');
await poolCluster.end();
console.log(res);
} catch (err) {
console.log(err);
}
const mysql = require('mysql2');
const awsCaBundle = require('aws-ssl-profiles');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
// ...
host: 'db.id.ap-southeast-2.rds.amazonaws.com',
ssl: awsCaBundle,
});
// poolCluster.add('clusterB', '...');
poolCluster.getConnection('clusterA', function (err, connection) {
if (err instanceof Error) {
console.log(err);
return;
}
// ... some query
connection.release();
});
For detailed instructions, please follow the AWS SSL Profiles documentation.
connectionquery('SHOW `status` LIKE "Ssl_cipher"', function (err, res) {
poolCluster.end();
if (err instanceof Error) {
console.log(err);
return;
}
console.log(res);
});
Don't forget to release the connection when finished by using:
connection.release()
add(group, config) — Socks
add(group: string, config: PoolOptions)
- A.js
- B.js
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');
const mysql = require('mysql2');
const SocksConnection = require('socksjs');
const poolCluster = mysql.createPoolCluster();
poolCluster.add('clusterA', {
debug: 1,
stream: function () {
return new SocksConnection({ port: 3306 });
},
});
// poolCluster.add('clusterB', '...');
const poolNamespace = poolCluster.of('clusterA');
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