1. 程式人生 > >mysql儲存emoji表情報錯的處理方法【更改編碼為utf8mb4】

mysql儲存emoji表情報錯的處理方法【更改編碼為utf8mb4】

utf-8編碼可能2個位元組、3個位元組、4個位元組的字元,但是MySQL的utf8編碼只支援3位元組的資料,而移動端的表情資料是4個位元組的字元。如果直接往採用utf-8編碼的資料庫中插入表情資料,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位元組的字元進行編碼儲存,然後取出來的時候,再進行解碼。但是這樣做會使得任何使用該字元的地方都要進行編碼與解碼。

utf8mb4編碼是utf8編碼的超集,相容utf8,並且能儲存4位元組的表情字元。
採用utf8mb4編碼的好處是:儲存與獲取資料的時候,不用再考慮表情字元的編碼與解碼問題

更改資料庫的編碼為utf8mb4:

1. MySQL的版本

utf8mb4的最低mysql版本支援版本為5.5.3+,若不是,請升級到較新版本。

2. MySQL驅動

5.1.34可用,最低不能低於5.1.13

3.修改MySQL配置檔案

修改mysql配置檔案my.cnf(windows為my.ini)

my.cnf一般在etc/mysql/my.cnf位置。找到後請在以下三部分裡新增如下內容:

[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. 重啟資料庫,檢查變數

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

 

collation_connection 、collation_database 、collation_server是什麼沒關係。

但必須保證

 

系統變數 描述
character_set_client (客戶端來源資料使用的字符集)
character_set_connection (連線層字符集)
character_set_database (當前選中資料庫的預設字符集)
character_set_results (查詢結果字符集)
character_set_server (預設的內部操作字符集)

 

這幾個變數必須是utf8mb4。

5. 資料庫連線的配置

資料庫連線引數中:

characterEncoding=utf8會被自動識別為utf8mb4,也可以不加這個引數,會自動檢測。

autoReconnect=true是必須加上的。

6. 將資料庫和已經建好的表也轉換成utf8mb4

更改資料庫編碼:

ALTER DATABASE caitu99 CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

更改表編碼:

ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATEutf8mb4_general_ci;

如有必要,還可以更改列的編碼

7、在第3步設定character_set_database,character_set_server不成功的可以試下直接在mysql.exe下

set @@character_set_server='utf8mb4';
set @@character_set_database='utf8mb4';
# 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

這下資料庫就可以存下emoji表情的編碼了。

附上我的my.ini