字符集的设置
一、查看字符集
1、查询 MySQL 字符集的设置
show variables like '%character%';
在 MySQL8.0 之前,MySQL 默认的字符集是 latin1
。MySQL8.0 之后的默认字符集是 utfmb4
。
2、查询数据库、表的字符集
show create database dbtest1;
show create table table1;
二、修改字符集
1、修改默认的字符集
在 my.cnf
中的 mysqld
后面加入
character_set_server=utf8
注意:这个只会之后创建的数据库有效,不会修改已经创建的数据的字符集。在已创建的数据中创建新的表,使用的默认字符集是数据库的字符集。
2、修改已存在的数据库的字符集
alter database dbtest1 character set utf8;
3、修改已存在的表的字符集
alter table table1 convert to character set utf8;
4、修改字段的字符集
alter table table1 change field1 varchar(20) character set utf8;
三、请求到响应过程中字符集的变化
使用 character_set_client
接收客户端发送的语句。
收到客户端发来的语句后, 服务端会将语句从 character_set_client
转换为 character_set_connection
服务器向客户端返回数据时使用 character_set_results
set names utf8;
-- 等效
set character_set_client = utf8;
set character_set_connection = utf8;
set character_set_results = utf8;
在 my.cnf
中指定这三个参数值
[mysqld]
default-character-set=utf8