SQL Life

解决mysql中文乱码问题

时间:2017/6/29 14:09:14  作者:solgle  来源:solgle.com  查看:799  评论:0
内容摘要:创建数据表StudentCREATE  TABLE  student (id  INT(10)  NOT NULL    PRIMARY KEY  ,name  VARCHAR(20)  NOT NULL ,sex  VARCHAR(4)  ,birth  YEAR,depa...
--创建数据表Student
CREATE  TABLE  student (
id  INT(10)  NOT NULL    PRIMARY KEY  ,
name  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4)  ,
birth  YEAR,
department  VARCHAR(20) ,
address  VARCHAR(50) 
);
 
--插入数据表student数据
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
 
--用工具或者是xshell登录mysql查询表数据,中文显示乱码
mysql> select * from student;
+-----+------+------+-------+------------+---------+
| id  | name | sex  | birth | department | address |
+-----+------+------+-------+------------+---------+
| 901 | ???  | ?    |  1985 | ????       | ??????  |
| 902 | ???  | ?    |  1986 | ???        | ??????  |
| 903 | ??   | ?    |  1990 | ???        | ??????  |
| 904 | ??   | ?    |  1990 | ???        | ??????  |
| 905 | ??   | ?    |  1991 | ???        | ??????  |
| 906 | ??   | ?    |  1988 | ????       | ??????  |
+-----+------+------+-------+------------+---------+
6 rows in set (0.00 sec)
 
mysql> 
 
---查看当前字符集设置
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
 
mysql> 
 
--修改mysql的配置文件my.cnf(my.ini)加入默认编码utf8
mysql> system cat /etc/my.cnf
[mysql]
default-character-set=utf8
 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8
character-set-filesystem = utf8
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
skip-grant-tables
 
 
--需要重启数据库
[root@nameNode ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
 
 
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     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
 
mysql> 
--奇怪的是character_set_database仍然为latin1编码   
 
--再次重启mysql
[root@nameNode ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
 
--这下全为utf8了,难道有延迟...
mysql> show variables like 'character%';
 
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | utf8                       |
| 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)
 
 
而Toad读取MySQL的character_set_system或者character-set-server设置决定Toad本地字符集。
重设MySQL默认字符集,查看官方文档后,设置如下:
在my.cnf配置文件中增加如下2条设置
[mysqld]
character-set-server = utf8
character-set-filesystem = utf8
 
--新建的数据库会以新定义的编码为准、如果数据库已经创建,可以尝试用以下语句来更改
alter database xxx CHARACTER SET utf8;  ---测试下

 
标签:mysql中文乱码 

solgle.com 版权所有,欢迎分享!!!

相关文章
    相关评论
     img1 img2 img3 img4 img5 img6 img7 img8 img9 img10
    评论者:      验证码:  点击获取验证码
       Copyright © 2013-2028 solgle.com,All rights reserved.[solgle.com] 公安机关备案号:51010802000219
    Email:solgle@solgle.com; weixin:cd1008610000 ICP:蜀ICP备14011070号-1