8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

MySQL 8.4 参考手册 / 12 种字符集、排序规则、Unicode / 12.5 配置应用程序字符集和排序规则
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

12.5 配置应用程序字符集和排序规则12.5 Configuring Application Character Set and Collation

对于使用默认 MySQL 字符集和排序规则(utf8mb4,utf8mb4_0900_ai_ci)存储数据的应用程序,不需要特殊配置。如果应用程序需要使用不同的字符集或排序规则存储数据,可以通过多种方式配置字符集信息:For applications that store data using the default MySQL character set and collation (utf8mb4, utf8mb4_0900_ai_ci), no special configuration should be needed. If applications require data storage using a different character set or collation, you can configure character set information several ways:

  • 为每个数据库指定字符设置。例如,使用一个数据库的应用程序可能使用默认的 utf8mb4,而使用另一个数据库的应用程序可能使用 sjis。Specify character settings per database. For example, applications that use one database might use the default of utf8mb4, whereas applications that use another database might use sjis.

  • 在服务器启动时指定字符设置。这将导致服务器对所有未做其他安排的应用程序使用给定的设置。Specify character settings at server startup. This causes the server to use the given settings for all applications that do not make other arrangements.

  • 如果您从源代码构建 MySQL,可以在配置时指定字符设置。这将导致服务器将给定的设置用作所有应用程序的默认值,而无需在服务器启动时指定它们。Specify character settings at configuration time, if you build MySQL from source. This causes the server to use the given settings as the defaults for all applications, without having to specify them at server startup.

当不同的应用程序需要不同的字符设置时,每个数据库的方法提供了很大的灵活性。如果大多数或所有应用程序使用相同的字符集,在服务器启动或配置时指定字符设置可能最为方便。When different applications require different character settings, the per-database technique provides a good deal of flexibility. If most or all applications use the same character set, specifying character settings at server startup or configuration time may be most convenient.

对于每个数据库或服务器启动的方法,这些设置控制数据存储的字符集。应用程序还必须告诉服务器在客户端/服务器通信中使用哪个字符集,如下述说明中所述。For the per-database or server-startup techniques, the settings control the character set for data storage. Applications must also tell the server which character set to use for client/server communications, as described in the following instructions.

