文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.3.2 服务器字符集和排序规则12.3.2 Server Character Set and Collation

MySQL 服务器有一个服务器字符集和服务器排序规则。默认情况下,这些为 utf8mb4 和 utf8mb4_0900_ai_ci,但可以在服务器启动时通过命令行或选项文件显式设置,并可在运行时更改。MySQL Server has a server character set and a server collation. By default, these are utf8mb4 and utf8mb4_0900_ai_ci, but they can be set explicitly at server startup on the command line or in an option file and changed at runtime.

最初,服务器字符集和排序规则取决于您启动 mysqld 时使用的选项。您可以使用 --character-set-server 来设置字符集。与此同时,您可以添加 --collation-server 来设置排序规则。如果您没有指定字符集,这就等同于说 --character-set-server=utf8mb4。如果您只指定了字符集(例如,utf8mb4)而没有指定排序规则,这就等同于说 --character-set-server=utf8mb4 --collation-server=utf8mb4_0900_ai_ci,因为 utf8mb4_0900_ai_ci 是 utf8mb4 的默认排序规则。因此,以下三个命令的效果是相同的:Initially, the server character set and collation depend on the options that you use when you start mysqld. You can use --character-set-server for the character set. Along with it, you can add --collation-server for the collation. If you don't specify a character set, that is the same as saying --character-set-server=utf8mb4. If you specify only a character set (for example, utf8mb4) but not a collation, that is the same as saying --character-set-server=utf8mb4 --collation-server=utf8mb4_0900_ai_ci because utf8mb4_0900_ai_ci is the default collation for utf8mb4. Therefore, the following three commands all have the same effect:

mysqld
mysqld --character-set-server=utf8mb4
mysqld --character-set-server=utf8mb4 \
  --collation-server=utf8mb4_0900_ai_ci

更改设置的一种方法是通过重新编译。要在从源代码构建时更改默认服务器字符集和排序规则,请使用 DEFAULT_CHARSET 和 DEFAULT_COLLATION 选项用于 CMake。例如:One way to change the settings is by recompiling. To change the default server character set and collation when building from sources, use the DEFAULT_CHARSET and DEFAULT_COLLATION options for CMake. For example:

cmake . -DDEFAULT_CHARSET=latin1

或者:Or:

cmake . -DDEFAULT_CHARSET=latin1 \
  -DDEFAULT_COLLATION=latin1_german1_ci

无论是 mysqld 还是 CMake 都会验证字符集/排序规则组合是否有效。如果无效,每个程序都会显示错误信息并结束运行。Both mysqld and CMake verify that the character set/collation combination is valid. If not, each program displays an error message and terminates.

如果在CREATE DATABASE语句中未指定数据库字符集和排序规则,则服务器字符集和排序规则将作为默认值使用。它们没有其他用途。The server character set and collation are used as default values if the database character set and collation are not specified in CREATE DATABASE statements. They have no other purpose.

当前服务器字符集和排序规则可以通过character_set_server和collation_server系统变量的值来确定。这些变量可以在运行时进行更改。The current server character set and collation can be determined from the values of the character_set_server and collation_server system variables. These variables can be changed at runtime.