How to make MySQL handle UTF-8 properly

前端 未结 14 2833
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:38

One of the responses to a question I asked yesterday suggested that I should make sure my database can handle UTF-8 characters correctly. How I can do this with MySQL?

14条回答
  •  半阙折子戏
    2020-11-22 07:10

    The short answer: Use utf8mb4 in 4 places:

    • The bytes in your client are utf8, not latin1/cp1251/etc.
    • SET NAMES utf8mb4 or something equivalent when establishing the client's connection to MySQL
    • CHARACTER SET utf8mb4 on all tables/columns -- except columns that are strictly ascii/hex/country_code/zip_code/etc.
    • if you are outputting to HTML. (Yes the spelling is different here.)

    More info ;
    UTF8 all the way

    The above links provide the "detailed canonical answer is required to address all the concerns". -- There is a space limit on this forum.

    Edit

    In addition to CHARACTER SET utf8mb4 containing "all" the world's characters, COLLATION utf8mb4_unicode_520_ci is arguable the 'best all-around' collation to use. (There are also Turkish, Spanish, etc, collations for those who want the nuances in those languages.)

提交回复
热议问题