java 微信暱稱emoji表情保存失敗 沒法存入數據庫(二)

參考連接:http://www.javashuo.com/article/p-fhfqfxxz-z.htmlhtml

mysql/Java服務端對emoji的支持

前言:

最近開發的iOS項目由於須要用戶文本的存儲,天然就遇到了emoji等表情符號如何被mysql DB支持的問題。困擾了很多天,在就要絕望放棄的邊緣,終於完成了轉換和遷移。在此特別分析和整理,方便更多人。java

 

問題描述:

若是UTF8字符集且是Java服務器的話,當存儲含有emoji表情時,會拋出相似以下異常:mysql

java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x94' for column 'name' at row 1  
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)  
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)  
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)  
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)  
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)  
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620)  
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662)  
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581)

這就是字符集不支持的異常。由於UTF-8編碼有多是兩個、三個、四個字節,其中Emoji表情是4個字節,而Mysql的utf8編碼最多3個字節,因此致使了數據插不進去。linux

升級前須要考慮的問題:

若是你的項目要進行移動產品的用戶文本的存儲,將你的DB字符集從UTF8/GBK等傳統字符集升級到utf8mb4將是勢在必行。你能夠經過應用層面轉換emoji等特殊字符,以達到原DB的兼容,我認爲可行,可是你可能走了彎路。sql

utf8mb4做爲utf8的super set,徹底向下兼容,因此不用擔憂字符的兼容性問題。切換中須要顧慮的主要影響是mysql須要從新啓動(雖然mysql官方文檔說能夠動態修改配置, 可是通過數次測試,仍是須要重啓纔可生效),對於業務可用率的影響是須要考慮的大問題,這裏就暫時不展開討論了。segmentfault

升級步驟:

1.utf8mb4的最低mysql版本支持版本爲5.5.3+,若不是,請升級到較新版本。windows

mysql版本查看命令請看:查看mysql版本的四種方法;mysql安裝步驟請看:Linux中升級Mysql到Mysql最新版本的方法
2.修改database、table和column字符集。參考如下語句:
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;
3.修改mysql配置文件my.cnf(windows爲my.ini)服務器

my.cnf通常在etc/mysql/my.cnf位置。找到後請在如下三部分裏添加以下內容:測試

[client]
default-character-set = utf8mb4ui

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

4.重啓 MySQL Server、檢查字符集

1.)重啓命令參考:/etc/init.d/mysql restart

2.)輸入命令:mysql,進入mysql命令行(若是提示沒權限,能夠嘗試輸入mysql -uroot -p你的密碼)

3.)在mysql命令行中輸入:SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';

檢查是否以下:

+--------------------------+--------------------+
| 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              |
| collation_connection    | utf8mb4_unicode_ci |
| collation_database      | utf8mb4_unicode_ci |
| collation_server        | utf8mb4_unicode_ci |
+--------------------------+--------------------+
rows in set (0.00 sec)

特別說明下:collation_connection/collation_database/collation_server若是是 utf8mb4_general_ci,沒有關係。但必須保證 character_set_client/character_set_connection/character_set_database /character_set_results/character_set_server爲utf8mb4。關於這些字符集配置是幹什麼用的,有什麼區 別,請參考:深刻Mysql字符集設置

5.若是你用的是java服務器,升級或確保你的mysql connector版本高於5.1.13,不然仍然沒法使用utf8mb4
這是mysql官方release note,你們能夠查看說明,並下載最新的mysql connector for java的jar包。
這裏爲你們提供一個:mysql-connector-java-5.1.31-bin.jar
同時記得修改pom配置哦~

6.檢查你服務端的db配置文件:

 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&autoReconnect=true&rewriteBatchedStatements=TRUE
jdbc.username=root
jdbc.password=password

 

特別說明其中的jdbc.url配置:autoReconnect配置我強烈建議配上,我以前就是忽略了這個屬性,致使由於緩 存緣故,沒有讀取到DB最新配置,致使一直沒法使用utf8mb4字符集,多麼痛的領悟!!

 

經本人實踐成功,如下是本人mysql的配置:

# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 character_set_connection = utf8mb4 [mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. log_bin = MySql-bin # These are commonly set, remove the # and set as required. basedir = "D:/softApp/mysql" datadir = "D:/softApp/mysqlData" character-set-server = utf8mb4 character-set-client-handshake = FALSE collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' port = 3306 server_id = MySql5.6 # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

相關文章
相關標籤/搜索