MySQL

MySQL is a popular open-source relational database management system (RDBMS) that is widely used for building web applications and other types of software. It provides a flexible and scalable database solution that allows for efficient storage and retrieval of data. Read more here: MySQL.

Example Usage

Make sure you have MySQL installed and configured. Then, you can connect it straight into Better Auth.

auth.ts
import { betterAuth } from "better-auth";
import { createPool } from "mysql2/promise";

export const auth = betterAuth({
  database: createPool({
    host: "localhost",
    user: "root",
    password: "password",
    database: "database",
    timezone: "Z", // Important to ensure consistent timezone values
  }),
});

For more information, read Kysely's documentation to the MySQLDialect.

Schema generation & migration

The Better Auth CLI allows you to generate or migrate your database schema based on your Better Auth configuration and plugins.

MySQL Schema Generation

MySQL Schema Migration

✅ Supported✅ Supported
Schema Generation
npx @better-auth/cli@latest generate
Schema Migration
npx @better-auth/cli@latest migrate

Joins (Experimental)

Database joins is useful when Better-Auth needs to fetch related data from multiple tables in a single query. Endpoints like /get-session, /get-full-organization and many others benefit greatly from this feature, seeing upwards of 2x to 3x performance improvements depending on database latency.

The Kysely MySQL dialect supports joins out of the box since version 1.4.0.

To enable this feature, you need to set the experimental.joins option to true in your auth configuration.

auth.ts
export const auth = betterAuth({
  experimental: { joins: true }
});

It's possible that you may need to run migrations after enabling this feature.

Additional Information

MySQL is supported under the hood via the Kysely adapter, any database supported by Kysely would also be supported. (Read more here)

If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.

On this page