文章目录
一、出现中文乱码的原因
1.查看字符集
mysql> show variables like ‘%char%’;
±-------------------------±------------------------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.6.34/share/charsets/ |
±-------------------------±------------------------------------------+
=======》》》
character_set_client 客户端使用的字符编码
character_set_connection 数据库链接使用的编码
character_set_database 数据库使用的字符编码
latin1字符集也是支持中文数据的,出现乱码的原因是这里的字符编码没有统一。
2.使用默认字符集创建数据库与数据表
2.1创建数据库
mysql> show create database liang\G
*************************** 1. row ***************************
Database: liang
Create Database: CREATE DATABASEliang
/*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
2.2创建数据表
mysql> show create table test01\G
*************************** 1. row ***************************
Table: test01
Create Table: CREATE TABLEtest01
(
id
int(4) NOT NULL AUTO_INCREMENT,
Name
char(20) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
2.4插入英文显示正常
mysql> insert into test01 (name) values (“laowang”);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test01;
±—±--------+
| id | Name |
±—±--------+
| 1 | laowang |
±—±--------+
1 row in set (0.00 sec
2.5插入中文
mysql> insert into test01 (name) values (“老王”);
Query OK, 1 row affected, 1 warning (0.01 sec)
2.6显现乱码
mysql> select * from test01;
±—±--------+
| id | Name |
±—±--------+
| 1 | laowang |
| 2 | ?? |
±—±--------+
2 rows in set (0.00 sec)
二、解决乱码的方法
1.设置客户端临时字符集
1.1 set names latin1;
mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
1.2插入中文数据
mysql> insert into test01 (name) values (“老王”);
Query OK, 1 row affected (0.02 sec)
1.3显示正常
mysql> select * from test01;
±—±--------+
| id | Name |
±—±--------+
| 1 | laowang |
| 2 | ?? |
| 3 | 老王 |
±—±--------+
3 rows in set (0.00 sec)
2.为什么set names latin1就解决了中文数据乱码问题?
mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘%char%’;
±-------------------------±------------------------------------------+
| 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 | /application/mysql-5.6.34/share/charsets/ |
±-------------------------±------------------------------------------+
8 rows in set (0.01 sec)
=====》》》
输入set names latin1后,字符编码统一就不会出现乱码。
解决乱码问题其实就是将客户端的字符编码与服务端、数据库的编码统一。这里的服务端与数据库编码都是latin1,所有set names latin1即可临时解决乱码问题。
3.文件数据导入形式
方式一:
mysql> system cat /root/test.sql
set names latin1; ###设置临时字符集
insert into test01 (name) values (“小王”);
mysql> source /root/test.sql###导入
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
mysql> select * from test01;
±—±--------+
| id | Name |
±—±--------+
| 1 | laowang |
| 2 | ?? |
| 3 | 老王 |
| 4 | 小王 |
±—±--------+
4 rows in set (0.00 sec)
方式二:
[root@mysql ~]# cat test01.sql
insert into test01 (name) values (“中王”);
[root@mysql ~]# mysql -uroot -p000000 -S /data/3306/mysql.sock --default-character-set=latin1 liang </root/test01.sql
mysql> select * from test01;
±—±--------+
| id | Name |
±—±--------+
| 1 | laowang |
| 2 | ?? |
| 3 | 老王 |
| 4 | 小王 |
| 5 | 中王 |
±—±--------+
5 rows in set (0.00 sec)
4.配置文件指定字符集
更改my.conf模块的参数
[client]
default-character-set=latin1
[mysqld]
character-set-server=latin1
三、统一字符集实战配置(以utf-8为例)
1.Linux系统
[root@mysql ~]# cat /etc/sysconfig/i18n
LANG=zh_CN.UTF-8
2.服务端
[mysqld]
character-set-server=utf8
3.客户端
[client]
default-character-set=latin1
4.库、表
mysql> show character set;###查询字符集对应的校对规则
使用utf8创建数据库
mysql> create database liang_utf8 default character set utf8 collate utf8_general_ci;
表不指定则默认继承库的字符集
5.重启数据库
[root@mysql ~]# /data/3306/mysqld restart
6.登录数据库查看使用的字符编码
mysql> 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 | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /application/mysql-5.6.34/share/charsets/ |
±-------------------------±------------------------------------------+
8 rows in set (0.00 sec)