linux环境下mysql数据库的编码问题

2016-12-20 10:03:18来源:oschina作者:Ana1ysis人点击

linux下则需要修改/etc/my.cnf,在[mysqld]下加入default-character-set = utf8,[client]下加入default-character-set = utf8,在[mysql]字段里加入default-character-set=utf8


实现: 1.查看原编码



代码如下
复制代码


mysql> show variables like 'character%'; //执行编码显示,可以看到默认是latin1 +--------------------------+----------------------------+ | 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/ | +--------------------------+----------------------------+

2.修改/etc/my.cnf,分别在[client],[mysqld],[mysql]字段里添加default-character-set=utf8。注意[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。


# vim /etc/my.cnf //添加default-character-set=utf8



代码如下
复制代码


[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set=utf8

[mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock character-set-server=utf8

[mysql] no-auto-rehash



default-character-set=utf8修改完成后,service mysql restart重启mysql服务器,使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8



代码如下
复制代码


mysql> show variables like 'character%'; +--------------------------+----------------------------------------+ | 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/mysql/charsets/ | +--------------------------+----------------------------------------+

3.如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):



代码如下
复制代码


SET NAMES ‘utf8′;



它相当于以下三指令:



代码如下
复制代码


SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8;

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台