對mysql Illegal mix of collations(gbk_chinese_ci,IMPLICIT) and (gb2312_chinese_ci,I的解決 對於常見的亂碼問題,有的在數據庫本身已經為gbk或gb2312時候,但是查詢出來卻是亂碼,這是因為mysql在連接的過程中還有道編碼,因此將連接的編碼設為gb2312或utf-8 即可,如: jdbc:mysql://localhost:3306/ipanel?useUnicode=true&characterEncoding=UTF-8 hibernate和jdbc的參數設置是不一樣,但大體相同,網上類似的資料很多。 最重要的是這個問題:
Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (gb2312_chinese_ci,IMPLICIT) for operation '='
相信有很多人碰到,意思是說字符編碼不一樣,不能進行比較,也就是說數據庫內部的編碼都不一樣,有的數據是 gbk_chinese_ci,有的數據是gb2312_chinese_ci,因此解決此問題的核心就是將數據庫所有的編碼進行統一。 進入命令行模式, 如果MySQL數據庫已經安裝好,可以使用下列SQL命令查看MySQL當前的字符集設置:
mysql> SHOW VARIABLES LIKE 'character_set_%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | latin1 | | character_set_results | latin1 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 7 rows in set (0.00 sec) mysql> SHOW VARIABLES LIKE 'collation_%'; +----------------------+-------------------+ | Variable_name | Value | +----------------------+-------------------+ | collation_connection | latin1_swedish_ci | | collation_database | latin1_swedish_ci | | collation_server | latin1_swedish_ci | +----------------------+-------------------+ 3 rows in set (0.00 sec)
依次執行:
set character_set_client =gb2312; set character_set_connection =gb2312; set character_set_database =gb2312; set character_set_results =gb2312; set character_set_server =gb2312; set character_set_system =gb2312; --此處utf-8也可以
然後執行:
SET collation_server = gb2312_chinese_ci SET collation_database = gb2312_chinese_ci SET collation_connection =gb2312_chinese_ci
執行完之後,請檢查mysql下每個數據庫,表,字段是否都是gb2312,不是則改過來,這樣子就不會出現
Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (gb2312_chinese_ci,IMPLICIT) for operation '='這個錯誤了,網上有資料說要重裝,其實根本就不必,改動其編碼即可