MySQL字符集修改为utf8mb4

MySQL要存储emoji表情,需要把以前的utf8修改为utf8mb4。

MySQL版本要大于5.5.3

通过的my.cnf修改MySQL的字符集

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

重启或重载mysql配置文件

重启后检查环境变量

mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';  
+--------------------------+--------------------+  
| 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               |  
| collation_connection     | utf8mb4_unicode_ci |  
| collation_database       | utf8mb4_unicode_ci |  
| collation_server         | utf8mb4_unicode_ci |  
+--------------------------+--------------------+  
 rows in set (0.00 sec)

修改数据库,表和字段的字符集

# 修改数据库:  
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;  
# 修改表:  
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;  
# 修改表字段:  
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

最后,在你的程序中也要使用utf8mb4连接数据库,修改mysql字符集使用配置文件,数据库或者字段建议使用客户端工具修改



Mjiong君

爱生活,爱技术,爱分享

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注