MySQL字符集乱码及解决方案分享

前言

字符集是一套符号和编码的规则,不论是在oracle数据库还是在mysql数据库,都存在字符集的选择问题,而且如果在数据库创建阶段没有正确选择字符集,那么可能在后期需要更换字符集,而字符集的更换是代价比较高的操作,也存在一定的风险,所以,我们推荐在应用开始阶段,就按照需求正确的选择合适的字符集,避免后期不必要的调整。

实战

1、安装MySQL数据库

2、乱码演示

mysql> show variables like 'character_set%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
 
mysql> insert into test.table10 values (1,'云中鹤');
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from test.table10;
+------+-----------+
| id | names |
+------+-----------+
| 1 | 云中鹤 |
+------+-----------+
1 row in set (0.00 sec)
 
mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
 
mysql> select * from test.table10;
+------+-------+
| id | names |
+------+-------+
| 1 | ??? |
+------+-------+
1 row 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 | utf8 #当前选中数据库的默认字符集   
| character_set_filesystem | binary   
| character_set_results | latin1 #查询结果字符集 |
| character_set_server | utf8 #默认的内部操作字符集   |
| character_set_system | utf8 #系统元数据(字段名等)字符集     
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

乱码解决办法

从上面可以看出,set names latin1;改变了三个参数.. 只要做到客户端,MySQL character-set-client,table charset三个字符集完全一致就可以保证一定不会有乱码出现。

方式:

​       1、在mysql命令行模式下执行set names xxx;

mysql> set names utf8;
 Query OK, 0 rows affected (0.01 sec)

       2、登录mysql的时候指定默认字符集

[root@node1 ~]# mysql -S /tmp/mysql.sock4 --defaults-character-set=utf8 #-S 指定多实例mysql的套接字文件,
 使用--defaults-character-set 指定默认字符集。

​       3、 修改/etc/sysconfig/i18n 文件,在my.cnf不指定默认字符集的情况下。

vim /etc/sysconfig/i18n
 LANG='zh_CN.UTF-8' #如果my.cnf不指定,默认使用系统字符集

​       4、  修改my.cnf 文件,下面两个字段在任意一个字段都是可以的。

[client]
  default-character-set=latin1
[mysql]
  default-character-set=latin1

​    前两种1,2是临时解决方案,后面两种3,4是永久有效的方案

查看字符集

1、查看系统当前字符集设置,修改之后登陆mysql执行。show variables lile 'character_set%';

mysql> show variables like 'character_set%';#我修改的为utf8,所以客户端的三个参数都是utf8.你可以自行设置字符集。
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |

2、mysql常用字符集。执行命令show character set; 查看系统支持字符集。

mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description     | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5  | Big5 Traditional Chinese | big5_chinese_ci  |  2 |
| dec8  | DEC West European   | dec8_swedish_ci  |  1 |
| cp850 | DOS West European   | cp850_general_ci |  1 |
| hp8  | HP West European   | hp8_english_ci  |  1 |
| koi8r | KOI8-R Relcom Russian  | koi8r_general_ci |  1 |
| latin1 | cp1252 West European  | latin1_swedish_ci |  1 | #常用
| latin2 | ISO 8859-2 Central European | latin2_general_ci |  1 |
| swe7  | 7bit Swedish    | swe7_swedish_ci  |  1 |
| ascii | US ASCII     | ascii_general_ci |  1 |
| ujis  | EUC-JP Japanese    | ujis_japanese_ci |  3 |
| sjis  | Shift-JIS Japanese   | sjis_japanese_ci |  2 |
| hebrew | ISO 8859-8 Hebrew   | hebrew_general_ci |  1 |
| tis620 | TIS620 Thai     | tis620_thai_ci  |  1 |
| euckr | EUC-KR Korean    | euckr_korean_ci  |  2 |
| koi8u | KOI8-U Ukrainian   | koi8u_general_ci |  1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci |  2 |
| greek | ISO 8859-7 Greek   | greek_general_ci |  1 |
| cp1250 | Windows Central European | cp1250_general_ci |  1 |
| gbk  | GBK Simplified Chinese  | gbk_chinese_ci  |  2 | #常用
| latin5 | ISO 8859-9 Turkish   | latin5_turkish_ci |  1 |
| armscii8 | ARMSCII-8 Armenian   | armscii8_general_ci |  1 |
| utf8  | UTF-8 Unicode    | utf8_general_ci  |  3 |#常用
| ucs2  | UCS-2 Unicode    | ucs2_general_ci  |  2 |
| cp866 | DOS Russian     | cp866_general_ci |  1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci |  1 |
| macce | Mac Central European  | macce_general_ci |  1 |
| macroman | Mac West European   | macroman_general_ci |  1 |
| cp852 | DOS Central European  | cp852_general_ci |  1 |
| latin7 | ISO 8859-13 Baltic   | latin7_general_ci |  1 |
| utf8mb4 | UTF-8 Unicode    | utf8mb4_general_ci |  4 | #常用
| cp1251 | Windows Cyrillic   | cp1251_general_ci |  1 |
| utf16 | UTF-16 Unicode    | utf16_general_ci |  4 |
| cp1256 | Windows Arabic    | cp1256_general_ci |  1 |
| cp1257 | Windows Baltic    | cp1257_general_ci |  1 |
| utf32 | UTF-32 Unicode    | utf32_general_ci |  4 |
| binary | Binary pseudo charset  | binary    |  1 |
| geostd8 | GEOSTD8 Georgian   | geostd8_general_ci |  1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci |  2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci |  3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.00 sec)

字符集选择

​      1、如果处理各种各样的文字,发布到不同语言国家地区,选择Unicode。,对mysql对号utf-8。

​      2、只需中文,数据量很大,性能要求也高,选择gbk.。

​      3、处理移动物联网业务,选utf8mb4

​      建议在能够完全满足应用的前提下,尽量使用小的字符集。因为更小的字符集意味着能够节省空间、减少网络传输字节数,同时由于存储空间的较小间接的提高了系统的性能。

服务器字符集设置

[mysqld]
...
character-set-server=utf8 #添加这条语句,可设置服务器端字符集。

mysql服务端重新启动后,这两个参数会改变为设定值。

| character_set_server  | utf8 
| character_set_database | utf8

切换字符集

将一种编码的数据库转换为另一种编码的数据。

alter database dbname character set xxx; #只能对以后的数据有效,对之前的数据无效。基本不使用

常用转换数据库字符集方案

mysqldump -S /tmp/mysql.sock4 --default-character-set=utf8 -d test > /data/test-`date +%F`.sql #1、导出表结构而不导出数据

vim /data/test-`date +%F`.sql
DROP TABLE IF EXISTS `table10`;
/*!40101 SET @saved_cs_client  = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `table10` (
 `id` int(11) DEFAULT NULL,
 `names` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; #2、这里的CHARSET改为你想要修改的字符集

mysqldump -S /tmp/mysql.sock4 --extended-insert --no-create-info test > /data/test-data-`date +%F`.sql #3、导出数据内容

vim /data/test-data-`date +%F`.sql
LOCK TABLES `table10` WRITE;
set names utf8; #4、这一行是多加的,自己指定想要转换的字符集
INSERT INTO `table10` VALUES (1,'云中鹤');
UNLOCK TABLES;

mysql> create database if not exists test; #5、创建数据库,存储转换后的数据
Query OK, 1 row affected, 0 warning (0.00 sec)

mysql -S /tmp/mysql.sock4 test < /data/test-`date +%F`.sql #6、导入表结构

mysql -S /tmp/mysql.sock4 test < /data/test-data-2015-09-25.sql #7、导入表数据

最后

这是在15年的时候,自己学习MySQL的笔记,今天再拿出来分享下

总结

相关推荐