一、mysql字符集简介
''' mysql字符集 字符集是一套符合和编码,校验规则(collation)是在字符集内用于比较字符的一套规则,即字符集的排序规则。MySQL可以使用对种字符集和检验规则来组织字符。 MySQL服务器可以支持多种字符集,在同一台服务器,同一个数据库,甚至同一个表的不同字段都可以指定使用不同的字符集 相比oracle等其他数据库管理系统,在同一个数据库只能使用相同的字符集,MySQL明显存在更大的灵活性。 '''
二、查看字符集
1、查看当前系统字符集
方式1:show variables like 'character%';
+--------------------------+---------------------------------------------------------------+ | Variable_name | Value | +--------------------------+---------------------------------------------------------------+ | character_set_client | utf8mb4 | | character_set_connection | utf8mb4 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | utf8mb4 | | character_set_server | utf8mb4 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64/share/charsets/ | +--------------------------+---------------------------------------------------------------+
方式2:show variables like 'collation%';
+----------------------+--------------------+ | Variable_name | Value | +----------------------+--------------------+ | collation_connection | utf8mb4_general_ci | | collation_database | utf8mb4_general_ci | | collation_server | utf8mb4_general_ci | +----------------------+--------------------+
2、查看mysql支持的所有字符集
show charset;
+----------+-----------------------------+---------------------+--------+ | 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 | | utf16le | UTF-16LE Unicode | utf16le_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 | +----------+-----------------------------+---------------------+--------+
3、查看指定库的字符集
show create database game;
+----------+------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------+ | gamee | CREATE DATABASE `game` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ | +----------+------------------------------------------------------------------+
4、查看表字符集
方式1:show table status from game like 'user_id'G;
*************************** 1. row *************************** Name: userid Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 898656 Avg_row_length: 29 Data_length: 26787840 Max_data_length: 0 Index_length: 9977856 Data_free: 5242880 Auto_increment: 1817490 Create_time: 2018-09-14 16:11:06 Update_time: 2018-09-30 17:54:12 Check_time: NULL Collation: utf8mb4_general_ci Checksum: NULL Create_options: Comment:
方式2:show create table user_id;
*************************** 1. row *************************** Table: user_id Create Table: CREATE TABLE `userid` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `is_used` tinyint(4) NOT NULL DEFAULT '0', `user_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`), KEY `is_used_idx` (`is_used`) ) ENGINE=InnoDB AUTO_INCREMENT=1817490 DEFAULT CHARSET=utf8mb4 1 row in set (0.01 sec)
5、查看表中字段的字符集
show full columns from 表名;
mysql> show full columns from recordinfo; +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment | +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+ | uuid | int(11) | NULL | NO | PRI | NULL | | select,insert,update,references | | | current | int(11) | NULL | NO | PRI | NULL | | select,insert,update,references | | | data | varchar(4096) | utf8mb4_general_ci | NO | | | | select,insert,update,references | | | replay | blob | NULL | NO | | NULL | | select,insert,update,references | | | code | int(11) | NULL | NO | MUL | NULL | | select,insert,update,references | | | time | timestamp | NULL | NO | | CURRENT_TIMESTAMP | | select,insert,update,references | | +---------------+---------------+--------------------+------+-----+-------------------+-------+---------------------------------+---------+
三、设置字符集(已存在的库、表、字段)
1、设置字段字符集
alter table 表名 modify `字段名` varchar(64) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '昵称';
2、设置表字符集
alter table 表名 default charset=utf8mb4;
3、设置库字符集
alter database 库名 default character set 'utf8mb4';
字符集优先级由高到低:字段——表——库
四、设置字符集(创建时指定字符集)
1、创建数据库
create database 库名 default character set=utf8mb4;
2、创建表
create table student( id int not null auto_increment primary key, name varchar(10) not null default '' comment '姓名' ) default character set='utf8mb4';
3、创建字段
create table student( id int not null auto_increment primary key, name varchar(10) character set utf8mb4 not null default '' comment '姓名' ) default character set='utf8mb4';
PS:
创建库时,如果不指定字符集,则使用系统默认库字符集
创建表时,如果不指定字符集,则继承库的字符集
新增字段时,如果不指定字符集,则继承表的字符集
五、修改全局字符集
临时修改(mysql服务重启失效):
/*建立连接使用的编码*/ set character_set_connection=utf8; /*数据库的编码*/ set character_set_database=utf8; /*结果集的编码*/ set character_set_results=utf8; /*数据库服务器的编码*/ set character_set_server=utf8; set character_set_system=utf8; set collation_connection=utf8; set collation_database=utf8; set collation_server=utf8;
永久修改:
修改配置文件,重启mysql服务
character-set-server=utf8mb4
[root@localhost ~]$ grep "utf8mb4" /etc/my.cnf character-set-server=utf8mb4
注意:以上修改,只是对新增的库生效,已存在的库 表 字段不生效