介绍

不同的编码格式会导致同一字符,在不同字符集下的编码会不同。同样同一编码在不同的字符集中代码的字符也不相同。当你的MySQL返回的字符串的编码格式(字符集)与你的客户工具程序使用的字符集不同时,就会造成乱码。

MySQL中默认字符集的设置有四级:服务器级,数据库级,表级 。最终是字段级 的字符集设置。注意前三种均为默认设置,并不代码你的字段最终会使用这个字符集设置。

MySQL中默认字符集的设置有四级:服务器级别 > 数据库级别 > 表级别 > 列级别。如果后者没有显示指定字符集,那么将采用前者的字符集。

MySQL默认字符集  

 MySQL对于字符集的指定可以细化到一个数据库,一张表,一列.传统的程序在创建数据库和数据表时并没有使用那么复杂的配置,它们用的是默认的配置.

        (1)、编译MySQL 时,指定了一个默认的字符集,这个字符集是 latin1;

        (2)、安装MySQL 时,可以在配置文件 (my.ini) 中指定一个默认的的字符集,如果没指定,这个值继承自编译时指定的;

        (3)、启动mysqld 时,可以在命令行参数中指定一个默认的的字符集,如果没指定,这个值继承自配置文件中的配置,此时 character_set_server 被设定为这个默认的字符集;

Server

1、查看数据库的编码方式命令


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


其中


- character_set_client              客户端来源数据使用的字符集编码;
 
  
- character_set_connection          为建立连接使用的编码(从客户端接收到数据,然后传输的字符集);
 
  
- character_set_database            默认数据库的字符集编码;
 
  
- character_set_filesystem          把os上文件名转化成此字符集,即把 character_set_client转换character_set_filesystem, 默认binary是不做任何转换的
 
  
- character_set_results             查询结果字符集编码;
 
  
- character_set_server              数据库服务器的默认字符集编码;
 
  
- character_set_system              系统元数据(字段名等)字符集,是为存储系统元数据的字符集;


2、另一组查看数据库编码的命令


mysql> show variables like 'collation%';
 
  
+----------------------+-----------------+
 
  
| Variable_name        | Value           |
 
  
+----------------------+-----------------+
 
  
| collation_connection | utf8_general_ci |
 
  
| collation_database   | utf8_general_ci |
 
  
| collation_server     | utf8_bin        |
 
  
+----------------------+-----------------+
 
  
3 rows in set (0.00 sec)


其中


- COLLATION_CONNECTION          当前连接的校对
 
  
- COLLATION_DATABASE            当前日期的默认校对。每次使用USE语句来“跳转”到另一个数据库时,这个变量就会改变。
 
  
- COLLATION_SERVER              服务器默认校对

Database

1、查看某数据库的字符集

    方法:show create database DatabaseName;


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


Table

1、数据库表的字符集设置

     方法:show create table TableName;



显示一:


mysql> show create table destination;
 
  
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 
  
| Table       | Create Table   |
 
  
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 
  
| destination | CREATE TABLE `destination` (
 
  
  `id` int(10) NOT NULL AUTO_INCREMENT,
 
  
  `pid` int(10) NOT NULL DEFAULT '0',
 
  
  `name` varchar(20) NOT NULL,
 
  
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8                         |
 
  
+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 
  
1 row in set (0.00 sec)


显示二:

mysql> show create table destination \G
 
  
*************************** 1. row ***************************
 
  
       Table: destination
 
  
Create Table: CREATE TABLE `destination` (
 
  
  `id` int(10) NOT NULL AUTO_INCREMENT,
 
  
  `pid` int(10) NOT NULL DEFAULT '0',
 
  
  `name` varchar(20) NOT NULL,
 
  
  `subarea` varchar(1000) DEFAULT NULL,
 
  
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8
 
  
1 row in set (0.00 sec)


column

1、数据库表中字段的字符集设置



显示一:


mysql> show full columns from destination;
 
  
+------------+---------------+-----------------+------+-----+---------------------+-----------------------------+---------------------------------+--------------------+
 
  
| Field      | Type          | Collation       | Null | Key | Default             | Extra                       | Privileges                      | Comment            |
 
  
+------------+---------------+-----------------+------+-----+---------------------+-----------------------------+---------------------------------+--------------------+
 
  
| id         | int(10)       | NULL            | NO   | PRI | NULL                | auto_increment              | select,insert,update,references |                    |
 
  
| pid        | int(10)       | NULL            | NO   | MUL | 0                   |                             | select,insert,update,references |                    |
 
  
| name       | varchar(20)   | utf8_general_ci | NO   |     | NULL                |                             | select,insert,update,references |                    |
 
  
| subarea    | varchar(1000) | utf8_general_ci | YES  |     | NULL                |                             | select,insert,update,references |                    |
 
  
+------------+---------------+-----------------+------+-----+---------------------+-----------------------------+---------------------------------+--------------------+
 
  
4 rows in set (0.00 sec)



显示二:


mysql> show full columns from destination \G
 
  
*************************** 1. row ***************************
 
  
     Field: id
 
  
      Type: int(10)
 
  
 Collation: NULL
 
  
      Null: NO
 
  
       Key: PRI
 
  
   Default: NULL
 
  
     Extra: auto_increment
 
  
Privileges: select,insert,update,references
 
  
   Comment:
 
  
*************************** 2. row ***************************
 
  
     Field: pid
 
  
      Type: int(10)
 
  
 Collation: NULL
 
  
      Null: NO
 
  
       Key: MUL
 
  
   Default: 0
 
  
     Extra:
 
  
Privileges: select,insert,update,references
 
  
   Comment:
 
  
*************************** 3. row ***************************
 
  
     Field: name
 
  
      Type: varchar(20)
 
  
 Collation: utf8_general_ci
 
  
      Null: NO
 
  
       Key:
 
  
   Default: NULL
 
  
     Extra:
 
  
Privileges: select,insert,update,references
 
  
   Comment:
 
  
*************************** 4. row ***************************
 
  
     Field: subarea
 
  
      Type: varchar(1000)
 
  
 Collation: utf8_general_ci
 
  
      Null: YES
 
  
       Key:
 
  
   Default: NULL
 
  
     Extra:
 
  
Privileges: select,insert,update,references
 
  
   Comment:
 
  
4 rows in set (0.00 sec)

修改默认字符集

1、修改mysql的my.cnf文件中的字符集键值



编辑配置文件

#vi /etc/my.cnf
 
  
  [mysqld]
 
  
   default-character-set = utf8
 
  
   character_set_server = utf8
 
  
   collation-server=utf8_bin


更新编辑完成之后,重启mysql服务器使其生效。



2、使用mysql的命令修改字符集的方法


mysql> SET character_set_client = utf8 ;
 
  
mysql> SET character_set_connection = utf8 ;
 
  
mysql> SET character_set_database = utf8 ;
 
  
mysql> SET character_set_results = utf8 ;
 
  
mysql> SET character_set_server = utf8 ;
 
  
mysql> SET collation_connection = utf8 ;
 
  
mysql> SET collation_database = utf8 ;
 
  
mysql> SET collation_server = utf8 ;

使其快速设置成相应的字符集,但重启之后会变成之前系统默认字符集。

问题追踪

设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,存入数据库的仍然是乱码。那connection连接层上可能出了问题。解决方法是在发送查询前执行一下下面这句: SET NAMES 'utf8';它相当于下面的三句指令:


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

转载于:https://blog.51cto.com/pangge/1595604