Linux下MySQL_5.5的修改字符集编码为UTF8

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:Linux下MySQL_5.5的修改字符集编码为UTF8

背景

mysql安装后, 插入的数据位乱码, 经检查为默认字符集是 latin1 , 而程序使用 UTF-8 .

检查字符集

1.使用ssh终端连接mysql

mysql -h 127.0.0.1 -u root -p

2.查看字符集

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 文件中的字符集键值, my.cnf 一般位于 /etc 目录下, 没找到的话可以使用 find / -name 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]字段是有区别的.

4.再次查看字符集

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/share/mysql/charsets/ |
+--------------------------+----------------------------+

5.如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上.

SET NAMES 'utf8';

该语句相当于

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Building Web Reputation Systems

Building Web Reputation Systems

Randy Farmer、Bryce Glass / Yahoo Press / 2010 / GBP 31.99

What do Amazon's product reviews, eBay's feedback score system, Slashdot's Karma System, and Xbox Live's Achievements have in common? They're all examples of successful reputation systems that enable ......一起来看看 《Building Web Reputation Systems》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具