当前位置: 首页 > 工具软件 > rails_emoji > 使用案例 >

MySQL utf8mb4 字符集,用于存储emoji表情

慕容安易
2023-12-01
采用修改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
参考文献:
http://blog.arkency.com/2015/05/how-to-store-emoji-in-a-rails-app-with-a-mysql-database/

来自:
http://blog.csdn.net/youngqj/article/details/52040865

 类似资料: