MySQL

MySQL 是一种流行的开源关系型数据库管理系统(RDBMS),被广泛用于构建 Web 应用和其他类型的软件。它提供了一种灵活且可扩展的数据库解决方案,能够高效地存储和检索数据。 更多信息请访问: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

确保你已经安装并配置了 MySQL。然后,你可以将它直接连接到 Better Auth。

🌐 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
  }),
});

欲了解更多信息,请查阅 Kysely 的文档,参考 MySQLDialect

架构生成与迁移

🌐 Schema generation & migration

Better Auth CLI 允许你根据你的 Better Auth 配置和插件生成或迁移数据库模式。

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

MySQL 架构生成

MySQL 架构迁移

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

联接(实验性)

🌐 Joins (Experimental)

当 Better-Auth 需要在单个查询中从多张表获取相关数据时,数据库连接(joins)非常有用。像 /get-session/get-full-organization 等端点以及许多其他端点都极大地受益于这一功能,根据数据库延迟的不同,性能提升可达 2 到 3 倍。

🌐 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.

1.4.0 版本起,Kysely 的 MySQL 方言开箱即支持连接操作。

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

要启用此功能,你需要在认证配置中将 experimental.joins 选项设置为 true

🌐 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 在底层通过 Kysely 适配器得到支持,任何 Kysely 支持的数据库也将被支持。(在此阅读更多)

如果你正在寻找性能改进或技巧,请查看我们的 性能优化指南

On this page