PS:昨天一同事遇到mysql 5.5中文乱码问题,找我解决。解决了,有个细节问题网上没人说,我就总结一下。
一、登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;(正确命令为show variables like "%char%";)下字符集,显示如下:
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
character_set_database和character_set_server的默认字符集还是latin1。
二、最简单的完美修改方法,修改mysql的my.cnf文件中的字符集键值(注意配置的字段细节):
1、在[client]字段里加入default-character-set=utf8,如下:
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
2、在[mysqld]字段里加入character-set-server=utf8,如下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
3、在[mysql]字段里加入default-character-set=utf8,如下:
[mysql]
no-auto-rehash
default-character-set=utf8
修改完成后,service mysql restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。这点在网上没人反馈过。
使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8。
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
4、如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):
SET NAMES 'utf8';
它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
网上很多其他方法不能彻底解决这个问题,这个可以完美解决!
<p class="postendmeta">
<a href="http://www.ha97.com/5359.html" rel="bookmark" title="(原创)Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)" class="permalink">永久链接</a> : http://www.ha97.com/5359.html </p>
</div>
Activity生命周期详解
android 和主线程有关的小问题
java解析Excel(xls、xlsx两种格式)
菜鸟感想一
springboot 填坑一 springboot java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
git 第一次提交至仓库
andriod studio报错 Emulator: emulator: ERROR: x86 emulation currently requires hardware acceleration! Emulator: Process finished with exit code 1
通过以太坊发行代币(token)
nginx+tomcat 配置负载均衡集群 (转载)
给一个真正完美的解决方案!
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect=’SET NAMES utf8′
character-set-server = utf8
楼主 给出了正确方法,要在[clien]下加defualt-character-set=utf-8,在[mysqld]加 character-set-service=utf8 ,还有在[mysql] 下也加defualt-character-set 完美解决了,找了几天了拜谢
[mysqld]加 character-set-service=utf8 ? => 新版本的mysql会无法重启。
最简单的解决办法如下:
[mysqld]加 character-set-server = utf8
可按需添加 init-connect=’SET NAMES utf8′