刚刚安装上MySQL,新建好了一个数据库,建好了表,读取数据是时候却出现了中文乱码!这搞得郁闷了半天,最后百度了一下才知道,MySQL默认字符是latinl,不支持中文,而windows系统默认字符集是GBK这就不得不修改MySQL字符集了 修改方法如下:
通过以下命令查看了MySQL的字符集
连接上mysql服务,输入以下命令:
mysql>show variables like 'character_set%';
其显示如下:
- +---------------------
-----+----------------- -----------+ - |
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/ | - +--------------------------+----------------------------+
为了让MySQL支持中文,需要把字符集改变成UTF8,
方法如下:
1. 打开etc/my.cnf
2. 修改成如下内容,保存
- [client]
- port=3306
- socket=/var/lib/mysql/mysql.sock
- default-character-set=utf8
-
- [mysqld]
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- user=mysql
- #
Disabling symbolic-links is recommended to prevent assorted security risks - symbolic-links=0
- character-set-server=utf8
-
- [mysql]
- no-auto-rehash
- default-character-set=utf8
-
- [mysqld_safe]
- log-error=/var/log/mysqld.log
- pid-file=/var/run/mysqld/mysqld.pid
再次查看mysql编码,已经更新成如下信息
- +--------------------------+----------------------------+
- |
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/share/mysql/charsets/ | - +--------------------------+----------------------------+
- 就这样,大功告成了。。。。。
-