Registry / storage / express-mysql-session

express-mysql-session

JSON →
library3.0.3jsnpmunverified

A MySQL session store for Express.js compatible with express-session. Current stable version is 3.0.3, with regular updates. It automatically creates a sessions table with utf8mb4 collation and supports custom schemas. Key differentiators: simple integration, automatic table creation, MySQL2 only (not mariadb). Note: latest version removed deprecated createPool method; use connection pooling or pass pool directly.

npm install express-mysql-session
INSTALL
IMPORT
SIG · EXPRESS-MYSQL-SESS
E
express-mysql-session
storagejavascriptv3.0.3
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
const MySQLStore = require('express-mysql-session')(session);
const MySQLStore = require('express-mysql-session');
The module is a function that must be called with express-session as argument. Common mistake: forgetting to pass session.
MySQLStore (TypeScript)
import session from 'express-session'; import mysqlSession from 'express-mysql-session'; const MySQLStore = mysqlSession(session);
import MySQLStore from 'express-mysql-session';
Default import is not the store itself; you must pass session to the default export.
MySQLStore (with existing connection)
const MySQLStore = require('express-mysql-session')(session); const sessionStore = new MySQLStore({}, existingConnection);
const sessionStore = new MySQLStore({}, connection); // missing (session) call
Always call the export with session first, even when providing an existing connection.

Basic setup of express-session with MySQL session store using default options.

const express = require('express'); const session = require('express-session'); const MySQLStore = require('express-mysql-session')(session); const app = express(); const options = { host: 'localhost', port: 3306, user: 'session_test', password: process.env.MYSQL_PASSWORD || '', database: 'session_test' }; const sessionStore = new MySQLStore(options); app.use(session({ key: 'session_cookie_name', secret: 'session_cookie_secret', store: sessionStore, resave: false, saveUninitialized: false })); app.get('/', (req, res) => { res.send('Session store ready'); }); app.listen(3000, () => console.log('Server listening on port 3000'));
Debug
Known issues
breakingVersion 3.x removed the deprecated createPool method; you must use createPool from mysql2 manually or pass a pool/connection directly.
fix
Use mysql2.createPool() or createConnection() and pass it as the second argument to MySQLStore.
affects: >=3.0.0
gotchaThe module requires mysql2 (not mysql) and does not support mariadb driver.
fix
Use mysql2 package for your MySQL/MariaDB connection.
affects: *
gotchaSession table collation is utf8mb4, which requires MySQL 5.5.3+; older versions will fail if table is auto-created.
fix
Create the sessions table manually with appropriate collation before initializing store.
affects: *
deprecatedThe onReady() method is deprecated and may be removed in future versions.
fix
Use MySQLStore's 'ready' event or check store.isReady property instead.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: MySQLStore is not a constructor
The module export was not called with express-session as argument.
fix
const MySQLStore = require('express-mysql-session')(session); // note (session)
Error: The mysql driver used does not support prepared statements
Using the old 'mysql' package instead of 'mysql2'.
fix
npm install mysql2 and replace require('mysql') with require('mysql2').
ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value
Session data contains characters not supported by utf8mb4 collation.
fix
Ensure session data is valid UTF-8 or use a different collation for the 'data' column.
Upgrade
Version history
3.0.3latest on npm
Audit
Dependencies
express-sessionrequiredpeer dependency; store is designed to be used with express-session middleware
mysql2requiredruntime dependency for MySQL connection; the library wraps mysql2 internally
Agent activity
26 hits · last 30 days
node
24
Amazon
1
Resources
express-mysql-session — npm install express-mysql-session · libregistry