Manage multiple hosts with PoolCluster
masterA PoolCluster allows you to manage multiple connection pools across different hosts, supporting grouping, retries, and selection strategies (e.g., for Master/Slave setups).
Adding and Removing Nodes
Nodes can be added with an automatic name or a specific identifier. They can be removed by ID or by a pattern (using wildcards).
Selecting Nodes
You can retrieve connections from specific groups, using patterns (wildcards or Regular Expressions), or using the of() method to create a sub-pool with a specific selector.
PoolCluster Options
canRetry: Iftrue, attempts to reconnect when a connection fails. (Default:true)removeNodeErrorCount: Number of failures allowed before a node is removed from the cluster. (Default:5)restoreNodeTimeout: Milliseconds to wait before attempting to reconnect to a failed node. If0, the node is removed and never re-used. (Default:0)defaultSelector: The strategy used to pick a node. Options:RR(Round-Robin),RANDOM, orORDER(first available).
var poolCluster = mysql.createPoolCluster();
// Add nodes
poolCluster.add('MASTER', masterConfig);
poolCluster.add('SLAVE1', slave1Config);
poolCluster.add('SLAVE2', slave2Config);
// Get connection from a specific group (Selector: Round-Robin)
poolCluster.getConnection('SLAVE*', function (err, connection) {});
// Get connection using a pattern and specific selector
poolCluster.getConnection(/^SLAVE[12]$/, 'ORDER', function (err, connection) {});
// Create a sub-pool with a specific selector
var pool = poolCluster.of('SLAVE*', 'RANDOM');
pool.query('SELECT 1');
// Close the cluster
poolCluster.end(function (err) {});