解决Mysql中文乱码问题

参考原文:http://www.ha97.com/5359.html

      我是在linux下用mysql5.6,通过程序向数据库插入数据,debug时程序里的中文是正常的,插入到数据库后是乱码;

以下是我的修改步骤:

1、命令: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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

说明:我的问题就在character_set_server,需要将其修改为character_set_server=utf8

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

SET character_set_server=utf8;

这三条语句只是在本次连接时有效,关掉数据库重新连接后character_set_server又变成latin1了

 

2,修改配置文件

linux下找到 /etc/my.cnf

修改my.cnf的内容;

命令:vi my.cnf;

在[mysqld]字段里加入character-set-server=utf8,如下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8

保存命令:  按esc 后输入":wq";

3,重启mysql服务

命令:service mysql restart

(有的版本重启命令:service mysqld restart,略有差别)

4,重新查看字符集

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

从新插入数据可以正常显示了!

posted @ 2015-10-31 11:12  阿特洛波斯的跳刀  阅读(212)  评论(0编辑  收藏  举报