关于MYSQL存储中文问题

最近在学习MYSQL时遇到了不能存储中文的问题,在网上找了很多类似的方法,最后都失败了(失败原因:修改完my.cnf文件后mysql server重新启动失败),濒临崩溃的时候重装了下mysql(Ubuntu重装mysql)然后用以下方法走了一遍就解决了。

 

造成MYSQL存储中文乱码的原因是编码格式不一致导致乱码,如何去查看编码是否一致呢?

首先进入mysql服务器:

~$ mysql -u root -p******
//“*”号处输入密码,与p之间没有空格;

 查看当前数据库相关编码集:

show variables like '%char%';       (注意最后有分号!)       
+------------------------------+---------------------------------+
| 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/      |
+------------------------------+---------------------------------+
8 rows in set (0.00 sec)

 如何去修改成一致的编码格式呢?

 Ctrl+C退出mysql服务器。

 用vi或者gedit打开MYSQL配置文件my.cnf,并添加内容:

~$ sudo vi /etc/mysql/my.cnf              //用vi打开

~$ sudo gedit /etc/mysql/my.cnf         //用gedit打开

在[client]标签下,增加客户端的字符编码
[client]
default-character-set=utf8

在[mysqld]标签下,增加服务器端的字符编码
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

 修改完后,重新启动MYSQL服务器:

~$ sudo /etc/init.d/mysql restart

 再次进入mysql服务器,查看编码集,出现如下结果表示You succeed:

+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

转载自本人ITeye链接:http://xiaozhuang0706.iteye.com/blog/2258342



 

posted @ 2017-03-03 14:32  Halo茫  阅读(7170)  评论(1编辑  收藏  举报