Registry / database / connect-mysql

connect-mysql

JSON →
library4.0.0jsnpmunverified

A MySQL session store for the Connect/Express session middleware, version 4.0.0. It uses the node-mysql module for database connections and supports connection pooling, session encryption (AES-256-CTR), configurable retries, and automatic cleanup of expired sessions. Unlike generic session stores, it integrates directly with express-session and provides MySQL-specific features like keepalive pings. Release cadence is irregular; last release was in 2018. Key differentiators: built-in encryption and full MySQL pooling support. Note: v2.2 broke backward compatibility with previous session formats; sessions stored with older versions must be cleared or will be discarded with a warning.

npm install connect-mysql
INSTALL
IMPORT
SIG · CONNECT-MYSQL
C
connect-mysql
databasejavascriptv4.0.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.

MySQLStore
var MySQLStore = require('connect-mysql')(session);
var MySQLStore = require('connect-mysql');
connect-mysql exports a function that must be called with the session middleware (e.g., express-session). Common mistake: forgetting to pass the session argument.
MySQLStore (with options)
var store = new MySQLStore(options);
var store = new MySQLStore(options, session);
The constructor takes only options; the session argument is already passed when calling require('connect-mysql')(session).
mysql module (pool)
var mysql = require('mysql');
var mysql = require('connect-mysql/mysql');
The node-mysql module must be installed separately and required for pool option.

Creates an Express app with MySQL-backed session storage, incrementing a view counter per session.

const express = require('express'); const session = require('express-session'); const MySQLStore = require('connect-mysql')(session); const app = express(); app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: true, store: new MySQLStore({ config: { user: process.env.DB_USER ?? 'root', password: process.env.DB_PASS ?? '', database: process.env.DB_NAME ?? 'test' }, table: 'sessions', cleanup: true }) })); app.get('/', (req, res) => { if (!req.session.views) req.session.views = 0; req.session.views++; res.send(`You visited this page ${req.session.views} times`); }); app.listen(3000);
Debug
Known issues
breakingSessions stored with connect-mysql < v2.2 are incompatible with v2.2+. Must clear session table or they will be discarded with a warning.
fix
Delete all rows from the sessions table before upgrading, or set 'discard' option (not documented).
affects: >=2.2
deprecatedThe package has not been updated since 2018. It may not work with modern Node.js versions or recent express-session API changes.
fix
Consider using a maintained alternative like express-mysql-session.
affects: >6
gotchaIf `pool` is set to true, the store will create its own pool using the `config` object. This pollutes the global pool and may lead to unintended connections.
fix
Provide your own pool instance via `options.pool` for better control.
affects: >=4.0.0
gotchaThe `cleanup` option is true by default, which runs a periodic cleanup job. If you have multiple app instances with the same database, they will each run cleanup, possibly causing race conditions.
fix
Set `cleanup: false` on all but one instance, and handle cleanup externally with a cron job or similar.
affects: >=4.0.0
Errors
Common errors & fixes
TypeError: MySQLStore is not a constructor
Forgetting to call the exported function with session middleware.
fix
Correct: var MySQLStore = require('connect-mysql')(session); Then: var store = new MySQLStore(options);
Error: connect ECONNREFUSED 127.0.0.1:3306
MySQL server not running or incorrect host/port in config.
fix
Ensure MySQL is running and config has correct host/port/user/password/database.
Warning: session data is not compatible with current store version
Session was stored by an older version of connect-mysql (<2.2) and is being read by v2.2+.
fix
Clear the sessions table manually: DELETE FROM sessions;
Error: ER_NO_SUCH_TABLE: Table 'database.sessions' doesn't exist
The sessions table has not been created automatically (older versions may not create it).
fix
Create the table manually: CREATE TABLE sessions (session_id VARCHAR(128) PRIMARY KEY, expires INT(11) UNSIGNED, data TEXT);
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources
connect-mysql — npm install connect-mysql · libregistry