升級安裝5.6版本mysql
linux環境下的yum默認mysql版本是5.1的,由於項目需要保存表情等4個字節的數據,版本受限,需要升級到5.6版本支持utf8mb4格式的編碼。
升級過程大概就是備份數據庫,完全卸載舊版本,重新安裝5.6版本,原先5.1版本下的數據庫需要根據自己情況調整設置
安裝成功後,測試保存emoji表情符號
修改數據庫編碼格式
例如:
ALTER DATABASE Test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
修改表的編碼格式
例如:
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
測試提交emoji表情,仍然返回不能保存這個字符
java.sql.SQLException: Incorrect string value: ‘\xF0\x9F\x92\x94’ for column ‘name’ at row 1
檢查數據庫變量
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
Variable_name | Value |
---|---|
character_set_client | utf8 |
character_set_connection | utf8 |
character_set_database | utf8 |
character_set_filesystem | binary |
character_set_results | utf8 |
character_set_server | utf8 |
character_set_system | utf8 |
collation_connection | utf8_unicode_ci |
collation_database | utf8_unicode_ci |
collation_server | utf8_unicode_ci |
發現數據庫的系統變量仍然是utf8編碼格式的
以下幾個一定要設置成utf8mb4格式
系統變量 | 描述 |
---|---|
character_set_client | (客戶端來源數據使用的字符集) |
character_set_connection | (連接層字符集) |
character_set_database | (當前選中數據庫的默認字符集) |
character_set_results | (查詢結果字符集) |
character_set_server | (默認的內部操作字符集) |
數據庫連接參數中:
characterEncoding=utf8會被自動識別爲utf8mb4,也可以不加這個參數,會自動檢測。
而autoReconnect=true是必須加上的。
修改my.conf文件
下面的這個是mysql的配置文件,尤爲重要,如果不指定啓動的編碼格式,還是會默認安裝utf8格式啓動,就算數據庫和表字段都設置成utf8mb4編碼格式依然會不能保存4個字節的數據
# 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. [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 # These are commonly set, remove the # and set as required. # basedir = ..... # datadir = ..... # port = ..... # server_id = ..... # socket = ..... # 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 [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'
如果你已經啓動mysql了,使用這個配置重啓服務即可
service mysqld stop// 關閉服務
service mysqld start// 開啓服務
重啓完成後我們再次查看系統變量,輸出如下
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 |
再次測試自己的接口,保存成功