nitrojs / nitrojs/nitro

Nitro keeps disconnecting mysql connection

Open
#3,095 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs reproduction nuxt v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default defineNuxtConfig({
  css: [
    '@/assets/css/custom-scrollbar.css'
  ],
  ssr: true,
  app: {
    baseURL: "/",
  },
  compatibilityDate: "2024-11-01",
  devtools: { enabled: true },
  modules: [
    "@nuxtjs/tailwindcss",
    "nuxt-auth-utils",
    "@nuxt/image",
    "@nuxt/icon"
  ],
  nitro: {
    experimental: {
      database: true
    },
    database: {
      default: {
        connector: 'mysql2',
        options: {
          database: process.env.NUXT_MYSQL_DATABASE_NAME,
          user: process.env.NUXT_MYSQL_USER,
          password: process.env.NUXT_MYSQL_PASSWORD,
          host: process.env.NUXT_MYSQL_HOST,
          port: process.env.NUXT_MYSQL_PORT,
          waitForConnections: true,
          connectionLimit: 10,
          queueLimit: 0,
          enableKeepAlive: true,
        }
      }
    },
    storage: {
      db: {
        driver: 'fs',
        base: resolve(__dirname, '.cache/db')
      }
    }
  }
});
Reproduction

I run Nuxt3 in development (or production) mode, some time passes and it loses connection to the database

Describe the bug

I've had a problem with Nuxt3 for some time, or more precisely with Nitro built into it. I'm using the experimental "database" option and I'm using mysql2 as a connector. The problem is that after some time of inactivity on the site, it disconnects the connection to the database and the only thing I have in the logs is this: [nuxt] [request error] [unhandled] [500] Can't add new command when connection is in closed state

Additional context

No response

Logs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Nitro database configuration shown in the report, focusing on the mysql2 connector and the idle-connection path that produces “Can’t add new command when connection is in closed state.” Reproduce after inactivity in Nuxt 3 development or production mode; done means subsequent database requests remain usable without this error, with an appropriate regression test if the project supports one.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, nuxt, typescript
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.