ubuntu MySQL5.6修改字符集编码为utf8

前端之家收集整理的这篇文章主要介绍了ubuntu MySQL5.6修改字符集编码为utf8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、登录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/ |
+--------------------------+----------------------------+

character_set_database和character_set_server的默认字符集还是latin1。

二、最简单的完美修改方法修改MysqLmy.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;

网上很多其他方法不能彻底解决这个问题,这个可以完美解决

原文链接:https://www.f2er.com/ubuntu/354671.html

猜你在找的Ubuntu相关文章