如何在 MySQL 数据库中支持完整的 Unicode utf8mb4
怎样修改MYSQL数据库、表和列的编码为utf8mb4
一、 数据库database改成 utf8mb4:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
二、 表结构table改成 utf8mb4:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
三、 列字段field改成 utf8mb4:
下面的SQL根据自己的表结构设置,这个示例只针对varchar()
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
四、 其它,
1. 修改前先备份数据库。安全第一!
2. utf8mb4 需要MySQL版本v5.53以上。查版本命令
SELECT VERSION();
3. 然后才修改数据库、表、列编码。
4. 查看服务器字符集配置
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 |
+--------------------------+--------------------+
10 rows in set (0.00 sec)
参考来源: https://mathiasbynens.be/notes/mysql-utf8mb4