mysql 修改字符集编码为utf8mb4

时间:2023-01-11 13:06:02

参考文章:http://www.cnblogs.com/shihaiming/p/5855616.html

1.修改配置文件my.cnf如下所示:

[client] 
default-character-set = utf8mb4 
[mysql] 
default-character-set = utf8mb4 
[mysqld] 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin 
init_connect='SET NAMES utf8mb4'

发现如下所示:只有character_set_database和character_set_server如我们所想,默认为utf8mb4

mysql> show variables like 'char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8mb4                          |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | utf8mb4                          |
| character_set_system     | utf8                             


2.正确的修改方法如下所示,需要把红色字体的两个字符正确添加后,才能修改成功

[client] 
default-character-set = utf8mb4 
[mysql] 
default-character-set = utf8mb4 
[mysqld] 
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'

重新启动后,如下所示:

+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8mb4                             |
| character_set_connection | utf8mb4                             |
| character_set_database   | utf8mb4                          |
| character_set_filesystem | binary                           |
| character_set_results    | utf8mb4                             |
| character_set_server     | utf8mb4                          |
| character_set_system     | utf8                             |