在 MySQL 中使用 utf8mb4
为了在 MySQL (5.6.11) 中使用 4 字节 utf8mb4
,我在 my.ini
文件(my.cnf
未找到).此文件位于 Windows XP 上名为 Application Data
(C:Documents and SettingsAll UsersApplication DataMySQLMySQL Server 5.6
) 的隐藏文件夹中.安装目录下没有.
In order to use 4-byte utf8mb4
in MySQL (5.6.11), I have set the following variables in the my.ini
file (my.cnf
is not found). This file is located in a hidden folder named Application Data
(C:Documents and SettingsAll UsersApplication DataMySQLMySQL Server 5.6
) on Windows XP. It is not available under the installation directory.
[client]
port=3306
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init-connect='SET NAMES utf8mb4'
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
然后发出以下命令,
SHOW VARIABLES
WHERE Variable_name
LIKE 'character\_set\_%'
OR Variable_name LIKE 'collation%';
仍显示以下列表.
从图片本身可以看出,有几个变量仍然使用3字节的utf8
.
From the picture itself, it is clear that several variables are still using 3-byte utf8
.
在执行此操作之前,已发出以下命令对数据库本身进行相应更改.
Before doing this, the following command had already been issued to make corresponding changes to the database itself.
ALTER DATABASE database_name
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
并且还对所述数据库中的每个表发出了以下命令.
And the following command had also been issued on each and every table in the said database.
ALTER TABLE table_name
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
<小时>
然而,为什么某些变量尚未设置为所述字符集以及排序规则?缺少什么?
Nevertheless, what is the reason why some variables have not yet been set to the said character set as well as the collation? What is missing?
在执行上述指定的每个任务后,系统(操作系统)本身会重新启动.
The system (operating system) itself was restarted after every single task specified above had been carried out.
推荐答案
客户端 通常在连接时设置这些值.my.ini 中的设置只是在客户端未明确指定连接编码时应用的默认值.由于它们不可靠,每个客户端应该指定一个连接编码.由于您在那里获得了一些精美的屏幕截图,因此我猜您正在连接一些 GUI 实用程序,这些实用程序可能明确确实设置了一些连接编码.
The client usually sets these values when connecting. The settings in my.ini are merely defaults which apply when the client does not explicitly specify a connection encoding. Since they're unreliable, every client should specify a connection encoding. Since you've got some fancy screenshot there I'll guess that you're connecting with some GUI utility which probably explicitly does set some connection encodings.
设置连接字符集的PHP示例:
PHP example of setting a connection charset:
new PDO('mysql:host=localhost;charset=utf8mb4')
相关文章