Linux下MySQL 5.5/5.6的修改字符集编码为UTF8(彻底解决中文乱码问题)

昨天在CentOS 7上遇到MySQL 5.6遇到乱码问题,特此总结一下: 一、登录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_databasecharacter_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/ |
+--------------------------+----------------------------+

输入图片说明

转载于:https://my.oschina.net/dabird/blog/550287

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值