Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
default
✓ import mysqlCluster from 'mysql-cluster'; const cluster = mysqlCluster(config);
✗ const cluster = require('mysql-cluster')(config);
ES6 import syntax; the package does not export named members.
CommonJS require
✓ const cluster = require('mysql-cluster')(config);
✗ const cluster = require('mysql-cluster'); cluster(config);
CommonJS usage; the module.exports is a factory function, not a constructor.
TypeScript type
✓ import mysqlCluster from 'mysql-cluster';
✗ import * as mysqlCluster from 'mysql-cluster';
The package lacks type definitions. Use @types/mysql or define your own types.
Initializes a MySQL cluster with master and slave pools, acquires a slave connection, runs a query, and releases the connection.
import mysql from 'mysql2';
import mysqlCluster from 'mysql-cluster';
const config = {
driver: mysql,
cluster: {
canRetry: true,
removeNodeErrorCount: 5,
defaultSelector: 'RR'
},
global: {
host: 'localhost',
user: 'root',
password: process.env.MYSQL_PASSWORD ?? '',
database: 'test',
connectionLimit: 100,
waitForConnections: true,
queueLimit: 10
},
pools: {
master: {
config: { user: 'root' },
nodes: [{ host: 'localhost' }]
},
slave: {
config: { user: 'root' },
nodes: [{ host: 'localhost' }, { host: 'localhost', user: 'root' }]
}
}
};
const cluster = mysqlCluster(config);
cluster.slave((err, conn) => {
if (err) {
console.error('Failed to acquire connection:', err);
return;
}
conn.query('SELECT * FROM test LIMIT 10', (err, results) => {
if (err) throw err;
console.log(results);
conn.release();
});
});
Errors
Common errors & fixes
TypeError: require(...) is not a function
Calling require('mysql-cluster') without invoking the returned factory function.
fixconst cluster = require('mysql-cluster')(config); // call it immediately Cannot find module 'mysql2'
mysql2 is not installed; it is a peer dependency.
fixRun 'npm install mysql2'
TypeError: Cannot read property 'slave' of undefined
Calling cluster.slave() but cluster is not properly initialized because require returned a function.
fixEnsure you call require('mysql-cluster')(config) to get the cluster object. Audit
Dependencies
mysql2requiredDefault MySQL driver used for database connections