3

請直接在 MySQL 裡面直接用 utf8mb4 取代 utf8

 2 years ago
source link: https://blog.niclin.tw/2019/03/11/%E8%AB%8B%E7%9B%B4%E6%8E%A5%E5%9C%A8-mysql-%E8%A3%A1%E9%9D%A2%E7%9B%B4%E6%8E%A5%E7%94%A8-utf8mb4-%E5%8F%96%E4%BB%A3-utf8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Nic Lin's Blog

喜歡在地上滾的工程師

上篇提到的 utf8 問題,今天就來說明一下 utf8 和 utf8mb4 其中的差異與淵源

有一句話是這樣講的

MySQL 中的 utf8 並不是真正的 utf8

出處就不可考了,之前上 DK 的課也有聽到這一個說法。

主要是 utf8 應該是 4 byte 的字符,不過 mysql 裡面的 utf8 只有支援 3 byte。

也就是說,如果你在這樣的設定下插入 4 byte 的字,通常是無法寫入會拿到 Incorrect string value的錯誤訊息。

但是因為這個設定發佈後,就很多人在使用,加上 mysql 有 migrate 自己的 code, 這個 bug 也找不到當時提交的記錄,為了保險起見。

於是在 2010 年就發佈一個新的字節utf8mb4

mb4 的意思就是 most byte 4,很簡單直白。

所以 MySQL 中的 utf8mb4 才是真正的 UTF-8,千萬記得,否則可能無法塞入表情符號之類的字元。

utf8mb4 會比 utf8 來的消耗空間,不過官方有提到可以用 VARCHAR 替代 CHAR 就是了。

  • utf8mb4 是真正的 UTF-8
  • utf8 只是一種專屬的編碼,而且能夠編碼的 Unicode 有限

所以,永遠不要再用 utf8,共勉之。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK