MySQL 修改字符集

一 常用命令

SQL 语句

描述

备注

show databases

列出所有数据库

create database

创建一个数据库

create database 库名 character set utf8

创建数据库,顺便执行字符集为 utf-8

show create database 库名

查看数据库的字符集

show variables like '%char%'

查询所有跟字符集相关的信息

set [字符集属性]=utf8

设置相应的属性为 utf8

只是临时修改,当前有效。服务重启后, 失效。

alter database 库名 character set 'utf8'

修改数据库的字符集

alter table 表 名 convert to character set 'utf8'

修改表的字符集

二 实战

[root@iZ251fpy8x9Z etc]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5868
Server version: 5.7.24 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 '%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                   | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+
9 rows in set (0.12 sec)

mysql> create database db01;
Query OK, 1 row affected (0.00 sec)

mysql> use db01;
Database changed
mysql> show tables;
Empty set (0.01 sec)

mysql> create table user(id int not null,name varchar(20));
Query OK, 0 rows affected (0.14 sec)

mysql> show tables;
+----------------+
| Tables_in_db01 |
+----------------+
| user           |
+----------------+
1 row in set (0.00 sec)

mysql> insert into user value(1,'z3');
Query OK, 1 row affected (0.00 sec)

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

mysql> insert into user values(2,'张三');
ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89' for column 'name' at row 1
mysql> select * from user;
+----+------+
| id | name |
+----+------+
|  1 | z3   |
+----+------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db01               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
9 rows in set (0.00 sec)

mysql> alter database db01 character set 'utf8';
Query OK, 1 row affected (0.00 sec)

mysql> use db01;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> alter table user convert to character set 'utf8';
Query OK, 1 row affected (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into user values(2,'张三');
Query OK, 1 row affected (0.01 sec)

mysql> select * from user;
+----+--------+
| id | name   |
+----+--------+
|  1 | z3     |
|  2 | 张三   |
+----+--------+
2 rows in set (0.00 sec)

三  字符集乱码原因

如果在建库建表的时候,没有明确指定字符集,则采用默认的字符集 latin1,其中是不包含中文字符的。查看默认的编码字符集方法。

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                   | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+

四 永久修改

1 修改配置文件 /etc/my.cnf

[client] 
default-character-set=utf8 
[mysqld] 
character_set_server=utf8 
character_set_client=utf8 
collation-server=utf8_general_ci 
[mysql] 
default-character-set=utf8

修改后的样子

MySQL 修改字符集_第1张图片

注意:已经创建的数据库的设定不会发生变化,参数修改只对新建的数据库有效!

2  修改已创建库、表字符集 

修改数据库的字符集 

mysql> alter database mydb character set 'utf8';

 修改数据表的字符集 

mysql> alter table mytbl convert to character set 'utf8';

3 修改已经乱码数据 

无论是修改 mysql 配置文件或是修改库、表字符集,都无法改变已经变成乱码的数据。 

只能删除数据重新插入或更新数据才可以完全解决。

你可能感兴趣的:(mysql)