Buffer Bounds Error When Using Sequelize with MySQL Database

Getting a buffer memory error with Sequelize

I’m working on a Node.js project and keep hitting this ERR_BUFFER_OUT_OF_BOUNDS error when trying to sync my database. I have two models set up and I’m just calling sequelize.sync() to create the tables. I’m pretty new to this stack so I’m not sure what’s causing the problem.

The error happens whether the database exists or not. I even tried dropping the database manually through MySQL CLI and running it again, but same result.

The error message I get:

RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to access memory outside buffer bounds
    at boundsError (internal/buffer.js:75:11)
    at Buffer.readUInt8 (internal/buffer.js:243:5)
    at Packet.type (/path/to/project/node_modules/tedious/lib/packet.js:143:24)

My database setup:

const { Sequelize } = require('sequelize')
const Author = require('./models/author')
const Article = require('./models/article')
const config = require('./database-config')

let database
if (process.env.NODE_ENV === 'production') {
    database = new Sequelize(config.production)
} else if (process.env.NODE_ENV === 'development') {
    database = new Sequelize(config.development)
} else {
    database = new Sequelize(config.test)
}

const AuthorModel = database.define('author', Author)
const ArticleModel = database.define('article', Article)
AuthorModel.hasMany(ArticleModel)
ArticleModel.belongsTo(AuthorModel)

module.exports = {
    database,
    Author: AuthorModel,
    Article: ArticleModel,
}

Author model definition:

const { DataTypes } = require('sequelize')

module.exports = {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true,
        validate: {
            isEmail: true,
        },
    },
    fullName: DataTypes.STRING,
    hashedPassword: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    isActive: {
        type: DataTypes.BOOLEAN,
        defaultValue: true,
    },
}

Article model:

const { DataTypes } = require('sequelize')

module.exports = {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    headline: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
            len: [3, 50],
        },
    },
    body: {
        type: DataTypes.TEXT,
        allowNull: false,
        validate: {
            len: [10, 1000],
        },
    },
    publishedAt: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW,
    },
}

Database config:

const development = {
    host: 'localhost',
    database: 'my_app_dev',
    username: 'root',
    password: '',
    dialect: 'mysql',
    port: 3306,
}

const test = {
    host: 'localhost',
    database: 'my_app_test',
    username: 'root',
    password: '',
    dialect: 'mysql',
    port: 3306,
}

module.exports = { development, test }

Any ideas what might be wrong? The error seems to be coming from the tedious package but I’m using MySQL, not SQL Server.

This buffer error happens when your Sequelize config doesn’t match the database driver you’re actually using. Seeing tedious in your stack trace is a red flag - that’s for SQL Server, not MySQL. I hit something similar last year during a project migration. Sequelize wasn’t loading the MySQL driver even though it was configured for MySQL. Check if you’ve got mysql2 installed - it’s what Sequelize recommends for MySQL. Missing it? Run npm install mysql2. Also check your database config. Try adding dialectModule: require('mysql2') to force Sequelize to use the right driver. Package resolution gets confused sometimes when you have multiple database drivers. One more thing - make sure your MySQL server version plays nice with your mysql2 version. Some combos cause weird buffer issues during connection handshake.

Had this exact buffer bounds error on a recent rebuild. Sequelize tries to use the wrong driver even when your MySQL config looks fine. Here’s what fixed it for me: add dialectOptions: { decimalNumbers: true } and pool: { max: 5, min: 0, acquire: 30000, idle: 10000 } to your Sequelize constructor. Pool settings can mess with driver conflicts. Also check your mysql2 version - I had to downgrade from 3.x to 2.3.3 for Node.js compatibility. Look for global npm packages that might interfere with driver resolution too. The tedious error goes away once Sequelize locks onto the MySQL driver instead of grabbing whatever’s first in the module path.

weird, that’s def the issue. Had this same prob a few months back with similar setup. Your config seems fine, but Sequelize is grabbing wrong driver. Try adding dialectModule: require('mysql2') to your db config and make sure you don’t have any MSSQL packages installed. Also check that your connection string isn’t pointing to a SQL Server instance instead of MySQL.

Your stack trace shows Sequelize is trying to use the tedious driver, which only works with SQL Server. This happens when there’s driver confusion in your Node setup. I hit this same issue recently when I had multiple database drivers installed. Fix it by explicitly setting the MySQL driver in your Sequelize config. Add dialectModule: require('mysql2') to your database constructor options. Also check if you’ve got conflicting packages like mssql or tedious in your dependencies - even if you’re not using them, they mess with Sequelize’s driver detection. Run npm ls mysql2 to make sure it’s installed properly. If it’s missing or old, reinstall it. And double-check your MySQL server is actually running on the right port before you try syncing.

You’re experiencing an error from the tedious package while trying to connect to MySQL, which is the source of your problem. The tedious package is intended for SQL Server, not MySQL. It sounds like you may have a package conflict or the wrong driver being loaded.

I faced a similar issue a while ago. I had both mysql2 and mssql packages in my project, and even though Sequelize was set up for MySQL, it kept defaulting to the tedious driver.

It’s important to verify your package.json to ensure mysql2 is included as the MySQL driver. If there are any SQL Server packages (like mssql or tedious), consider removing them temporarily to see if it resolves the issue. You might also want to delete the node_modules folder and carry out a fresh installation.

Additionally, make sure that the NODE_ENV variable is correctly set when you run the application, as your configuration only exports for development and test environments, not production.