乱码问题一直是困扰程序员最心烦的问题之一,今天网上查询了一些资料,把mysql的字符编码设置总结如下.
一:首先修改mysql中的配置文件my.ini,
在[mysql]标签中修改default-character-set=utf8
在[client]标签中添加default-character-set=utf8
在[mysqld]标签中修改default-character-set=utf8
在[mysqld]标签中添加character_set_server = utf8
#表名不区分大小写(此与编码无关)
lower_case_table_names = 1
添加标签[mysql.server]和[mysql_safe] 并分别在标签下添加
default-character-set=utf8
命令行输入 net stop mysql
命令行输入 net start mysql
命令行输入mysql -uroot -pXXX 进入
命令行输入show variables like 'character\_set\_%';[用于查看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 |
+--------------------------+--------+
显示了所有的编码方式;
还可以对数据库的编码方式进行修改
命令行输入 alter database test character set 'gbk';
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | gbk |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+--------+
这样就设置了test数据库的表的编码方式为GBK了
假如想把character_set_filesystem 也改为utf8;
命令行输入 set character_set_filesystem=utf8;
命令行输入 set character_set_database=utf8;
命令行输入 show variables set 'character\_set\_%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | utf8 |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
+--------------------------+-------+
但是对于 character_set_filesystem 这个文件的编码 在下次启动后依然为
+--------------------------+--------+
| 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_set_client和character_set_filesystem等已经在配置文件里设置了,这里修改的只对当前页面有效。
参考资料http://xinsync.xju.edu.cn/index.php/archives/7493
分享到: