MySQL 服务器有一个编译时默认的字符集和校对规则。要更改这些默认值,请在启动服务器时使用 --character-set-server 和 --collation-server 选项。请参阅 第 7.1.7 节,“服务器命令选项”。校对规则必须是默认字符集的合法校对规则。要确定每个字符集可用的校对规则,请使用 SHOW COLLATION 语句或查询 INFORMATION_SCHEMA COLLATIONS 表。The MySQL server has a compiled-in default character set and
collation. To change these defaults, use the
--character-set-server and
--collation-server options when you
start the server. See Section 7.1.7, “Server Command Options”. The
collation must be a legal collation for the default character set.
To determine which collations are available for each character
set, use the SHOW COLLATION
statement or query the INFORMATION_SCHEMA
COLLATIONS table.
如果你尝试使用未编译到二进制文件中的字符集,你可能会遇到以下问题:If you try to use a character set that is not compiled into your binary, you might run into the following problems:
如果您的程序使用了错误的路径来确定字符集的存储位置(通常是在 MySQL 安装目录下的
share/mysql/charsets或share/charsets目录),可以通过在运行程序时使用--character-sets-dir选项来修复。例如,要为 MySQL 客户端程序指定使用的目录,可以在您的选项文件的[client]组中列出它。这里给出的示例显示了在 Unix 或 Windows 下该设置可能的样子:If your program uses an incorrect path to determine where the character sets are stored (which is typically theshare/mysql/charsetsorshare/charsetsdirectory under the MySQL installation directory), this can be fixed by using the--character-sets-diroption when you run the program. For example, to specify a directory to be used by MySQL client programs, list it in the[client]group of your option file. The examples given here show what the setting might look like for Unix or Windows, respectively:[client] character-sets-dir=/usr/local/mysql/share/mysql/charsets [client] character-sets-dir="C:/Program Files/MySQL/MySQL Server 8.4/share/charsets"
如果字符集是无法动态加载的复杂字符集,则必须重新编译程序以支持该字符集。If the character set is a complex character set that cannot be loaded dynamically, you must recompile the program with support for the character set.
对于 Unicode 字符集,您可以使用 LDML 表示法在无需重新编译的情况下定义排序规则。请参阅 第 12.14.4 节,“向 Unicode 字符集添加 UCA 排序规则”。For Unicode character sets, you can define collations without recompiling by using LDML notation. See Section 12.14.4, “Adding a UCA Collation to a Unicode Character Set”.
如果字符集是动态字符集,但您没有它的配置文件,则应从新的 MySQL 发行版中安装该字符集的配置文件。If the character set is a dynamic character set, but you do not have a configuration file for it, you should install the configuration file for the character set from a new MySQL distribution.
如果你的字符集索引文件(
Index.xml)不包含字符集的名称,你的程序会显示一个错误信息:If your character set index file (Index.xml) does not contain the name for the character set, your program displays an error message:Character set '
charset_name' is not a compiled character set and is not specified in the '/usr/share/mysql/charsets/Index.xml' file要解决此问题,您应该获取一个新的索引文件,或手动将任何缺失字符集的名称添加到当前文件中。To solve this problem, you should either get a new index file or manually add the name of any missing character sets to the current file.
您可以按如下方式强制客户端程序使用特定字符集:You can force client programs to use specific character set as follows:
[client]
default-character-set=charset_name
这通常是不必要的。然而,当 character_set_system 与 character_set_server 或 character_set_client 不同时,如果你手动输入字符(作为数据库对象标识符、列值或两者),这些字符可能在客户端输出中显示不正确,或者输出本身可能格式不正确。在这种情况下,通过 --default-character-set= 启动 mysql 客户端——即将客户端字符集设置为与系统字符集匹配——应该可以解决问题。This is normally unnecessary. However, when
system_character_setcharacter_set_system differs from
character_set_server or
character_set_client, and you
input characters manually (as database object identifiers, column
values, or both), these may be displayed incorrectly in output
from the client or the output itself may be formatted incorrectly.
In such cases, starting the mysql client with
--default-character-set=—that
is, setting the client character set to match the system character
set—should fix the problem.system_character_set