mysql存儲(chǔ)emoji表情報(bào)錯(cuò)的處理方法【更改編碼為utf8mb4】
本文實(shí)例分析了mysql存儲(chǔ)emoji表情報(bào)錯(cuò)的處理方法。分享給大家供大家參考,具體如下:
utf-8編碼可能2個(gè)字節(jié)、3個(gè)字節(jié)、4個(gè)字節(jié)的字符,但是MySQL的utf8編碼只支持3字節(jié)的數(shù)據(jù),而移動(dòng)端的表情數(shù)據(jù)是4個(gè)字節(jié)的字符。如果直接往采用utf-8編碼的數(shù)據(jù)庫中插入表情數(shù)據(jù),Java程序中將報(bào)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)
可以對(duì)4字節(jié)的字符進(jìn)行編碼存儲(chǔ),然后取出來的時(shí)候,再進(jìn)行解碼。但是這樣做會(huì)使得任何使用該字符的地方都要進(jìn)行編碼與解碼。
utf8mb4編碼是utf8編碼的超集,兼容utf8,并且能存儲(chǔ)4字節(jié)的表情字符。
采用utf8mb4編碼的好處是:存儲(chǔ)與獲取數(shù)據(jù)的時(shí)候,不用再考慮表情字符的編碼與解碼問題。
更改數(shù)據(jù)庫的編碼為utf8mb4:
1. MySQL的版本
utf8mb4的最低mysql版本支持版本為5.5.3+,若不是,請(qǐng)升級(jí)到較新版本。
2. MySQL驅(qū)動(dòng)
5.1.34可用,最低不能低于5.1.13
3.修改MySQL配置文件
修改mysql配置文件my.cnf(windows為my.ini)
my.cnf一般在etc/mysql/my.cnf位置。找到后請(qǐng)?jiān)谝韵氯糠掷锾砑尤缦聝?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)部操作字符集) |
這幾個(gè)變量必須是utf8mb4。
5. 數(shù)據(jù)庫連接的配置
數(shù)據(jù)庫連接參數(shù)中:
characterEncoding=utf8
會(huì)被自動(dòng)識(shí)別為utf8mb4,也可以不加這個(gè)參數(shù),會(huì)自動(dòng)檢測。
而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存儲(chǔ)過程技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
相關(guān)文章
mysql下怎樣運(yùn)行腳本以運(yùn)行niuzi.sql為例
mysql下運(yùn)行腳本,有兩種方法,都是在命令行下進(jìn)行的,需要的朋友可以記錄下2014-07-07

mysql啟動(dòng)報(bào)錯(cuò)Failed?to?start?LSB:start?and?stop?MySQL的問題解決

MYSQL時(shí)區(qū)導(dǎo)致時(shí)間差了14或13小時(shí)的解決方法

windows?64位下mysql8.0.25安裝配置教程(最詳細(xì)!)

mysql 數(shù)據(jù)庫取前后幾秒 幾分鐘 幾小時(shí) 幾天的語句

MySQL隱式類型轉(zhuǎn)換導(dǎo)致索引失效的解決