mysql-connection-manager
Manages keep-alive signals, reconnections for a MySQL connection or connection pool.
![Status of Dependencies](https://david-dm.org/chill117/mysql-connection-manager.svg)
Installation
Add to your application via npm
:
npm install mysql-connection-manager --save
This will install mysql-connection-manager
and add it to your application's package.json
file.
How to Use
To have mysql-connection-manager
create the connection for you:
var MySQLConnectionManager = require('mysql-connection-manager');
var options = {
host: 'localhost',
port: 3306,
user: 'db_user',
password: 'password',
database: 'db_name'
};
var manager = new MySQLConnectionManager(options);
manager.on('connect', function(connection) {
});
var something = new SomeThing(someOtherOptions, manager.connection);
The connection object is the same as provided by node-mysql.
You can also pass an already existing node-mysql connection object to mysql-connection-manager
, like this:
var mysql = require('mysql');
var MySQLConnectionManager = require('mysql-connection-manager');
var options = {
host: 'localhost',
port: 3306,
user: 'db_user',
password: 'password',
database: 'db_name'
};
var connection = mysql.createConnection(options);
var manager = new MySQLConnectionManager(options, connection);
To cleanly end the current connection:
manager.endConnection();
Options
A list of all available options:
var options = {
host: 'localhost',
port: 3306,
user: 'connect_mng_test',
password: 'password',
database: 'connect_mng_test',
autoReconnect: true,
reconnectDelay: [
500,
1000,
5000,
30000,
300000
],
useConnectionPooling: false,
reconnectDelayGroupSize: 5,
maxReconnectAttempts: 25,
keepAlive: true,
keepAliveInterval: 30000
};
Connection Pooling
When useConnectionPooling
is set to true
, the manager.connection
object is a connection pool object returned by mysql.createPool
; see node-mysql for details.
SSL
Provide your SSL configuration options as you would when using the node-mysql module directly. Here's an example:
var MySQLConnectionManager = require('mysql-connection-manager');
var options = {
ssl: {
cert : fs.readFileSync( '/path/to/server-cert.pem'),
key : fs.readFileSync( '/path/to/server-key.pem')
}
};
var manager = new MySQLConnectionManager(options);
For more details on SSL options, see node-mysql.
Reconnect Delays
The reconnect-related options may require a bit of additional explanation. With the default options shown above, the reconnect attempts will have the following delay pattern:
- Attempts #1 through #5 will have a delay of 500 milliseconds each.
- Attempts #6 through #10 will have a delay of 1 second each.
- Attempts #11 through #15 will have a delay of 5 seconds each.
- Attempts #16 through #20 will have a delay of 30 seconds each.
- Attempts #21 through #25 will have a delay of 5 minutes each.
If the reconnectDelayGroupSize
was 3:
- Attempts #1 through #3 will have a delay of 500 milliseconds each.
- Attempts #4 through #6 will have a delay of 1 second each.
- Attempts #7 through #9 will have a delay of 5 seconds each.
- Attempts #10 through #12 will have a delay of 30 seconds each.
- Attempts #13 through #25 will have a delay of 5 minutes each.
Any reconnect attempts beyond the last value in the reconnectDelay
array will simply use the last value from the reconnectDelay
array.
Alternatively you may supply a single integer value to the reconnectDelay
option to have one delay time between all reconnect attempts, like this:
var options = {
reconnectDelay: 500
};
Events
There are a few events that can be listened for on the manager
object:
manager.on('connect', function(connection) {
});
manager.on('reconnect', function(connection) {
});
manager.on('disconnect', function() {
});
The manager
object is extended with the nodejs EventEmitter, so you can use all of the methods that it provides as well: on
, off
, once
, emit
, etc.
Debugging
mysql-connection-manager
uses the debug module to output debug messages to the console. To output all debug messages, run your node app with the DEBUG
environment variable:
DEBUG=mysql-connection-manager node your-app.js
This will output log messages as well as error messages from mysql-connection-manager
.
Contributing
There are a number of ways you can contribute:
- Improve or correct the documentation - All the documentation is in this
readme.md
file. If you see a mistake, or think something should be clarified or expanded upon, please submit a pull request - Report a bug - Please review existing issues before submitting a new one; to avoid duplicates. If you can't find an issue that relates to the bug you've found, please create a new one.
- Request a feature - Again, please review the existing issues before posting a feature request. If you can't find an existing one that covers your feature idea, please create a new one.
- Fix a bug - Have a look at the existing issues for the project. If there's a bug in there that you'd like to tackle, please feel free to do so. I would ask that when fixing a bug, that you first create a failing test that proves the bug. Then to fix the bug, make the test pass. This should hopefully ensure that the bug never creeps into the project again. After you've done all that, you can submit a pull request with your changes.
Before you contribute code, please read through at least some of the source code for the project. I would appreciate it if any pull requests for source code changes follow the coding style of the rest of the project.
Now if you're still interested, you'll need to get your local environment configured.
Configure Local Environment
Step 1: Get the Code
First, you'll need to pull down the code from GitHub:
git clone git@github.com:chill117/mysql-connection-manager.git
Step 2: Install Dependencies
Second, you'll need to install the project dependencies as well as the dev dependencies. To do this, simply run the following from the directory you created in step 1:
npm install
Step 3: Set Up the Test Database
Now, you'll need to set up a local test database:
{
host: 'localhost',
port: 3306,
user: 'connect_mng_test',
password: 'password',
database: 'connect_mng_test'
};
These database credentials are located at test/config/database.js
Running Tests
With your local environment configured, running tests is as simple as:
npm test