字符集修改
- 一 在mysql5.7中输入汉字报错
- 二 修改针对修改后创建的数据库,过去创建的数据库不算
- 三 想要单独修改
- 四 基本的mysql命令
- 五 各级别的字符集
一 在mysql5.7中输入汉字报错
# 在5,7中,默认字符集为latin1,开发人员在数据库设计时,需要手动
# 将编码改成utf8,没有修改,就会出现迁移数据库时报错乱码的问题
#
SHOW VARIABLES LIKE 'character%';
SHOW VARIABLES LIKE '%char%';
USE test1;
CREATE TABLE emp1(id INT,lname VARCHAR(15));
SELECT * FROM emp1;
INSERT INTO emp1(id,lname) VALUES (1,'Tom');
INSERT INTO emp1(id,lname) VALUES (2,'康师傅');
错误
1 queries executed, 0 success, 1 errors, 0 warnings
查询:nsert into emp1(id,lname) values (2,'康师傅')
错误代码: 1064
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near 'nsert into emp1(id,lname) values (2,'康师傅')' at line 1
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec
二 修改针对修改后创建的数据库,过去创建的数据库不算
改环境
vim /etc/my.cnf
systemctl restart mysqld.service
vim时候,主要在表末尾加入
character_set_server=utf8
执行下面指令,得到一个表,证明类型修改成功
show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
执行下面指令,正面在修改配置环境之前创建的数据库,它的类型不会改变
mysql> show create database test3;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| test3 | CREATE DATABASE `test3` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
三 想要单独修改
mysql> alter database test1 character set 'utf8';
Query OK, 1 row affected (0.00 sec)
mysql> show create database test1;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| test1 | CREATE DATABASE `test1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use test1;
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 emp1 convert to character set 'utf8';
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
四 基本的mysql命令
create database test1;
use test1;
create table emp1(age int,name varchar(15));
insert into emp1(age, name) values (1,'康师傅');
mysql> select * from emp1;
+------+-----------+
| age | name |
+------+-----------+
| 1 | 康师傅 |
+------+-----------+
五 各级别的字符集
上一层默认决定下一层
服务器
数据库
表级别
列级别
utf8决定一个字符需要1到4个字节,
可能说三个字节就能满足大多数数据的存储需要,同时兼顾存储性能,因此默认是utf8mb3
但是一些特殊的字符需要四个字节,要用utf8mb4
mysql> show variables like '%character%'
-> ;
+--------------------------+----------------------------+
| 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 | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
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 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
客户端把查询的信息发给服务器处理的具体过程,需要经过一个编码的翻译再转码的过程