当前位置: 首页 > 工具软件 > MySQL Maria > 使用案例 >

Mysql学习笔记(二):Mysql(MariaDB)的编码级别和比较级别

张心水
2023-12-01

之前在CentOS7.8上完成了MariaDB的安装,本以为万事大吉,但是在愉快的建表以后,用客户端连接数据库时发现插入的中文数据乱码了,哎,以前光知道写sql去了,没怎么关注编码的问题,乘着这次机会,正好整理下MySQL(MariaDB同理)的编码问题。

Mysql编码级别分为:system、server、database、table、column,还有3个和客户端通讯的client、connection、result。

先看下安装的MySQL各个级别的编码变量

MariaDB [(none)]> 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/ |
+--------------------------+----------------------------+
8 rows in set (0.001 sec)

MariaDB [(none)]> 

Mysql编码层级

1.system

系统编码级别,没啥介绍的,就是当前操作系统使用的编码,应该是上面的character_set_system,一般都是utf8格式的,也不会去修改这个东西,不需要过多关注。

2.server

服务编码级别,即当前MySQL服务所使用的编码,对应上面的character_set_server,可以看到,我现在的编码是latin1,需要修改成UTF-8格式的

3.database

数据库级别,对应上面的character_set_database。如果新建数据库时未指定编码,则使用对应的编码。

4.table

表级别。在建表时指定编码,只对当前表所有未指定编码的列生效(因为下面还可以指定列编码,指定后表级别的编码对该列就不生效),这个指定是mysql独有的,而且只能通过sql在建表或修改表时指定, 在标准sql中,没有可指定表编码的sql语法

5.column

列级别,设置列的编码。建表或修改列时设置,标准sql语法。

Mysql与客户端通讯编码

通讯流程如下:

1。服务端使用系统变量character_set_client来处理客户端发来的语句

2。服务端会把客户端发来的语句(以character_set_client 编码)转换为character_set_connection编码。

3。系统变量character_set_results用来把数据以该编码方式返回给客户端。

了解完这些概念后,我们接着来改一下编码。

[root@VM-0-16-centos etc]# mysql --help|grep -A1 -i 'default option' #查看默认安装加载的配置文件
Default options are read from the following files in the given order:
/etc/my.cnf ~/.my.cnf 
--
--no-defaults             Don't read default options from any option file.
The following specify which files/extra groups are read (specified before remaining options):
--defaults-file=#         Only read default options from the given file #.
--defaults-extra-file=#   Read this file after the global files are read.
[root@VM-0-16-centos etc]# vi /etc/my.cnf #修改默认编码

[mysqld]
character-set-server = utf8

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

修改后重启MySQL服务:

[root@VM-0-16-centos etc]# systemctl restart mariadb
[root@VM-0-16-centos etc]# systemctl status mariadb
● mariadb.service - MariaDB 10.3.27 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/mariadb.service.d
           └─migrated-from-my.cnf-settings.conf
   Active: active (running) since Wed 2020-12-23 14:57:44 CST; 10s ago
···
[root@VM-0-16-centos etc]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show variables like 'char%';
+--------------------------+----------------------------+
| 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.001 sec)

MariaDB [(none)]> show variables like 'colla%';
+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.001 sec)

MariaDB [(none)]>

好了,到这里,MySQL的编码设置就结束了,谢谢观看!

 类似资料: