首批通过分布式安全可靠测评,为关键业务系统打造
illegal mix of collations 报错处理
更新时间:2026-05-18 09:11
问题描述
应用执行 SQL 报如下错误。
### Error querying database. Cause: com.dangdang.ddframe.rdb.sharding.exception.ShardingJdbcException: java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='
适用版本
JDBC 2.x 版本以上。
问题原因
这个问题出现的前提是表的字段已经是 utf8mb4_general_ci,但是由于 MySQL 的连接采用的是 utf8_general_ci,所以一对比的时候就会出错,特别是字符串包含 emoji 的时候。
解决方法
修改连接时的字符集。
db.connect()
db.execute_sql("SET collation_connection = 'utf8mb4_general_ci';")