N
Nuxt4d ago
Pikawshh

MySQL connection closes after some time with db0 + mysql2

Hey everyone I’m running a Nuxt 4 app with a connection to a MySQL database using db0 + mysql2. my nuxt config
export default defineNuxtConfig({
compatibilityDate: '2025-09-20',
ssr: false,
modules: [
'@nuxt/ui',
'nuxt-charts',
'@pinia/nuxt',
'pinia-plugin-persistedstate/nuxt',
'nuxt-auth-utils'
],
css: ['~/assets/css/main.css'],
ui: {
colorMode: true
},
pinia: {
storesDirs: ['./stores/**'],
},
nitro: {
experimental: {
database: true
},
database: {
default: {
connector: 'mysql2',
}
}
},
$production: {
runtimeConfig: {
session: {
password: sessionPassword
}
}
},
$development: {
devtools: { enabled: true },
vite: {
server: {
watch: {
usePolling: true,
interval: 100
}
}
}
}
})
export default defineNuxtConfig({
compatibilityDate: '2025-09-20',
ssr: false,
modules: [
'@nuxt/ui',
'nuxt-charts',
'@pinia/nuxt',
'pinia-plugin-persistedstate/nuxt',
'nuxt-auth-utils'
],
css: ['~/assets/css/main.css'],
ui: {
colorMode: true
},
pinia: {
storesDirs: ['./stores/**'],
},
nitro: {
experimental: {
database: true
},
database: {
default: {
connector: 'mysql2',
}
}
},
$production: {
runtimeConfig: {
session: {
password: sessionPassword
}
}
},
$development: {
devtools: { enabled: true },
vite: {
server: {
watch: {
usePolling: true,
interval: 100
}
}
}
}
})
db config file
import { createDatabase } from 'db0'
import mysql from 'db0/connectors/mysql2'

const db = createDatabase(
mysql({
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT || 3306),
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0
})
)

export default db
import { createDatabase } from 'db0'
import mysql from 'db0/connectors/mysql2'

const db = createDatabase(
mysql({
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT || 3306),
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0
})
)

export default db
About the issue Everything works fine at first, but after a few hours/days of inactivity, I get this error in my container logs:
H3Error: Can't add new command when connection is in closed state
at PromiseConnection.query (/app/server/node_modules/mysql2/lib/promise/connection.js:29:22)
H3Error: Can't add new command when connection is in closed state
at PromiseConnection.query (/app/server/node_modules/mysql2/lib/promise/connection.js:29:22)
It looks like the database socket gets closed (MySQL wait_timeout / idle), and db0/mysql2 doesn’t recover → I need to restart the container to make it work again. Someone have any idea or already met that problem ? Thanks for you're help 🙏
2 Replies
kapa.ai
kapa.ai4d ago
Hi @Pikawshh. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry, but the knowledge sources provided do not contain specific information about using db0 with mysql2 in Nuxt 4, nor do they address issues with MySQL connections closing after periods of inactivity or how to handle automatic reconnection in this context. The sources do cover Nitro's server engine, advanced Nitro configuration, and database adapters supported by Nuxt Content (such as SQLite, D1, Postgres, and LibSQL), but there is no mention of db0, mysql2, or strategies for persistent MySQL connections or handling dropped connections in Nuxt 4 or Nitro. Therefore, I cannot provide a confident, source-based answer to your question about this specific MySQL connection issue. If you need official guidance, I recommend checking the documentation for db0, mysql2, or opening an issue in the Nuxt or db0 repositories for more targeted support. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4d ago
<@612280800997539881> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?