Mycat (3) temporary solution to UTF8MB4 coding problem

Source: Internet
Author: User
Tags finally block

This text connection: http://blog.csdn.net/freewebsys/article/details/45537411 reprint Please specify the source!

1, about UTF8MB4

UTF8MB4 is a superset of UTF8
UTF8MB4 is compatible with UTF8 and can represent more characters than UTF8.
Emoji is an emoticon; meaning is derived from Japanese (えもじ,e-moji,moji is a character in Japanese)
Emoji is now widely used in mobile phone text messages and web chat software.
Emoji emoji, in foreign mobile phone messages inside is already very popular use of an expression.

Support UTF8MB4 in 2,mycat

Go a lot more detours. Theoretically as long as the Server.xml charset set, but not so simple. The connection is not directly connected.

The same configuration UTF8 GBK is possible, but the sense of UTF8MB4 this code is too special, mycat judgment may have a bug.

The ultimate must kill: Modify the source code, org.opencloudb.mysql.nio.mysqlconnection:450 line.

 private  static  commandpacket getcharsetcommand  (int  ci) {String charset = Charsetutil.getcharset (CI); StringBuilder s = new  StringBuilder (); Logger.info ( "################## mysqlconnection Getcharsetcommand:"  +ci+ "\t|"  +charset); S.append ( "SET names utf8mb4" ); //.append (charset);  Commandpacket cmd = new  commandpacket (); Cmd.packetid = 0 ; Cmd.command = Mysqlpacket.com_query; Cmd.arg = S.tostring (). GetBytes (); return  cmd; }

Workaround, write the encoding of the dead connection settings directly. Of course, the MySQL server also need to modify the code.
Vi/etc/my.cnf

[client]default-character-set = utf8mb4[mysql]default-character-set = utf8mb4[mysqld]character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_unicode_ciinit_connect=‘SET NAMES utf8mb4‘

Client View encoding

SHOW VARIABLES LIKE ‘character%‘+--------------------------+--------------------+| 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              |+--------------------------+--------------------+rows in set (0.00 sec)

JDBC Modifies the connection:

jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUEjdbc.username=rootjdbc.password=password

Modifying Data tables: (Database and table field modifications)

 ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4 _unicode_ci; ALTER TABLE table_name CONVERT  to CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; ALTER TABLE table_name Change column_name VARCHAR(191) CHARACTER  SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Http://www.tutorialspoint.com/jdbc/jdbc-select-records.htm
Test Connection Database View character encoding:

//step 1. Import Required PackagesImport java.sql.*; Public classJdbcexample {//JDBC driver name and database URL   StaticFinal String Jdbc_driver ="Com.mysql.jdbc.Driver";StaticFinal String Db_url ="Jdbc:mysql://localhost/students";//Database credentials   StaticFinal String USER ="username";StaticFinal String PASS ="Password"; Public Static void Main(string[] args) {Connection conn =NULL; Statement stmt =NULL;Try{//step 2:register JDBC driverClass.forName ("Com.mysql.jdbc.Driver");//step 3:open a connectionSystem. out. println ("Connecting to a selected database ...");      conn = Drivermanager.getconnection (Db_url, USER, PASS); System. out. println ("Connected database successfully ...");//step 4:execute a querySystem. out. println ("Creating statement ...");      stmt = Conn.createstatement (); String sql ="Show variables like ' character% '"; ResultSet rs = stmt.executequery (SQL);//step 5:extract data from result set       while(Rs.next ()) {//display ValuesSystem. out. Print (Rs.getstring (1)); System. out. Print ("\ T"); System. out. Print (Rs.getstring (2)); System. out. println ();   } rs.close (); }Catch(SQLException se) {//handle errors for JDBCSe.printstacktrace (); }Catch(Exception e) {//handle errors for Class.forNameE.printstacktrace (); }finally{//finally block used to close resources      Try{if(stmt!=NULL) Conn.close (); }Catch(SQLException se) {      }// do nothing      Try{if(conn!=NULL) Conn.close (); }Catch(SQLException se)      {Se.printstacktrace (); }//end finally try}//end TrySystem. out. println ("goodbye!");}//end Main}//end jdbcexample

If you return to UTF8MB4:

character_set_client    utf8mb4character_set_connection    utf8mb4character_set_database  utf8mb4character_set_filesystem    binarycharacter_set_results   utf8mb4character_set_server    utf8mb4character_set_system    utf8character_sets_dir  /usr/share/mysql/charsets/

More about MySQL character set: http://www.laruence.com/2008/01/05/12.html

3, Summary:

This text connection: http://blog.csdn.net/freewebsys/article/details/45537411 reprint Please specify the source!

It is important to note that only MySQL 5.5 supports the UTF8MB4 character set.
CentOS installation MySQL 5.5 reference: http://stackoverflow.com/questions/9361720/update-mysql-version-from-5-1-to-5-5-in-centos-6-2

Mycat design is very good, but also very stable. The benefit of open source is that you can study the code yourself. Very happy.

Mycat (3) temporary solution to UTF8MB4 coding 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.