Better Auth + MySQL - Failing Schema Generation

Hey guys, I have been trying to get better-auth up and running with my existing NextJS project. To do so I have been following the instructions in the better auth documentation: (https://www.better-auth.com/docs/adapters/mysql). Despite that I am unable to generate the respective schemas using the npx @better-auth/cli@latest generate command. In my project I use yarn as my package manager. For whatever reason trying to install the mysql2/promise package, as per documentation, already fails.
yarn add mysql2/promise ─╯
yarn add v1.22.22
[1/4] 🔍 Resolving packages...
Couldn't find any versions for "mysql2" that matches "^3.14.0^"
? Please choose a version of "mysql2" from this list: 3.14.0
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://[email protected]/mysql2/promise.git
Directory: /Users/user/Documents/GitHub/Project
Output:
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
yarn add mysql2/promise ─╯
yarn add v1.22.22
[1/4] 🔍 Resolving packages...
Couldn't find any versions for "mysql2" that matches "^3.14.0^"
? Please choose a version of "mysql2" from this list: 3.14.0
error Command failed.
Exit code: 128
Command: git
Arguments: ls-remote --tags --heads ssh://[email protected]/mysql2/promise.git
Directory: /Users/user/Documents/GitHub/Project
Output:
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
After some research I noticed that there is a package called mysql2-promise available via yarn, but using this package does also not allow me run the generate-schema command. I hope someone has already figured out a way to use a MySQL database in combination with Better Auth.
Solution:
Thank you for your help. I was able to fix the issue, which boils down to very basic mistake / misunderstanding. While I had tried to install the mysql2 package, I then also updated the import statement to "import {createPool } from "mysql2", which then lead to an error once again. It now works as expected.
Jump to solution
5 Replies
KiNFiSH
KiNFiSH3w ago
Mike Ross
Mike RossOP3w ago
When using the mysql2-promisepackage (https://classic.yarnpkg.com/en/package/mysql2-promise) in combination with this better-auth configuration:
import { betterAuth } from 'better-auth'
import { createPool } from 'mysql2-promise'

export const auth = betterAuth({
database: createPool({
host: 'localhost',
user: 'root',
password: '123',
database: 'Project',
port: 3306,
}),
})
import { betterAuth } from 'better-auth'
import { createPool } from 'mysql2-promise'

export const auth = betterAuth({
database: createPool({
host: 'localhost',
user: 'root',
password: '123',
database: 'Project',
port: 3306,
}),
})
I get the following error when running npx @better-auth/cli@latest generate:
2025-04-05T05:34:46.779Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. TypeError: (0 , _mysql2Promise.createPool) is not a function
at /Users/user/Documents/GitHub/Project/src/lib/auth.ts:5:43
at async Function.import (/Users/user/.npm/_npx/05eee8cc65087e98/node_modules/jiti/dist/jiti.cjs:1:199772)
at async resolveConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:346:18)
at async loadConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:146:23)
at async getConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:234:30)
at async Command.generateAction (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:643:18)
2025-04-05T05:34:46.779Z ERROR [Better Auth]: [#better-auth]: Couldn't read your auth config. TypeError: (0 , _mysql2Promise.createPool) is not a function
at /Users/user/Documents/GitHub/Project/src/lib/auth.ts:5:43
at async Function.import (/Users/user/.npm/_npx/05eee8cc65087e98/node_modules/jiti/dist/jiti.cjs:1:199772)
at async resolveConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:346:18)
at async loadConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/c12/dist/shared/c12.PQMoYrit.mjs:146:23)
at async getConfig (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:234:30)
at async Command.generateAction (file:///Users/user/.npm/_npx/05eee8cc65087e98/node_modules/@better-auth/cli/dist/index.mjs:643:18)
Note that the mysql database is up and running and accessible.
Yarn
mysql2-promise
Small promises wrapper for mysql2.
Mike Ross
Mike RossOP3w ago
Will do
Solution
Mike Ross
Mike Ross3w ago
Thank you for your help. I was able to fix the issue, which boils down to very basic mistake / misunderstanding. While I had tried to install the mysql2 package, I then also updated the import statement to "import {createPool } from "mysql2", which then lead to an error once again. It now works as expected.
KiNFiSH
KiNFiSH3w ago
glad you fixed it

Did you find this page helpful?