mysql存儲emoji表情報錯的處理方法【更改編碼為utf8mb4】
本文實例分析了mysql存儲emoji表情報錯的處理方法。分享給大家供大家參考,具體如下:
utf-8編碼可能2個字節(jié)、3個字節(jié)、4個字節(jié)的字符,但是MySQL的utf8編碼只支持3字節(jié)的數(shù)據(jù),而移動端的表情數(shù)據(jù)是4個字節(jié)的字符。如果直接往采用utf-8編碼的數(shù)據(jù)庫中插入表情數(shù)據(jù),Java程序中將報SQL異常:
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)
可以對4字節(jié)的字符進(jìn)行編碼存儲,然后取出來的時候,再進(jìn)行解碼。但是這樣做會使得任何使用該字符的地方都要進(jìn)行編碼與解碼。
utf8mb4編碼是utf8編碼的超集,兼容utf8,并且能存儲4字節(jié)的表情字符。
采用utf8mb4編碼的好處是:存儲與獲取數(shù)據(jù)的時候,不用再考慮表情字符的編碼與解碼問題。
更改數(shù)據(jù)庫的編碼為utf8mb4:
1. MySQL的版本
utf8mb4的最低mysql版本支持版本為5.5.3+,若不是,請升級到較新版本。
2. MySQL驅(qū)動
5.1.34可用,最低不能低于5.1.13
3.修改MySQL配置文件
修改mysql配置文件my.cnf(windows為my.ini)
my.cnf一般在etc/mysql/my.cnf位置。找到后請在以下三部分里添加如下內(nèi)容:
[client] default-character-set = utf8mb4 [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. 重啟數(shù)據(jù)庫,檢查變量
| 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 |
collation_connection 、collation_database 、collation_server是什么沒關(guān)系。
但必須保證
| 系統(tǒng)變量 | 描述 |
|---|---|
| character_set_client | (客戶端來源數(shù)據(jù)使用的字符集) |
| character_set_connection | (連接層字符集) |
| character_set_database | (當(dāng)前選中數(shù)據(jù)庫的默認(rèn)字符集) |
| character_set_results | (查詢結(jié)果字符集) |
| character_set_server | (默認(rèn)的內(nèi)部操作字符集) |
這幾個變量必須是utf8mb4。
5. 數(shù)據(jù)庫連接的配置
數(shù)據(jù)庫連接參數(shù)中:
characterEncoding=utf8會被自動識別為utf8mb4,也可以不加這個參數(shù),會自動檢測。
而autoReconnect=true是必須加上的。
6. 將數(shù)據(jù)庫和已經(jīng)建好的表也轉(zhuǎn)換成utf8mb4
更改數(shù)據(jù)庫編碼:
更改表編碼:
如有必要,還可以更改列的編碼
7、在第3步設(shè)置character_set_database,character_set_server不成功的可以試下直接在mysql.exe下
set @@character_set_server='utf8mb4'; set @@character_set_database='utf8mb4';
這下數(shù)據(jù)庫就可以存下emoji表情的編碼了。
附上我的my.ini
# 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 [mysqld] character-set-client-handshake = FALSE character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci init_connect='SET NAMES utf8mb4' # 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 # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # 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
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL存儲過程技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信