MySQL 4字节utf8字符更新失败一例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: MySQL 4字节utf8字符更新失败一例 业务的小伙伴反映了下面的问题 问题 有一个4字节的utf8字符'????'插入到MySQL数据库中时报错 java.sql.SQLException: Incorrect string value: '\xF0\xA0\x99\xB6' for column 'c_utf8mb4' at row 1 数据库中存放该字符的列已经定义为utf8mb4编码了,但相关的参数character_set_server的值为utf8。

MySQL 4字节utf8字符更新失败一例

业务的小伙伴反映了下面的问题

问题

有一个4字节的utf8字符'????'插入到MySQL数据库中时报错

java.sql.SQLException: Incorrect string value: '\xF0\xA0\x99\xB6' for column 'c_utf8mb4' at row 1 

数据库中存放该字符的列已经定义为utf8mb4编码了,但相关的参数character_set_server的值为utf8。 比较奇怪的是使用mysql-connector-java-5.1.15.jar驱动时没有问题,使用更高版本的驱动如mysql-connector-java-5.1.22.jar,就会出错。JDBC的下面2个连接参数,不过设置与否,都没有影响。

  • characterEncoding=utf8
  • useUnicode=true

原因

jdbc驱动未正确设置SET NAMES utf8mb4导致转码错误。

根据MySQL官方手册,在MySQL Jdbc中正确使用4字节UTF8字符的方法如下:

http://dev.mysql.com/doc/relnotes/connector-j/5.1/en/news-5-1-14.html:

Connector/J mapped both 3-byte and 4-byte UTF8 encodings to the same Java UTF8 encoding.

To use 3-byte UTF8 with Connector/J set characterEncoding=utf8 and set useUnicode=true in the connection string.

