MySQL with JPA: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

前端 未结 1 1961
暖寄归人
暖寄归人 2020-12-18 03:23

I need to be able to store characters like \\xF0\\x9F\\x94\\xA5 in my database, which, according to this post need UTF8mb4 encoding.

So I

相关标签:
1条回答
  • 2020-12-18 04:17

    You do not need any change in the Java side, as utf8mb4 is just UTF-8 in Java.

    Instead, as you can see here:

    show variables WHERE variable_name like "col%";
    +----------------------+------------------+
    | Variable_name        | Value            |
    +----------------------+------------------+
    | collation_connection | utf8_general_ci  |
    | collation_database   | utf32_general_ci |
    | collation_server     | utf8_general_ci  |
    +----------------------+------------------+
    

    your connection setting is still utf8_general_ci; to set it at the connection level, one option is to execute the (mysql specific) query:

    SET NAMES='utf8mb4'
    

    before any attempt to use the utf8mb4 collation; or, generally for the mysql server, in /etc/my.cnf:

    [mysql]
    default-character-set = utf8mb4
    
    [mysqld]
    character-set-server = utf8mb4
    collation-server = utf8mb4_unicode_ci
    

    Another option without changing the connection string, is using a jdbc driver version >= 5.1.13: http://www.opensubscriber.com/message/java@lists.mysql.com/14151747.html

    0 讨论(0)
提交回复
热议问题