# connect-mysql

> a MySQL session store for connect

Latest version **4.0.0** (published 2020-04-05) · 0 weekly downloads

## Install

```sh
npm install connect-mysql
pnpm add connect-mysql
yarn add connect-mysql
bun add connect-mysql
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2020-04-05 |
| First published | 2012-04-05 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >6 |
| Dependencies | 2 |
| Unpacked size | 17.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 54 |
| Author | Nathan LaFreniere |
| Maintainers | jas-, mjeffery, nlf |

## Links

- npm: https://www.npmjs.com/package/connect-mysql
- Repository: https://github.com/nlf/connect-mysql
- Issues: https://github.com/nlf/connect-mysql/issues
- npm.io page: https://npm.io/package/connect-mysql

## Dependencies (2)

- [mysql](https://npm.io/package/mysql.md) ^2.18.1
- [kruptein](https://npm.io/package/kruptein.md) ^2.0.7

## Recent versions

- 4.0.0 (latest) — 2020-04-05
- 3.0.0 — 2020-04-05
- 2.2.5 — 2019-03-04
- 2.2.4 — 2019-03-03
- 2.2.3 — 2019-03-02
- 2.2.1 — 2019-02-28
- 2.2.0 — 2019-02-28
- 2.1.7 — 2017-12-01
- 2.1.6 — 2017-10-26
- 2.1.5 — 2017-09-19
- 2.1.4 — 2017-09-08
- 2.1.3 — 2017-09-04
- 2.1.1 — 2014-06-12
- 2.1.0 — 2014-06-01
- 2.0.5 — 2014-03-21
- … 21 more at https://npm.io/package/connect-mysql/versions

## README

# connect-mysql

This is a simple MySQL backed session store for connect.

It uses the [node-mysql](https://github.com/felixge/node-mysql) module already installed in your project to establish and pool connections.

## Upgrading

Changes introduced in v2.2 mean sessions stored in earlier versions are not backwards compatible with v2.2.
It it recommended that you clear the session table; alternately sessions will be discarded with a warning message.

## Options

* `table`: the name of the database table that should be used for storing sessions. Defaults to `'sessions'`
* `pool`: a node-mysql connection pool or `true` if the store should instantiate its own pool
* `config`: the configuration that will be passed to `createConnection()` or `createPool()` if pool is `true`
* `retries`: how many times to retry connecting to the database before failing.  Defaults to `3`
* `keepalive`: keep pooled connections open by periodically pinging them.  Set to `true` to use the default interval of `30000` ms or provide a positive number to set your own.  Defaults to `true`.
* `cleanup`: a boolean specifying whether to enable the cleanup events. note that if this is disabled, cleanup will not take place at all and should be done externally.  Sessions with an expiration time of `0` will always be ignored and should also be cleaned up externally.
* `secret`: key that will be used to encrypt session data.  If this option is not provided then data will be stored in plain text
* `algorithm`: the algorithm that should be used to encrypt session data.  Defaults to `'aes-256-ctr'`


## Examples
Here are some example use cases to get your application up and running.

### Default use case
Simple use case using the `express` framework & `connect-session` middleware with `connect-mysql` as the data store.

```javascript
var express = require('express'), // express framework
    session = require('express-session'), // session middleware
    cookieParser = require('cookie-parser'), // cookie middleware
    MySQLStore = require('connect-mysql')(session), // mysql session store
    options = {
      config: {
        user: 'username', 
        password: 'password',
        database: 'databasename' 
      }
    },
    app = express();

app.use(cookieParser());

app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: {
    httpOnly: false,
    secure: false,
    maxAge: 1000 * 60 * 60 * 24 * 3,
    expires: 1000 * 60 * 60 * 24 * 3
  },
  store: new MySQLStore(options) // Change the express session store
}));

app.get('/', function (req, res) {
  if (req.session.views) {
    req.session.views++
  } else {
    req.session.views = 1;
  }

  res.send('Hello world! '+req.session.views);
});
 
app.listen(3000, 'localhost');
```

### Connection pooling example
For those MySQL installations that make use of pools the following examples are available.

```javascript
  var mysql = require('mysql'),
      options = {
        pool: mysql.createPool({
          user: 'dbuser',
          password: 'dbpassword',
          database: 'db'
        })
      };
```

Or

```javascript
var options = {
      pool: true,
      config: {
        user: 'dbuser', 
        password: 'dbpassword', 
        database: 'db' 
      }
    };
```

### Ssession encryption example
This option enables transparent session encryption assisting

```javascript
var options = {
      secret: 'thesessionsecret',
      config: {
        user: 'dbuser', 
        password: 'dbpassword', 
        database: 'db' 
      }
   };
```

## contributing ##

Contributions are welcome & appreciated. Refer to the [contributing document](https://github.com/nlf/connect-mysql/blob/master/CONTRIBUTING.md)
to help facilitate pull requests.

## license ##

This software is licensed under the [MIT License](https://github.com/nlf/connect-mysql/blob/master/LICENSE).

Nathan LaFreniere, Copyright (c) 2012 &Yet

---
_Source: https://npm.io/package/connect-mysql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
