Error: Too many connections (MySQL)

I currently use better-auth in combination with a MySQL database. I use NextJS to render my website. I noticed that when making repeated changes (re-renders) better-auth throws an error:
Error: Too many connections
Error: Too many connections
. I have looked through previous posts and found that someone had the same issue when using a Prisma client. Given that I do not initialize the connection to the database myself, I was wondering how to store the connection during development? This is what my configuration looks like:
export const auth = betterAuth({
database: createPool({
host: env.DATABASE_HOST,
port: env.DATABASE_PORT,
database: env.DATABASE_NAME,
user: env.DATABASE_USER,
password: env.DATABASE_PASSWORD,
}),
emailAndPassword: {
enabled: true,
minPasswordLength: 8,
autoSignIn: true,
},
socialProviders: {
github: {
clientId: env.AUTH_GITHUB_ID,
clientSecret: env.AUTH_GITHUB_SECRET,
},
},
plugins: [nextCookies()],
})
export const auth = betterAuth({
database: createPool({
host: env.DATABASE_HOST,
port: env.DATABASE_PORT,
database: env.DATABASE_NAME,
user: env.DATABASE_USER,
password: env.DATABASE_PASSWORD,
}),
emailAndPassword: {
enabled: true,
minPasswordLength: 8,
autoSignIn: true,
},
socialProviders: {
github: {
clientId: env.AUTH_GITHUB_ID,
clientSecret: env.AUTH_GITHUB_SECRET,
},
},
plugins: [nextCookies()],
})
This is the error shown in the console:
2025-04-12T06:19:33.790Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR [Error: Too many connections] {
code: 'ER_CON_COUNT_ERROR',
errno: 1040,
sqlState: '',
sqlMessage: 'Too many connections',
sql: undefined
}


⨯ [Error [APIError]: Failed to get session] {
status: 'INTERNAL_SERVER_ERROR',
body: [Object],
headers: {},
statusCode: 500,
digest: '3142529621'
}
2025-04-12T06:19:33.790Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR [Error: Too many connections] {
code: 'ER_CON_COUNT_ERROR',
errno: 1040,
sqlState: '',
sqlMessage: 'Too many connections',
sql: undefined
}


⨯ [Error [APIError]: Failed to get session] {
status: 'INTERNAL_SERVER_ERROR',
body: [Object],
headers: {},
statusCode: 500,
digest: '3142529621'
}
1 Reply
KiNFiSH
KiNFiSH2w ago
may be pools are not cleanup on dev so may be you need to flush them unless they remain open and raise a conflict -
let pool: Pool | null = null;

export function getDatabasePool() {
if (!pool) {
pool = createPool({
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT),
database: process.env.DATABASE_NAME,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
connectionLimit: 10,
queueLimit: 0,
waitForConnections: true,
});

if (process.env.NODE_ENV === 'development') {
process.on('SIGTERM', async () => {
if (pool) {
await pool.end();
pool = null;
}
});
}
}
return pool;
}
let pool: Pool | null = null;

export function getDatabasePool() {
if (!pool) {
pool = createPool({
host: process.env.DATABASE_HOST,
port: Number(process.env.DATABASE_PORT),
database: process.env.DATABASE_NAME,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
connectionLimit: 10,
queueLimit: 0,
waitForConnections: true,
});

if (process.env.NODE_ENV === 'development') {
process.on('SIGTERM', async () => {
if (pool) {
await pool.end();
pool = null;
}
});
}
}
return pool;
}
this is sth i found with their docs so may be the flag for termination might not be right but this is what could find and we can circle back if this is not the right solution

Did you find this page helpful?