MYSQL:UTF8MB4 's Problem

Source: Internet
Author: User

And the QQ said there are many Martian and emoji expression pictures, these data, if directly insert to MySQL database, general will error, set to UTF8 are not good, must be changed to UTF8MB4 code, the difference between the two see: MySQL utf8mb4 and emoji expression , most of the online solution is to modify the MY.CNF parameters, set the MySQL encoding for UTF8MB4, although this method is thorough, but usually to restart MySQL, will cause the production system temporary machine.

The following are the less-affected processing methods:

Premise: MySQL version can not be too low, less than 5.5.3 version does not support UTF8MB4 encoding.

The corresponding field in the table, such as the name field of the membership table, whose character set is modified to UTF8MB4.

Then in the Java client, upgrade Mysql-connector-java to the latest version (currently the latest version is 5.1.37), and finally modify the configuration of the Druid data source, add a line:

<name= "Connectioninitsqls"  value= "Set names utf8mb4;" />

Complete configuration reference below:

    <BeanID= "DataSource"class= "Com.alibaba.druid.pool.DruidDataSource"Init-method= "Init"Destroy-method= "Close">        < Propertyname= "Driverclassname"value= "${jdbc-driver}"/>        < Propertyname= "url"value= "${jdbc-url}"/>        < Propertyname= "username"value= "${jdbc-user}"/>        < Propertyname= "Password"value= "${jdbc-password}"/>        < Propertyname= "Filters"value= "Stat"/>        < Propertyname= "Maxactive"value= " the"/>        < Propertyname= "InitialSize"value= "1"/>        < Propertyname= "Maxwait"value= "60000"/>        < Propertyname= "Minidle"value= "1"/>        < Propertyname= "Timebetweenevictionrunsmillis"value= " the"/>        < Propertyname= "Minevictableidletimemillis"value= "300000"/>        < Propertyname= "Validationquery"value= "Select ' X '"/>        < Propertyname= "Testwhileidle"value= "true"/>        < Propertyname= "Testonborrow"value= "false"/>        < Propertyname= "Testonreturn"value= "false"/>        < Propertyname= "Poolpreparedstatements"value= "true"/>        < Propertyname= "Maxpoolpreparedstatementperconnectionsize"value= " the"/>        < Propertyname= "Connectioninitsqls"value= "Set names utf8mb4;"/>    </Bean>

Basically should be OK, if not yet, check the JDBC connection string settings:

jdbc:mysql://localhost:3306/db name? Useunicode=true&characterencoding=utf8

If it doesn't work, remove the last Useunicode=true&characterencoding=utf8.

MYSQL:UTF8MB4 's Problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.