mysql乱码相关

Posted aaallenn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql乱码相关相关的知识,希望对你有一定的参考价值。

1、库和表的环境如下,字符集都是latin1:

mysql> show create database yuanqk;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| yuanqk   | CREATE DATABASE `yuanqk` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 
mysql> use yuanqk
Database changed
mysql> show create table student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                        |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ind_name` (`name`),
  KEY `ind_name_age` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> 

  2、插入中文内容后,进行查询,发现是乱码:

mysql> insert into student values(1,‘袁清坤‘);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from student;
+----+------+
| id | name |
+----+------+
|  1 | ???  |
+----+------+
1 row in set (0.00 sec)

  3、乱码产生的原因,一般是数据库服务端字符集和客户端字符集不一致,查看mysql的字符集

mysql> show variables like ‘character_set%‘
    -> ;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |  <===客户端字符集为utf8
| character_set_connection | utf8                       |  
| character_set_database   | latin1                     |  <===数据库字符集为latin1
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /u01/mysql/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

  4、数据库的字符集这应该是create database的时候设置的,即便修改成了utf8,里面已经插入的数据也必须重新导入也能解决乱码问题,但是使用set names latin1来指定客户端字符集就没问题:

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into student values(1,‘袁清坤‘);
ERROR 1062 (23000): Duplicate entry ‘1‘ for key ‘PRIMARY‘
mysql> insert into student values(2,‘袁清坤‘);
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | ???       |
|  2 | 袁清坤    |    <==中文正常显示了
+----+-----------+
2 rows in set (0.00 sec)

mysql> 

  5、其实还可以通过登录时指定字符集也是可以的,如:mysql -uroot -pyuanqk yuanqk --default-character-set=latin1 -S /data/3306/mysql.sock 效果是一样的。

  6、那set names latin1是对数据库做了什么使得可以正常显示了呢,再看下现在的字符集,客户端和服务器端的字符集一致了。

mysql> show variables like ‘character_set%‘;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |  <===客户端字符集变成latin1了
| character_set_connection | latin1                     |  <===连接字符集也变成了latin1
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |  <===这应该是结果返回字符集,也变成latin1了
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /u01/mysql/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

  7、那客户端的字符集为什么会是utf8呢?其实如果这些字符集都在my.cnf中进行了设置,那就不会有这种情况,出现这种情况,我觉得一般都是参数文件中没有设置,那这些客户端的字符集就和操作系统的字符集有关,做个测试看看:

[[email protected] 3306]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"              <===这里可以看到操作系统的字符集就是utf8
SYSFONT="latarcyrheb-sun16"
[[email protected] 3306]# 
--给他换个gb2312看看
[[email protected] 3306]# cat /etc/sysconfig/i18n
#LANG="en_US.UTF-8"
LANG="zh_CN.GB2312" <===换成gb2312
SYSFONT="latarcyrheb-sun16"
[[email protected] 3306]#
[[email protected] 3306]# . /etc/sysconfig/i18n                                             <===使他生效
[[email protected] 3306]# mysql -uroot -pyuanqk -S /data/3306/mysql.sock                    <===重新连接
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.60-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> show variables like ‘character_set%‘;                                        <===再次查看数据库字符集
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | gb2312 |                                                   <===客户端相关的字符集全部变成了gb2312了
| character_set_connection | gb2312 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /u01/mysql/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql>

mysql> select * from student;                                 <===再查询,显示的全是乱码
+----+--------------+
| id | name |
+----+--------------+
| 1 | ??? |
| 2 | ???????????? |
| 5 | ??????? |
| 6 | ?????? |
+----+--------------+
4 rows in set (0.05 sec)

mysql>

    9、然后再设置set names llatin1就又可以显示中文了,因此乱码一般就是客户端和服务器端的字符集不一致导致的,对于客户端的字符集又和操作系统的字符集有关系。最好的办法就是全部写在配置文件中。

mysql> set names latin1
    -> ;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from student;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | ???       |
|  2 | 袁清坤 |
|  5 | 张三    |
|  6 | 李四    |
+----+-----------+
4 rows in set (0.00 sec)

mysql> show variables like ‘character_set%‘;
+--------------------------+----------------------------+
| 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     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /u01/mysql/share/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> 

  

 





以上是关于mysql乱码相关的主要内容,如果未能解决你的问题,请参考以下文章

关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)(转)

解决mysql乱码

mysql存储中文乱码

java入数据库Mysql乱码问题:

mysql插入数据时,中文乱码问题的解决

MySQL 插入数据时,中文乱码问题的解决(转)