MySQL 不可以识别中文怎么办?
初步判断,大概是字符集设置问题。尝试把字符集设置为utf8mb4,可能解决问题。
MySQL 的utf8字符集不是真正的 UTF-8,只支持最多三个字节的字符。真正的 UTF-8 可能会出现四个字节的字符。MySQL 从来没有修复这个 Bug,而是使用另外的解决方法:真正的 UTF-8 字符集改用utf8mb4的名字提供。参考自博客文章,《In MySQL, never use “utf8”. Use “utf8mb4”.》:https://adamhooper.medium.com/in-mysql-never-use-utf8-use-utf8mb4-11761243e434
In MySQL, never use “utf8”. Use “utf8mb4”.
如何把utf8转换为utf8mb4?运行以下 SQL 语句,注意替换相关名称:
# 代码参考: 《MySQL utf8 vs utf8mb4 – What’s the difference between utf8 and utf8mb4?》 (https://www.eversql.com/mysql-utf8-vs-utf8mb4-whats-the-difference-between-utf8-and-utf8mb4/)# Run this once on each schema you have (Replace database_name with your schema name)ALTERDATABASEdatabase_nameCHARACTERSET=utf8mb4COLLATE=utf8mb4_unicode_ci;# Run this once for each table you have (replace table_name with the table name)ALTERTABLEtable_nameCONVERTTOCHARACTERSETutf8mb4COLLATEutf8mb4_unicode_ci;# Run this for each column (replace table name, column_name, the column type, maximum length, etc.)ALTERTABLEtable_nameCHANGEcolumn_namecolumn_nameVARCHAR(191)CHARACTERSETutf8mb4COLLATEutf8mb4_unicode_ci;
要更详细了解 MySQL 字符集相关知识,请参考官方手册,如,MySQL 8 的参考手册《10.9.1 The utf8mb4 Character Set (4-Byte UTF-8 Unicode Encoding)》、《10.10.1 Unicode Character Sets》等。
阅读手册中的字符集相关章节
utf8: An alias for utf8mb3.