此处显示的示例假设在特定情况下使用 latin1 字符集和 latin1_swedish_ci 校对规则,作为 utf8mb4 和 utf8mb4_0900_ai_ci 默认设置的替代方案。The examples shown here assume use of the latin1 character set and latin1_swedish_ci collation in particular contexts as an alternative to the defaults of utf8mb4 and utf8mb4_0900_ai_ci.

  • 为每个数据库指定字符设置。 要创建一个数据库,使其表在数据存储时使用给定的默认字符集和校对规则,请使用如下 CREATE DATABASE 语句:Specify character settings per database.  To create a database such that its tables use a given default character set and collation for data storage, use a CREATE DATABASE statement like this:

    CREATE DATABASE mydb
      CHARACTER SET latin1
      COLLATE latin1_swedish_ci;
    

    在该数据库中创建的表在任何字符列上默认使用 latin1 和 latin1_swedish_ci。Tables created in the database use latin1 and latin1_swedish_ci by default for any character columns.

    使用数据库的应用程序每次连接时也应该配置它们的服务器连接。这可以通过在连接后执行 SET NAMES 'latin1' 语句来完成。该语句可以在任何连接方式下使用(无论是 mysql 客户端、PHP 脚本等)。Applications that use the database should also configure their connection to the server each time they connect. This can be done by executing a SET NAMES 'latin1' statement after connecting. The statement can be used regardless of connection method (the mysql client, PHP scripts, and so forth).

    在某些情况下,可能可以通过其他方式配置连接以使用所需的字符集。例如,要使用 mysql 连接,可以指定--default-character-set=latin1 命令行选项,以实现与 SET NAMES 'latin1' 相同的效果。In some cases, it may be possible to configure the connection to use the desired character set some other way. For example, to connect using mysql, you can specify the --default-character-set=latin1 command-line option to achieve the same effect as SET NAMES 'latin1'.

    有关配置客户端连接的更多信息,请参见 第 12.4 节,“连接字符集和校对规则”。For more information about configuring client connections, see Section 12.4, “Connection Character Sets and Collations”.

    Note

    如果你使用 ALTER DATABASE 来更改数据库默认字符集或校对规则,那么数据库中使用这些默认设置的现有存储例程必须删除并重新创建,以便它们使用新的默认设置。(在存储例程中,如果未明确指定字符集或校对规则,字符数据类型的变量将使用数据库默认值。参见 第 15.1.17 节,“CREATE PROCEDURE 和 CREATE FUNCTION 语句”。)If you use ALTER DATABASE to change the database default character set or collation, existing stored routines in the database that use those defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly. See Section 15.1.17, “CREATE PROCEDURE and CREATE FUNCTION Statements”.)

  • 在服务器启动时指定字符设置。 要在服务器启动时选择字符集和排序规则,请使用 --character-set-server 和 --collation-server 选项。例如,要在选项文件中指定这些选项,请包含以下行:Specify character settings at server startup.  To select a character set and collation at server startup, use the --character-set-server and --collation-server options. For example, to specify the options in an option file, include these lines:

    [mysqld]
    character-set-server=latin1
    collation-server=latin1_swedish_ci
    

    这些设置适用于整个服务器,并作为由任何应用程序创建的数据库以及在这些数据库中创建的表的默认设置。These settings apply server-wide and apply as the defaults for databases created by any application, and for tables created in those databases.

    应用程序在连接后仍然需要使用SET NAMES或等效方法配置其连接,如前面所述。您可能会想通过--init_connect="SET NAMES 'latin1'"选项启动服务器,以便在每个客户端连接时自动执行SET NAMES。然而,这可能会产生不一致的结果,因为具有CONNECTION_ADMIN权限(或已弃用的SUPER权限)的用户不会执行init_connect的值。It is still necessary for applications to configure their connection using SET NAMES or equivalent after they connect, as described previously. You might be tempted to start the server with the --init_connect="SET NAMES 'latin1'" option to cause SET NAMES to be executed automatically for each client that connects. However, this may yield inconsistent results because the init_connect value is not executed for users who have the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege).

  • 在 MySQL 配置时指定字符设置。 如果您从源码配置并构建 MySQL,要选择字符集和排序规则,请使用 DEFAULT_CHARSET 和 DEFAULT_COLLATION CMake 选项:Specify character settings at MySQL configuration time.  To select a character set and collation if you configure and build MySQL from source, use the DEFAULT_CHARSET and DEFAULT_COLLATION CMake options:

    cmake . -DDEFAULT_CHARSET=latin1 \
      -DDEFAULT_COLLATION=latin1_swedish_ci
    

    生成的服务器使用 latin1 和 latin1_swedish_ci 作为数据库和表以及客户端连接的默认值。在服务器启动时无需使用 --character-set-server 和 --collation-server 来指定这些默认值。应用程序在连接到服务器后也无需使用 SET NAMES 或等效命令来配置其连接。The resulting server uses latin1 and latin1_swedish_ci as the default for databases and tables and for client connections. It is unnecessary to use --character-set-server and --collation-server to specify those defaults at server startup. It is also unnecessary for applications to configure their connection using SET NAMES or equivalent after they connect to the server.

无论你如何为应用程序使用配置 MySQL 字符集,你也必须考虑这些应用程序运行的环境。例如,如果你打算使用从编辑器创建的文件中获取的 UTF-8 文本发送语句,你应该在环境的区域设置为 UTF-8 时编辑该文件,以确保文件编码正确,并且操作系统能够正确处理它。如果你在终端窗口中使用 mysql 客户端,则窗口必须配置为使用 UTF-8,否则字符可能无法正确显示。对于在 Web 环境中执行的脚本,脚本必须正确处理与 MySQL 服务器交互的字符编码,并且必须生成正确标明编码的页面,以便浏览器知道如何显示页面内容。例如,你可以在 <head> 元素中包含此 <meta> 标签:Regardless of how you configure the MySQL character set for application use, you must also consider the environment within which those applications execute. For example, if you intend to send statements using UTF-8 text taken from a file that you create in an editor, you should edit the file with the locale of your environment set to UTF-8 so that the file encoding is correct and so that the operating system handles it correctly. If you use the mysql client from within a terminal window, the window must be configured to use UTF-8 or characters may not display properly. For a script that executes in a Web environment, the script must handle character encoding properly for its interaction with the MySQL server, and it must generate pages that correctly indicate the encoding so that browsers know how to display the content of the pages. For example, you can include this <meta> tag within your <head> element:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />