本文共 3311 字,大约阅读时间需要 11 分钟。
1 2 3 4 5 | [mysql] default-character- set =utf8 // 客户端的默认字符集 [mysqld] default-character- set =utf8 // 服务器端默认的字符集 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | mysql> show variables like 'character_set_%' ; + --------------------------+----------------------------------+ | Variable_name | Value | + --------------------------+----------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/ local /mysql/share/charsets/ | + --------------------------+----------------------------------+ 8 rows in set (0.00 sec) mysql> |
解决办法是,在连接数据库之后且读取数据之前,先执行 SET NAMES UTF8 命令。在 mysql 命令行中执行 SET NAMES UTF8; 后重新查看字符编码信息,结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | mysql> set names utf8; Query OK, 0 rows affected (0.00 sec) mysql> mysql> show variables like 'character_set_%' ; + --------------------------+----------------------------------+ | Variable_name | Value | + --------------------------+----------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/ local /mysql/share/charsets/ | + --------------------------+----------------------------------+ 8 rows in set (0.00 sec) mysql> |
可以看到发生变化的变量为
1 2 3 | SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; |
转载地址:http://ghlox.baihongyu.com/