Registry / database / mysql-cluster

mysql-cluster

JSON →
library0.5.0jsnpmunverified

A wrapper around the MySQL driver (mysql2 or mysql) that provides easy master/slave server setup via a PoolCluster configuration. Version 0.5.0 is the current stable release, though it has seen no recent updates. It allows you to define master and slave node pools and automatically route queries to the appropriate pool. Key differentiators include support for retries, node error counting, and round-robin or random selection strategies. This package simplifies read/write splitting in MySQL deployments.

npm install mysql-cluster
INSTALL
IMPORT
SIG · MYSQL-CLUSTER
M
mysql-cluster
databasejavascriptv0.5.0
harness data pending
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(); }); });
Debug
Known issues
gotchaThe package requires mysql2 as a peer dependency but does not install it automatically. Ensure mysql2 is in your package.json.
fix
npm install mysql2
affects: all
gotchaThe factory function uses module.exports = function(config){...}, so const cluster = require('mysql-cluster') returns a function, not an object. You must call it immediately.
fix
const cluster = require('mysql-cluster')(config);
affects: all
deprecatedThe package has not been updated since 2020 and relies on outdated MySQL driver APIs. Consider using mysql2's built-in PoolCluster.
fix
Migrate to mysql2's createPoolCluster API.
affects: 0.5.0
gotchaError: Cannot find module 'mysql2' will occur if mysql2 is not installed as a dependency.
fix
npm install mysql2
affects: all
gotchaThe 'driver' config option is optional but if omitted, defaults to mysql2. Explicitly providing a different driver may cause unexpected behavior.
fix
Always set driver explicitly or ensure the default mysql2 is installed.
affects: all
Errors
Common errors & fixes
TypeError: require(...) is not a function
Calling require('mysql-cluster') without invoking the returned factory function.
fix
const cluster = require('mysql-cluster')(config); // call it immediately
Cannot find module 'mysql2'
mysql2 is not installed; it is a peer dependency.
fix
Run 'npm install mysql2'
TypeError: Cannot read property 'slave' of undefined
Calling cluster.slave() but cluster is not properly initialized because require returned a function.
fix
Ensure you call require('mysql-cluster')(config) to get the cluster object.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies
mysql2requiredDefault MySQL driver used for database connections
Agent activity
4 hits · last 30 days
node
4
Resources
mysql-cluster — npm install mysql-cluster · libregistry