> ## Documentation Index
> Fetch the complete documentation index at: https://villagesql.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MySQL 指南

> 面向开发者的实用 MySQL 指南。涵盖数据建模、查询、安全、性能、AI 集成和日常运维。

MySQL 的覆盖面很广。这些指南聚焦于最常遇到的部分——从您在第一次迁移前只需做一次的决定，到您每周都会用到的技巧。每一篇都注重实用，并且适用于 VillageSQL。

刚接触 SQL？[MySQL 教程](https://villagesql.com/docs/zh/tutorial/index.md)按顺序、一次一课地讲解它，全部基于同一个示例数据库。如果您已经会写 SQL，请从[选择数据类型](https://villagesql.com/docs/zh/guides/choosing-data-types.md)、[联接](https://villagesql.com/docs/zh/guides/joins.md)和[索引](https://villagesql.com/docs/zh/guides/mysql-indexes.md)开始——它们涵盖了其他一切所依赖的基础。

## 比较 MySQL 和 PostgreSQL

* [MySQL 与 PostgreSQL 如何比较，以及如何选择](https://villagesql.com/docs/zh/guides/mysql-vs-postgresql.md)

## 存储数据

一旦数据进入生产环境，列和模式的决定就很难撤销。这些指南涵盖列类型、主键策略、约束，以及由 VillageSQL 扩展框架启用的类型——例如原生 UUID 和网络地址类型。

* [如何高效地生成和存储 UUID](https://villagesql.com/docs/zh/guides/uuids.md)
* [如何在自增、UUID 和复合键之间选择](https://villagesql.com/docs/zh/guides/primary-key-strategies.md)
* [如何高效地存储 IPv4 和 IPv6 地址](https://villagesql.com/docs/zh/guides/storing-ip-addresses.md)
* [如何在 MySQL 中存储和查询 MAC 地址](https://villagesql.com/docs/zh/guides/storing-mac-addresses.md)
* [如何使用 VillageSQL 原生处理 IPv6 地址](https://villagesql.com/docs/zh/guides/ipv6-storage.md)
* [如何存储、查询和操作 JSON 文档](https://villagesql.com/docs/zh/guides/json-in-mysql.md)
* [如何为您的数据选择正确的列类型](https://villagesql.com/docs/zh/guides/choosing-data-types.md)
* [如何存储真正的布尔值而不是 TINYINT(1)](https://villagesql.com/docs/zh/guides/boolean-columns.md)
* [字符集和排序规则如何影响您的数据](https://villagesql.com/docs/zh/guides/character-sets.md)
* [如何用 MySQL 时间戳正确处理时区](https://villagesql.com/docs/zh/guides/timestamps-timezones.md)
* [如何通过数据库规范化构建您的模式](https://villagesql.com/docs/zh/guides/normalization.md)
* [如何用外键强制引用完整性](https://villagesql.com/docs/zh/guides/foreign-keys.md)
* [如何用生成列计算并索引派生值](https://villagesql.com/docs/zh/guides/generated-columns.md)
* [如何用 CHECK 约束在数据库级别验证数据](https://villagesql.com/docs/zh/guides/check-constraints.md)
* [如何用 deleted\_at 列实现软删除](https://villagesql.com/docs/zh/guides/soft-deletes.md)
* [如何用 utf8mb4 存储表情符号和完整 Unicode](https://villagesql.com/docs/zh/guides/utf8mb4.md)

## 编写查询

这些指南涵盖您最常编写的 SQL，从 JOIN 和聚合到窗口函数、CTE 和全文搜索。VillageSQL 特有的指南包括按 IP 子网查询，以及用 HTTP 和 AI 函数丰富结果。

* [如何让缓慢或错误的 JOIN 正常工作](https://villagesql.com/docs/zh/guides/joins.md)
* [如何在子查询和 JOIN 之间做决定](https://villagesql.com/docs/zh/guides/subqueries-vs-joins.md)
* [如何判断 CTE 是在帮忙还是在添乱](https://villagesql.com/docs/zh/guides/ctes-in-mysql.md)
* [如何让缓慢的 GROUP BY 变快](https://villagesql.com/docs/zh/guides/group-by-having.md)
* [如何用窗口函数计算累计总和、排名和移动平均](https://villagesql.com/docs/zh/guides/window-functions.md)
* [NULL 在 MySQL 中的行为以及如何正确处理它](https://villagesql.com/docs/zh/guides/null-in-mysql.md)
* [如何在 MySQL 中实现全文搜索](https://villagesql.com/docs/zh/guides/full-text-search.md)
* [如何用视图简化复杂查询](https://villagesql.com/docs/zh/guides/views.md)
* [如何用 upsert 在一条语句中插入或更新行](https://villagesql.com/docs/zh/guides/upsert.md)
* [如何用键集分页和 OFFSET 分页处理大结果集](https://villagesql.com/docs/zh/guides/pagination.md)
* [如何在 SQL 中操作和转换字符串](https://villagesql.com/docs/zh/guides/string-functions.md)
* [如何在 MySQL 查询中处理日期和时间](https://villagesql.com/docs/zh/guides/date-time-functions.md)
* [如何在 MySQL 中按 IP 子网查询和过滤](https://villagesql.com/docs/zh/guides/subnet-queries.md)
* [如何在 MySQL 中按 IP 地址进行地理定位查找](https://villagesql.com/docs/zh/guides/ip-geolocation.md)
* [如何直接从 MySQL 发起 HTTP 请求](https://villagesql.com/docs/zh/guides/http-requests-in-mysql.md)
* [如何用外部 REST API 的数据丰富查询结果](https://villagesql.com/docs/zh/guides/rest-api-enrichment.md)
* [如何用 cube 类型存储和查询多维数据](https://villagesql.com/docs/zh/guides/cube-queries.md)
* [如何在 MySQL 中用三元组相似度查找相似字符串](https://villagesql.com/docs/zh/guides/trigram-similarity.md)
* [如何在 MySQL 中按读音和编辑距离匹配名称](https://villagesql.com/docs/zh/guides/fuzzy-string-matching.md)

## 保护数据

VillageSQL 的加密扩展把 bcrypt、AES 加密、HMAC 签名和摘要函数带入 SQL。这些指南涵盖这些加密函数，以及 MySQL 内置的用户和权限管理。

* [如何用 bcrypt 和 Argon2 安全地哈希密码](https://villagesql.com/docs/zh/guides/password-hashing.md)
* [如何生成校验和与摘要以保证数据完整性](https://villagesql.com/docs/zh/guides/hashing-data.md)
* [如何在数据库级别加密敏感的列数据](https://villagesql.com/docs/zh/guides/encrypting-columns.md)
* [如何在 MySQL 中用 AES 加密和解密数据](https://villagesql.com/docs/zh/guides/symmetric-encryption.md)
* [如何在 MySQL 中用 HMAC 签名和验证数据](https://villagesql.com/docs/zh/guides/hmac-mysql.md)
* [如何生成加密安全的随机值](https://villagesql.com/docs/zh/guides/random-data-mysql.md)
* [如何创建和管理 MySQL 用户和权限](https://villagesql.com/docs/zh/guides/user-management.md)
* [如何让用户用 JWT 而不是密码登录](https://villagesql.com/docs/zh/guides/jwt-authentication.md)
* [如何为生产环境加固 MySQL 服务器](https://villagesql.com/docs/zh/guides/security-hardening.md)

## 优化性能

慢查询通常是索引或模式的问题。这些指南涵盖如何诊断它们——利用 EXPLAIN 输出、慢查询日志，以及对 InnoDB 如何存储数据的理解——以及要改什么。

* [如何诊断和修复缓慢的 MySQL 查询](https://villagesql.com/docs/zh/guides/mysql-query-performance.md)
* [索引的工作原理以及何时使用它们](https://villagesql.com/docs/zh/guides/mysql-indexes.md)
* [如何读懂 MySQL EXPLAIN 输出并采取行动](https://villagesql.com/docs/zh/guides/reading-explain.md)
* [覆盖索引如何消除回表查找](https://villagesql.com/docs/zh/guides/covering-indexes.md)
* [如何用慢查询日志找出并修复慢查询](https://villagesql.com/docs/zh/guides/slow-query-log.md)
* [如何用连接池高效地管理数据库连接](https://villagesql.com/docs/zh/guides/connection-pooling.md)
* [InnoDB 如何存储数据以及为什么这对性能很重要](https://villagesql.com/docs/zh/guides/innodb-storage.md)
* [如何快速插入大量数据](https://villagesql.com/docs/zh/guides/bulk-inserts.md)
* [如何对大表分区以获得更好的查询性能](https://villagesql.com/docs/zh/guides/table-partitioning.md)

## 用 AI 构建

VillageSQL 的 AI 扩展让您可以直接从 SQL 调用语言模型并生成嵌入。这些指南涵盖提供商设置，以及如何在真实查询中使用 `ai_prompt()` 和 `ai_embedding()`。

* [如何为 VillageSQL 配置 AI 提供商凭据](https://villagesql.com/docs/zh/guides/ai-api-setup.md)
* [如何用 ai\_prompt() 直接从 SQL 调用 AI 模型](https://villagesql.com/docs/zh/guides/ai-prompts-in-mysql.md)
* [如何在 MySQL 中生成和存储向量嵌入](https://villagesql.com/docs/zh/guides/vector-embeddings.md)
* [如何在 MySQL 内部使用 AI 对文本分类](https://villagesql.com/docs/zh/guides/text-classification.md)
* [如何在 MySQL 中用 AI 函数进行情感分析](https://villagesql.com/docs/zh/guides/sentiment-analysis.md)
* [如何直接在 SQL 中用 AI 生成文本摘要](https://villagesql.com/docs/zh/guides/ai-summarization.md)
* [如何用 MCP 将 AI 代理连接到 MySQL](https://villagesql.com/docs/zh/guides/mysql-ai-agents-mcp.md)

## 运维 MySQL

这些指南涵盖运维方面：模式迁移、Docker 设置、事务、复制、备份，以及生产环境出问题时该怎么办。

* [如何以最少的停机时间升级 MySQL](https://villagesql.com/docs/zh/guides/upgrade.md)
* [如何在 MySQL 8.0 停止支持后升级到 8.4](https://villagesql.com/docs/zh/guides/mysql-80-eol-upgrade.md)
* [如何从 PostgreSQL 迁移到 MySQL](https://villagesql.com/docs/zh/guides/postgres-to-mysql.md)
* [如何从 MariaDB 迁移到 MySQL](https://villagesql.com/docs/zh/guides/mariadb-to-mysql-migration.md)
* [如何在生产环境中安全地管理模式变更](https://villagesql.com/docs/zh/guides/schema-migrations.md)
* [如何在不停机的情况下对线上表运行 ALTER TABLE](https://villagesql.com/docs/zh/guides/zero-downtime-schema-changes.md)
* [如何为 MySQL 配置 Django、Rails 和 Prisma](https://villagesql.com/docs/zh/guides/mysql-with-orms.md)
* [如何在 Docker 中运行 MySQL 用于本地开发和生产](https://villagesql.com/docs/zh/guides/mysql-on-docker.md)
* [如何使用 Testcontainers 针对真实的 MySQL 服务器进行测试](https://villagesql.com/docs/zh/guides/testing-with-testcontainers.md)
* [如何在 Kubernetes 上运行 MySQL](https://villagesql.com/docs/zh/guides/mysql-on-kubernetes.md)
* [如何用事务保持数据一致](https://villagesql.com/docs/zh/guides/transactions.md)
* [如何检测、预防死锁并从中恢复](https://villagesql.com/docs/zh/guides/deadlocks.md)
* [如何修复最常见的 MySQL 错误](https://villagesql.com/docs/zh/guides/common-mysql-errors.md)
* [如何在 MySQL 中编写和使用存储过程](https://villagesql.com/docs/zh/guides/stored-procedures.md)
* [如何用 MySQL 触发器自动执行操作](https://villagesql.com/docs/zh/guides/triggers.md)
* [如何从 MySQL 触发器发送 Webhook](https://villagesql.com/docs/zh/guides/http-webhooks.md)
* [如何用 INFORMATION\_SCHEMA 查询 MySQL 元数据](https://villagesql.com/docs/zh/guides/information-schema.md)
* [如何备份和恢复 MySQL 数据库](https://villagesql.com/docs/zh/guides/backup-strategies.md)
* [MySQL 复制的工作原理以及如何设置](https://villagesql.com/docs/zh/guides/replication-basics.md)
* [如何用 MySQL 只读副本扩展读取](https://villagesql.com/docs/zh/guides/read-replicas.md)
* [二进制日志的工作原理以及如何用它进行时间点恢复](https://villagesql.com/docs/zh/guides/binary-logging.md)

## 连接应用程序

每个框架连接 MySQL 的方式都略有不同。这些指南各自针对一台运行中的服务器构建一个可运行的小应用程序，并涵盖第一次容易出错的设置步骤：驱动程序选择、迁移、连接池，以及连接池应该放在哪里。

* [如何用 Prisma 将 Next.js 连接到 MySQL](https://villagesql.com/docs/zh/guides/mysql-with-prisma.md)
* [如何用 Drizzle 将 Next.js 连接到 MySQL](https://villagesql.com/docs/zh/guides/mysql-with-drizzle.md)
* [如何不用 ORM 将 Next.js 连接到 MySQL](https://villagesql.com/docs/zh/guides/nextjs-mysql.md)
* [如何用 SQLAlchemy 2.0 将 FastAPI 连接到 MySQL](https://villagesql.com/docs/zh/guides/fastapi-sqlalchemy-mysql.md)
* [如何用 Eloquent 将 Laravel 连接到 MySQL](https://villagesql.com/docs/zh/guides/laravel-mysql.md)
* [如何用 ActiveRecord 将 Rails 连接到 MySQL](https://villagesql.com/docs/zh/guides/rails-mysql.md)
* [如何用 sqlc 在 Go 中生成类型安全的 MySQL 查询](https://villagesql.com/docs/zh/guides/go-sqlc-mysql.md)
* [如何防止无服务器运行时耗尽 MySQL 连接](https://villagesql.com/docs/zh/guides/serverless-mysql-drizzle.md)
* [如何将 MySQL 表暴露为 REST API](https://villagesql.com/docs/zh/guides/rest-api-from-tables.md)

## 编写扩展

VillageSQL 的扩展框架让您无需触及服务器代码就能为 MySQL 添加自定义函数和类型。这些指南带您从零开始做出一个可安装的扩展。

* [如何用 C++ 构建 VillageSQL 扩展](https://villagesql.com/docs/zh/guides/cpp-extensions.md)
* [如何用 Rust 构建 VillageSQL 扩展](https://villagesql.com/docs/zh/guides/rust-extensions.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.