To use 4-byte UTF8 with Connector/J configure the MySQL server with character_set_server=utf8mb4. Connector/J will then use that setting as long as characterEncoding has not been set in the connection string. This is equivalent to autodetection of the character set. (Bug #58232) 

按照MySQL官方手册提供的方法,MySQL JDBC驱动内部会在建立连接时发送SET NAMES utf8mb4给服务端,确保正确进行字符编码。 所以,本问题属于应用未按要求使用MySQL JDBC。但5.1.15可以插入4字节字符也是比较奇怪的事情。 mysql-connector官网的 change log中并且提交5.1.15~5.1.22之间有相关的改动。但是,通过比较代码发现,这部分逻辑确实发生了变更。

5.1.15

com\mysql\jdbc\ConnectionImpl.java:

private boolean configureClientCharacterSet(boolean dontCheckServerMatch)
    throws SQLException
{
...
    if(getEncoding() != null)
    {
        String mysqlEncodingName = CharsetMapping.getMysqlEncodingForJavaEncoding(getEncoding().toUpperCase(Locale.ENGLISH), this);
        if(getUseOldUTF8Behavior())
            mysqlEncodingName = "latin1";
        if(dontCheckServerMatch || !characterSetNamesMatches(mysqlEncodingName))
            execSQL(null, (new StringBuilder()).append("SET NAMES ").append(mysqlEncodingName).toString(), -1, null, 1003, 1007, false, database, null, false);
        realJavaEncoding = getEncoding();
    }
...
} 

给CharsetMapping.getMysqlEncodingForJavaEncoding()传入的参数是UTF-8,对应的mysql的编码有2个,utf8和utf8mb4, 其中utf8mb4优先,所以这个函数返回的mysql编码是utf8mb4。即之后执行了SET NAMES utf8mb4

相关代码:

com\mysql\jdbc\CharsetMapping.java:

public static final String getMysqlEncodingForJavaEncoding(String javaEncodingUC, Connection conn)
    throws SQLException
{
    List mysqlEncodings = (List)JAVA_UC_TO_MYSQL_CHARSET_MAP.get(javaEncodingUC);
    if(mysqlEncodings != null)
    {
        Iterator iter = mysqlEncodings.iterator();
        VersionedStringProperty versionedProp = null;
        do
        {
            if(!iter.hasNext())
                break;
            VersionedStringProperty propToCheck = (VersionedStringProperty)iter.next();
            if(conn == null)
                return propToCheck.toString();
            if(versionedProp != null && !versionedProp.preferredValue && versionedProp.majorVersion == propToCheck.majorVersion && versionedProp.minorVersion == propToCheck.minorVersion && versionedProp.subminorVersion == propToCheck.subminorVersion)
                return versionedProp.toString();
            if(!propToCheck.isOkayForVersion(conn))
                break;
            if(propToCheck.preferredValue)
                return propToCheck.toString();
            versionedProp = propToCheck;
        } while(true);
        if(versionedProp != null)
            return versionedProp.toString();
    }
    return null;
}

...

CHARSET_CONFIG.setProperty("javaToMysqlMappings", "US-ASCII =\t\t\tusa7,US-ASCII =\t\t\t>4.1.0 ascii,...
UTF-8 = \t\tutf8,UTF-8 =\t\t\t\t*> 5.5.2 utf8mb4,..."); 

注:上面的定义UTF-8 =\t\t\t\t*> 5.5.2 utf8mb4中的*代表有多个mysql编码对应于同一个Java编码时,该编码优先

5.1.22

com\mysql\jdbc\ConnectionImpl.java:

private boolean configureClientCharacterSet(boolean dontCheckServerMatch)
    throws SQLException
{
...
     if(getEncoding() != null)
    {
        String mysqlEncodingName = getServerCharacterEncoding();
        if(getUseOldUTF8Behavior())
            mysqlEncodingName = "latin1";
        boolean ucs2 = false;
        if("ucs2".equalsIgnoreCase(mysqlEncodingName) || "utf16".equalsIgnoreCase(mysqlEncodingName) || "utf32".equalsIgnoreCase(mysqlEncodingName))
        {
            mysqlEncodingName = "utf8";
            ucs2 = true;
            if(getCharacterSetResults() == null)
                setCharacterSetResults("UTF-8");
        }
        if(dontCheckServerMatch || !characterSetNamesMatches(mysqlEncodingName) || ucs2)
            execSQL(null, (new StringBuilder()).append("SET NAMES ").append(mysqlEncodingName).toString(), -1, null, 1003, 1007, false, database, null, false);
        realJavaEncoding = getEncoding();
    }
...
}

...
public String getServerCharacterEncoding()
{
    if(io.versionMeetsMinimum(4, 1, 0))
    {
        String charset = (String)indexToCustomMysqlCharset.get(Integer.valueOf(io.serverCharsetIndex));
        if(charset == null)
            charset = (String)CharsetMapping.STATIC_INDEX_TO_MYSQL_CHARSET_MAP.get(Integer.valueOf(io.serverCharsetIndex));
        return charset == null ? (String)serverVariables.get("character_set_server") : charset;
    } else
    {
        return (String)serverVariables.get("character_set");
    }
} 

解决办法

一直使用旧版的5.1.15驱动不是一个好办法,因此在使用新版驱动时,采取以下措施之一解决这个问题。

  1. 参考官网的说明,修改my.cnf

    character_set_server=utf8mb4 
  2. 在应用中获取连接后执行下面的SQL

    stmt.executeUpdate("set names utf8mb4")

补充

 根据5.1.22的MySQL JDBC驱动代码,MySQL JDBC支持utf8mb4需要满足以下2个条件 
 
1. MySQL系统变量`character_set_server`的值为utf8mb4  
2. MySQL JDBC连接参数characterEncoding的值为以下值之一    
     - null
     - UTF8
     - UTF-8 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4月前
|
存储 关系型数据库 MySQL
Mysql中utf8和utf8mb4区别
Mysql中utf8和utf8mb4区别
44 0
|
2月前
|
安全 关系型数据库 MySQL
MySQL注入 -- 宽字节注入
MySQL注入 -- 宽字节注入
18 0
|
4月前
|
存储 关系型数据库 MySQL
为什么不建议在MySQL中使用 utf8?
MySQL 字符编码集中有两套 UTF-8 编码实现:utf8 和 utf8mb4。 如果使用 utf8 的话,存储 emoji 符号和一些比较复杂的汉字、繁体字就会出错。
|
7月前
|
JSON 关系型数据库 MySQL
MySQL的字符转义
MySQL的字符转义
41 0
|
7月前
|
大数据 关系型数据库 MySQL
字节、腾讯争先部署,ClickHouse+Doris 赶超 MySQL 810 倍
阿里流传着这样一句话,“一切业务数据化,一切数据业务化”。
157 0
|
8月前
|
关系型数据库 MySQL Linux
mysql 8中utf8和utf8mb4运行时的编码警告提醒[Warning]——my.ini配置问题
mysql 8中utf8和utf8mb4运行时的编码警告提醒[Warning]——my.ini配置问题
|
8月前
|
关系型数据库 MySQL 数据库
60【MySQL】MySQL存不了字符引发的折腾
【MySQL】MySQL存不了字符引发的折腾
36 0
|
8月前
|
存储 关系型数据库 MySQL
Mysql中utf8和utf8mb4区别
Mysql中utf8和utf8mb4区别
80 0
|
9月前
|
存储 关系型数据库 MySQL
MySQL中字段类型存储需要多少字节
MySQL中字段类型存储需要多少字节
46 0
|
9月前
|
存储 关系型数据库 MySQL
MySQL中字符、字节、位之间的关系
MySQL中字符、字节、位之间的关系
55 0