赞
踩
-- 查看 mysql 默认字符集
SHOW VARIABLES LIKE 'character_%';
-- 查看 mysql 默认字符排序
SHOW VARIABLES LIKE 'collation_%';
返回的结果为:
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 | utf8mb3 |
character_sets_dir | D:\java_web\java_environment\Mysql\MySQL Server 8.0\share\charsets| |
与
Variable_name | Value |
---|---|
collation_connection | utf8mb4_0900_ai_ci |
collation_database | utf8mb4_0900_ai_ci |
collation_server | utf8mb4_0900_ai_ci |
当 character_set_database 与 character_set_server 为 utf8mb4 时,新建数据库与表的时候默认的字符编码集就是 utf8mb4。
当 collation_database 与 collation_server 为 utf8mb4_0900_ai_ci 时,新建表的时候默认的字符比较规则就是 utf8mb4_0900_ai_ci。
我们可以测试一下:
-- 新建一个数据库
CREATE DATABASE test_database;
-- 查看数据库信息
SHOW CREATE DATABASE test_database;
-- 使用新建数据库
USE test_database;
-- 新建一个表
CREATE TABLE test_table(id INT, name VARCHAR(10));
-- 查看表信息
SHOW CREATE TABLE test_table;
运行 SHOW CREATE DATABASE test_database; 的结果为
Database | Create Database |
---|---|
test_database | CREATE DATABASE test_database /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci / /!80016 DEFAULT ENCRYPTION=‘N’ */ |
运行 SHOW CREATE TABLE test_table; 结果为
Table | Create Table |
---|---|
test_table | CREATE TABLE test_table (id int DEFAULT NULL,name varchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
修改数据库 my.ini 文件
在 [mysql] 下修改
default-character-set=utf8
在 [mysqld] 下修改
character-set-server=utf8
collation-server=utf8_general_ci
重启服务,服务名每台电脑不一样,Mysql 8.0 版本默认是 MYSQL80
net stop MYSQL80
net start MYSQL80
修改数据库 my.cnf 文件,一般在 /etc/my.cnf。
在 [mysql] 下修改
default-character-set=utf8
在 [mysqld] 下修改
character-set-server=utf8
collation-server=utf8_general_ci
重启服务
systemctl restart mysqld.service
Mysql 有四个字符集与比较级别
我们再看下数据库字符集的参数:
其中 character_set_client、character_set_connection、character_set_results 这三个是客户端每次链接进来设置的,与服务端没关系。所以每一个客户端看到的是自己链接数据库所配置的字符集,所以看到的也并不相同。如果想要修改可以直接使用 sql 进行修改,如:SET character_set_connection = 'utf8';
。
character_set_database 是当前所在库的字符集,切换不同的数据库可以看到对应数据库的字符集。
character_set_server 是服务器配置的字符集,需要修改的话就必须在 Mysql 的配置文件中修改,在 Mysql 8.0 版本以上默认是 utfmb4,在 8.0 版本以下默认是 latin1。
在这四个级别中
新建数据库的时候如果不指定字符集与字符比较方式就会默认以服务器级别为准;
新建表的时候如果不指定字符集与字符比较方式时就会默认以数据库级别为准;
新建字段的时候如果不指定字符集与字符比较方式时就会默认以表级别为准。
utf-8 字符集需要 1 ~ 4 个字节表示一个字符,我们常用的一些字符使用 1 ~ 3 个字节就通常就可以表示。于是 Mysql 设计的时候就将 utf8 定义了两套字符集:一套是能满足平时大多数情况且占内存较少的字符集 utf8mb3,字符只占 1 ~ 3 个字节;一套是能满足所有情况但是相对比较占用内存的字符集 utf8mb4,字符需占用 1 ~ 4 个字节。
SHOW CHARSET;
返回结果:
Charset | Description | Default collation | Maxien |
---|---|---|---|
armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
ascii | US ASCII | ascii_general_ci | 1 |
big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
binary | Binary pseudo charset | binary | 1 |
cp1250 | Windows Central European | cp1250_general_ci | 1 |
cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
cp1256 | Windows Arabic | cp1256_general_ci | 1 |
cp1257 | Windows Baltic | cp1257_general_ci | 1 |
cp850 | DOS West European | cp850_general_ci | 1 |
cp852 | DOS Central European | cp852_general_ci | 1 |
cp866 | DOS Russian | cp866_general_ci | 1 |
cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
dec8 | DEC West European | dec8_swedish_ci | 1 |
eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
euckr | EUC-KR Korean | euckr_korean_ci | 2 |
gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
greek | ISO 8859-7 Greek | greek_general_ci | 1 |
hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
hp8 | HP West European | hp8_english_ci | 1 |
keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
latin1 | cp1252 West European | latin1_swedish_ci | 1 |
latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
macce | Mac Central European | macce_general_ci | 1 |
macroman | Mac West European | macroman_general_ci | 1 |
sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
tis620 | TIS620 Thai | tis620_thai_ci | 1 |
ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
utf8mb3 | UTF-8 Unicode | utf8mb3_general_ci | 3 |
utf8mb4 | UTF-8 Unicode | utf8mb4_0900_ai_ci | 4 |
我们常用的有:专门描述汉字的字符集 gb2312、gbk;国际通用字符集:utf8mb3、utf8mb4。
后缀 | 英文释义 | 描述 |
---|---|---|
_ai | accent_insensitive | 不区分重音 |
_as | accent_sensitive | 区分重音 |
_ci | case_insensitive | 不区分大小写 |
_cs | case_sensitive | 区分大小写 |
_bin | binary | 以二进制方式比较 |
SHOW COLLATION LIKE 'utf8mb4%';
结果如下:
Collation | Charset | Id | Default | Compiled | Sortlen | Pad_attribute |
---|---|---|---|---|---|---|
utf8mb4_0900_ai_ci | utf8mb4 | 255 | Yes | Yes | 0 | NO PAD |
utf8mb4_0900_as_ci | utf8mb4 | 305 | Yes | 0 | NO PAD | |
utf8mb4_0900_as_cs | utf8mb4 | 278 | Yes | 0 | NO PAD | |
utf8mb4_0900_bin | utf8mb4 | 309 | Yes | 1 | NO PAD | |
utf8mb4_bg_0900_ai_ci | utf8mb4 | 318 | Yes | 0 | NO PAD | |
utf8mb4_bg_0900_as_cs | utf8mb4 | 319 | Yes | 0 | NO PAD | |
utf8mb4_bin | utf8mb4 | 46 | Yes | 1 | PAD SPACE | |
utf8mb4_bs_0900_ai_ci | utf8mb4 | 316 | Yes | 0 | NO PAD | |
utf8mb4_bs_0900_as_cs | utf8mb4 | 317 | Yes | 0 | NO PAD | |
utf8mb4_croatian_ci | utf8mb4 | 245 | Yes | 8 | PAD SPACE | |
utf8mb4_cs_0900_ai_ci | utf8mb4 | 266 | Yes | 0 | NO PAD | |
utf8mb4_cs_0900_as_cs | utf8mb4 | 289 | Yes | 0 | NO PAD | |
utf8mb4_czech_ci | utf8mb4 | 234 | Yes | 8 | PAD SPACE | |
utf8mb4_danish_ci | utf8mb4 | 235 | Yes | 8 | PAD SPACE | |
utf8mb4_da_0900_ai_ci | utf8mb4 | 267 | Yes | 0 | NO PAD | |
utf8mb4_da_0900_as_cs | utf8mb4 | 290 | Yes | 0 | NO PAD | |
utf8mb4_de_pb_0900_ai_ci | utf8mb4 | 256 | Yes | 0 | NO PAD | |
utf8mb4_de_pb_0900_as_cs | utf8mb4 | 279 | Yes | 0 | NO PAD | |
utf8mb4_eo_0900_ai_ci | utf8mb4 | 273 | Yes | 0 | NO PAD | |
utf8mb4_eo_0900_as_cs | utf8mb4 | 296 | Yes | 0 | NO PAD | |
utf8mb4_esperanto_ci | utf8mb4 | 241 | Yes | 8 | PAD SPACE | |
utf8mb4_estonian_ci | utf8mb4 | 230 | Yes | 8 | PAD SPACE | |
utf8mb4_es_0900_ai_ci | utf8mb4 | 263 | Yes | 0 | NO PAD | |
utf8mb4_es_0900_as_cs | utf8mb4 | 286 | Yes | 0 | NO PAD | |
utf8mb4_es_trad_0900_ai_ci | utf8mb4 | 270 | Yes | 0 | NO PAD | |
utf8mb4_es_trad_0900_as_cs | utf8mb4 | 293 | Yes | 0 | NO PAD | |
utf8mb4_et_0900_ai_ci | utf8mb4 | 262 | Yes | 0 | NO PAD | |
utf8mb4_et_0900_as_cs | utf8mb4 | 285 | Yes | 0 | NO PAD | |
utf8mb4_general_ci | utf8mb4 | 45 | Yes | 1 | PAD SPACE | |
utf8mb4_german2_ci | utf8mb4 | 244 | Yes | 8 | PAD SPACE | |
utf8mb4_gl_0900_ai_ci | utf8mb4 | 320 | Yes | 0 | NO PAD | |
utf8mb4_gl_0900_as_cs | utf8mb4 | 321 | Yes | 0 | NO PAD | |
utf8mb4_hr_0900_ai_ci | utf8mb4 | 275 | Yes | 0 | NO PAD | |
utf8mb4_hr_0900_as_cs | utf8mb4 | 298 | Yes | 0 | NO PAD | |
utf8mb4_hungarian_ci | utf8mb4 | 242 | Yes | 8 | PAD SPACE | |
utf8mb4_hu_0900_ai_ci | utf8mb4 | 274 | Yes | 0 | NO PAD | |
utf8mb4_hu_0900_as_cs | utf8mb4 | 297 | Yes | 0 | NO PAD | |
utf8mb4_icelandic_ci | utf8mb4 | 225 | Yes | 8 | PAD SPACE | |
utf8mb4_is_0900_ai_ci | utf8mb4 | 257 | Yes | 0 | NO PAD | |
utf8mb4_is_0900_as_cs | utf8mb4 | 280 | Yes | 0 | NO PAD | |
utf8mb4_ja_0900_as_cs | utf8mb4 | 303 | Yes | 0 | NO PAD | |
utf8mb4_ja_0900_as_cs_ks | utf8mb4 | 304 | Yes | 24 | NO PAD | |
utf8mb4_latvian_ci | utf8mb4 | 226 | Yes | 8 | PAD SPACE | |
utf8mb4_la_0900_ai_ci | utf8mb4 | 271 | Yes | 0 | NO PAD | |
utf8mb4_la_0900_as_cs | utf8mb4 | 294 | Yes | 0 | NO PAD | |
utf8mb4_lithuanian_ci | utf8mb4 | 236 | Yes | 8 | PAD SPACE | |
utf8mb4_lt_0900_ai_ci | utf8mb4 | 268 | Yes | 0 | NO PAD | |
utf8mb4_lt_0900_as_cs | utf8mb4 | 291 | Yes | 0 | NO PAD | |
utf8mb4_lv_0900_ai_ci | utf8mb4 | 258 | Yes | 0 | NO PAD | |
utf8mb4_lv_0900_as_cs | utf8mb4 | 281 | Yes | 0 | NO PAD | |
utf8mb4_mn_cyrl_0900_ai_ci | utf8mb4 | 322 | Yes | 0 | NO PAD | |
utf8mb4_mn_cyrl_0900_as_cs | utf8mb4 | 323 | Yes | 0 | NO PAD | |
utf8mb4_nb_0900_ai_ci | utf8mb4 | 310 | Yes | 0 | NO PAD | |
utf8mb4_nb_0900_as_cs | utf8mb4 | 311 | Yes | 0 | NO PAD | |
utf8mb4_nn_0900_ai_ci | utf8mb4 | 312 | Yes | 0 | NO PAD | |
utf8mb4_nn_0900_as_cs | utf8mb4 | 313 | Yes | 0 | NO PAD | |
utf8mb4_persian_ci | utf8mb4 | 240 | Yes | 8 | PAD SPACE | |
utf8mb4_pl_0900_ai_ci | utf8mb4 | 261 | Yes | 0 | NO PAD | |
utf8mb4_pl_0900_as_cs | utf8mb4 | 284 | Yes | 0 | NO PAD | |
utf8mb4_polish_ci | utf8mb4 | 229 | Yes | 8 | PAD SPACE | |
utf8mb4_romanian_ci | utf8mb4 | 227 | Yes | 8 | PAD SPACE | |
utf8mb4_roman_ci | utf8mb4 | 239 | Yes | 8 | PAD SPACE | |
utf8mb4_ro_0900_ai_ci | utf8mb4 | 259 | Yes | 0 | NO PAD | |
utf8mb4_ro_0900_as_cs | utf8mb4 | 282 | Yes | 0 | NO PAD | |
utf8mb4_ru_0900_ai_ci | utf8mb4 | 306 | Yes | 0 | NO PAD | |
utf8mb4_ru_0900_as_cs | utf8mb4 | 307 | Yes | 0 | NO PAD | |
utf8mb4_sinhala_ci | utf8mb4 | 243 | Yes | 8 | PAD SPACE | |
utf8mb4_sk_0900_ai_ci | utf8mb4 | 269 | Yes | 0 | NO PAD | |
utf8mb4_sk_0900_as_cs | utf8mb4 | 292 | Yes | 0 | NO PAD | |
utf8mb4_slovak_ci | utf8mb4 | 237 | Yes | 8 | PAD SPACE | |
utf8mb4_slovenian_ci | utf8mb4 | 228 | Yes | 8 | PAD SPACE | |
utf8mb4_sl_0900_ai_ci | utf8mb4 | 260 | Yes | 0 | NO PAD | |
utf8mb4_sl_0900_as_cs | utf8mb4 | 283 | Yes | 0 | NO PAD | |
utf8mb4_spanish2_ci | utf8mb4 | 238 | Yes | 8 | PAD SPACE | |
utf8mb4_spanish_ci | utf8mb4 | 231 | Yes | 8 | PAD SPACE | |
utf8mb4_sr_latn_0900_ai_ci | utf8mb4 | 314 | Yes | 0 | NO PAD | |
utf8mb4_sr_latn_0900_as_cs | utf8mb4 | 315 | Yes | 0 | NO PAD | |
utf8mb4_sv_0900_ai_ci | utf8mb4 | 264 | Yes | 0 | NO PAD | |
utf8mb4_sv_0900_as_cs | utf8mb4 | 287 | Yes | 0 | NO PAD | |
utf8mb4_swedish_ci | utf8mb4 | 232 | Yes | 8 | PAD SPACE | |
utf8mb4_tr_0900_ai_ci | utf8mb4 | 265 | Yes | 0 | NO PAD | |
utf8mb4_tr_0900_as_cs | utf8mb4 | 288 | Yes | 0 | NO PAD | |
utf8mb4_turkish_ci | utf8mb4 | 233 | Yes | 8 | PAD SPACE | |
utf8mb4_unicode_520_ci | utf8mb4 | 246 | Yes | 8 | PAD SPACE | |
utf8mb4_unicode_ci | utf8mb4 | 224 | Yes | 8 | PAD SPACE | |
utf8mb4_vietnamese_ci | utf8mb4 | 247 | Yes | 8 | PAD SPACE | |
utf8mb4_vi_0900_ai_ci | utf8mb4 | 277 | Yes | 0 | NO PAD | |
utf8mb4_vi_0900_as_cs | utf8mb4 | 300 | Yes | 0 | NO PAD | |
utf8mb4_zh_0900_as_cs | utf8mb4 | 308 | Yes | 0 | NO PAD |
SHOW VARIABLES LIKE '%_server';
结果如下
Variable_name | Value |
---|---|
character_set_server | utf8mb4 |
collation_server | utf8mb4_0900_ai_ci |
innodb_dedicated_server | OFF |
ALTER DATABASE test2 CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
查看:
SHOW CREATE DATABASE test2;
结果如下:
Database | Create Database |
---|---|
test2 | CREATE DATABASE test2 /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci / /!80016 DEFAULT ENCRYPTION=‘N’ */ |
utf8mb4_general_ci 和 utf8mb4_unicode_ci 都是比较常用的两种字符比较方式。
一般情况使用 utf8mb4_general_ci 就足够用了,但是在有德语、法语或俄语的时候一定要使用 utf8mb4_unicode_ci。
SHOW TABLE STATUS FROM test2 LIKE 'user';
总体来说 Mysql 的字符集有 4 个级别服务级别、数据库级别、表级别、列级别。确定某条数据的以最小级别为主,如表的字符集为 utf8,而字段的字符集为 gbk,则该字段以 gbk 进行编码。在创建数据库、表、列的时候如果不确定其字符集,则以上一个字符集级别为准。
如果想要修改服务级别的字符集和字符排序方式,windows 修改 my.ini 文件,linux 修改 my.cnf 文件。修改后对原来的数据库与表没有影响,如果想要修改之前的数据库与表的字符集或字符排序方式则仍需要使用 alter 语句进行修改。
在 Mysql 中 utf8 被分为 utf8mb3 与 utf8mb4 两种,utf8mb3 可以满足多部份场景,对 emoj 表情不识别,utf8mb4 满足所有场景,缺点是比较占用内存。
utf8 的字符集排序方式常用的有两个 general_ci 和 unicode_ci,general_ci 速度比较快,但是德语、法语、俄语的准确度有影响,需要使用 unicode_ci。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。