MySQL 默认的字符集是 latin1,通常跟我们的中文会打架,导致读写异常或乱码,而我们需要的字符集是 utf-8.
MySQL 字符集的查看
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
可以查大部分都是 latin1.
MySQL 字符集的修改
设置字符集相关的所有变量为 utf8
set character_set_client=utf8 set character_set_connection=utf8 set character_set_database=utf8 set character_set_results=utf8 set character_set_server=utf8 set character_set_system=utf8
这样设置完,新创建的数据库字符集还是 latin1.
2. 新建数据库的时候,显式指定字符集为 utf8.
3. 添加或修改 my.conf 中的默认字符集为 utf8.
参考文档: