mysql utf8mb4 & utf8

utf8mb4 mb4most bytes 4

最近在做微信相关的项目,其中MySQL 要存储emoji表情,因此发现我们常用的utf8 字符集根本无法存储表情。网上有不少替代方案。本人还是采用了修改mysql字符集的方案简单快捷。
首先将我们数据库默认字符集由utf8 更改为utf8mb4,对应的表默认字符集也更改为utf8mb4 已经存储表情的字段默认字符集也做了相应的调整。
SQL 语句如:

修改数据库:

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;

其次修改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'  

最后检查环境变量 和测试 SQL 如下:

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)  

说明:MySQL版本必须为5.5.3以上版本,否则不支持字符集utf8mb4


升级原因 MySQL默认的utf8只支持三字节字符,不支持Emoji表情符(四字节),如果有四字节的字符写入会报错。从MySQL 5.5开始,提供了utf8mb4,支持四字节的字符。
许多使用iphone的用户来在填写昵称的时候会加入Emoji表情符,如果MySQL没有使用utf8mb4字符集,这样的昵称信息就无法写入。
一、升级前。

mysql> SHOW VARIABLES LIKE 'character_set%';  

+--------------------------+--------------------------------------------------------------------+
| Variable_name            | Value                                                              |
+--------------------------+--------------------------------------------------------------------+
| character_set_client     | utf8                                                               |
| character_set_connection | utf8                                                               |
| character_set_database   | utf8                                                               |
| character_set_filesystem | binary                                                             |
| character_set_results    | utf8                                                               |
| character_set_server     | utf8                                                               |
| character_set_system     | utf8                                                               |
| character_sets_dir       | /home/sdkserver/local/mysql-5.5.43-linux2.6-x86_64/share/charsets/ |
+--------------------------+--------------------------------------------------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

二、修改配置文件。
打开MySQL的配置文件,将字符集的配置修改成如下:

[client]    
default-character-set = utf8mb4

[mysql]  
default-character-set = utf8mb4

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

三、升级后。

mysql> SHOW VARIABLES LIKE 'character_set%';

+--------------------------+--------------------------------------------------------------------+
| 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                                                               |
| character_sets_dir       | /home/nieyong/local/mysql-5.5.43/share/charsets/                   |
+--------------------------+--------------------------------------------------------------------+
8 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation%';

+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | utf8mb4_general_ci |
| collation_database   | utf8mb4_general_ci |
| collation_server     | utf8mb4_general_ci |
+----------------------+--------------------+
3 rows in set (0.00 sec)
  • qq_43638135
    妲己再美究为妃: 博主没有想过自己接一些私活干吗?我现在还没毕业,但是我也确实听说外挂市场自动化游戏脚本市场挺火热的,并且报酬也很丰厚,但是具体的我也不是很清楚,求解答。 (1个月前 #47楼) 查看回复(2) 举报 回复
    22