[MySQL/MariaDB] 執行 SQL 指令時出現 Illegal mix of collations 錯誤

[MySQL/MariaDB] 執行 SQL 指令時出現 Illegal mix of collations 錯誤

今天想要把一些程式從 MySQL 轉到 MariaDB,

結果在執行 SQL 指令來建立 stored procedure 時,

出現了 Illegal mix of collations 的錯誤:

ERROR 1267 (HY000) at line 41: Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb3_unicode_ci,IMPLICIT) for operation '='

 

研究了半天,找到了 MariaDB 裡的設定檔有這些值:

character_set_server=utf8
collation_server=utf8_unicode_ci
init_connect='SET NAMES utf8'

 

再爬文了一下,MariaDB 預設的 utf8 代表的是 utf8mb3 (一個 UTF-8 字元以 3 bytes 表示),

但後來都建議使用 utf8mb4 了 (一個 UTF-8 字元以 4 bytes 表示)~

 

用  SHOW TABLE STATUS WHERE Name = 'Statistics'  裡找到了資料表的 Collation 設定,

看起來是 utf8mb3_general_ci,這應該是 utf8mb3 的預設 collation,

爬文是說這個的字串比較速度快,但在某些字元是 utf8mb3_unicode_ci 的結果比較正確:

 

對比錯誤訊息裡的 Illegal mix of collations

(utf8mb3_general_ci,IMPLICIT) and (utf8mb3_unicode_ci,IMPLICIT) for operation ‘=’

推測訊息裡的 utf8mb3_general_ci 應該是資料表的 Collation,

而訊息裡的 utf8mb3_unicode_ci 應該是 MariaDB config 的 utf8_unicode_ci。

 

因此這問題可能就是資料表建立時使用了 utf8mb3_general_ci,

但在使用 SQL 指令建立 stored procedure 時用了 MariaDB config 的 utf8_unicode_ci,

因此這兩個不同的 collation 就無法比較…

 

最後的解法就是將 MariaDB 的 config 改成 utf8mb4_unicode_ci:

character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

 

在所有的地方都使用一致的 collation,就沒有問題囉~

 

參考資料:MySQL错误Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT)解决方法

(本頁面已被瀏覽過 19 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料