注:关于 utf8mb4 字符集,请参考 MySQL 官方文档(http://dev.mysql.com/doc/refman/5.6/en/charset-unicode-utf8mb4.html)
对于 JDBC 连接,需要使用 MySQL Connector/J 5.1.13(含)以上的版本。
JDBC 的连接串中,建议不配置 characterEncoding 选项。
注:关于 MySQL Connector/J 5.1.13,请参考 MySQL 官方 Release Notes(http://dev.mysql.com/doc/relnotes/connector-j/5.1/en/news-5-1-13.html)
[client]
default-character-set = utf8mb4
default-character-set = utf8mb4
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
Step 4. 设置字段的字符集为 utf8mb4
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
c、如果是Java程序,mysql-connection.jar版本高于5.1.13,否则仍然不能试用utf8mb4;
查看服务器字符集设置
mysql> SHOW VARIABLES WHERE Variable_name LIKE
'character%'
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 |
+--------------------------+--------------------+
查看表字符集
查看列字符集
执行SQL时,有时会报错:ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
报错原因在于:
MySQL Innodb 的索引长度限制为767字节,
utf8编码最多占3个字节,对于varchar(255)这样的数据库结构,所以255*3=765,没有超出767的限制。
转载自: