Skip to main content

createPoolCluster

信息

有关查询,请参阅 简单查询准备好的语句 示例。

¥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);
}
警告

完成后,不要忘记使用以下命令释放连接:

¥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);
}
警告

完成后,不要忘记使用以下命令释放连接:

¥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);
}
警告

完成后,不要忘记使用以下命令释放连接:

¥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);
}
警告

完成后,不要忘记使用以下命令释放连接:

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

  • connection.release()

add(group, config) — RDS SSL

add(group: string, config: PoolOptions)

你可以使用 Amazon RDS 字符串作为 ssl 属性的值,通过 SSL 连接到 Amazon RDS MySQL。

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

在这种情况下,使用 https://s3.amazonaws.com/rds-downloads/mysql-ssl-ca-cert.pem CA 证书:

¥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);
}
信息

有关详细说明,请遵循 AWS SSL 配置文件文档

¥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);
}
警告

完成后,不要忘记使用以下命令释放连接:

¥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');
测试
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 扩展了 ConnectionOptions 中的所有选项:

¥PoolOptions extends all options from ConnectionOptions:

ConnectionOptions Specification
PoolOptions Specification