1、显示当前编码信息
mysql>show variables like '%character%'
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
2、设置客户端编码
在/etc/mysql/my.cnf文件中的[mysql]或[client]下设置:
default-character-set=utf8
然后,退出myql客户端并重新连接mysql服务器,即设置生效。上述设置影响三项设置:
character_set_client
character_set_connection
character_set_results
注意:
上述设置和在命令行方式下的配置效果相同,不需要重启
mysql>set names utf8;
在Windows的命令行下,执行下面的命令解决中文乱码问题
mysql>set names gbk;
3、设置服务器端编码
在/etc/mysql/my.cnf文件中的[mysqld]下设置:
character-set-server=utf8
重启mysql服务,即设置生效。上述设置影响两项设置:
character_set_database
character_set_server
4、通过MySQL命令行修改
mysql> set character_set_client=utf8;
mysql> set character_set_connection=utf8;
mysql> set character_set_database=utf8;
mysql> set character_set_results=utf8;
mysql> set character_set_server=utf8;
mysql> set character_set_system=utf8;
mysql> set collation_connection=utf8;
mysql> set collation_database=utf8;
mysql> set collation_server=utf8;