0. 不用k8s了, 要直接安装MariaDB, 并设置真正的utf-8, MariaDB的utf-8只有3个字节, 不是真正的utf-8, utf-8mb4才是.
1. 安装MariaDB. 二种方式, 使用默认源和自建官方源.
1. 使用默认源
yum -y install mariadb-server mariadb-client
- 安装后执行, mysql_secure_installation是初始化
systemctl enable mariadb
systemctl start mariadb
mysql_secure_installation
2. 使用自建官方源, 可安装MariaDB 10.5稳定版. 官方文档地址
- vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.5 CentOS repository list - created 2021-01-14 03:58 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.5/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
- 再执行安装命令即可
yum install -y mariadb-server mariadb-client
- 安装后执行, mysql_secure_installation是初始化.
systemctl enable mariadb
systemctl start mariadb
mysql_secure_installation
2. 设置MariaDB字符集. 需要设置4个文件my.cnf, client.cnf, mysql-clients.cnf, server.cnf.
- vim /etc/my.cnf 增加以下内容
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
- vim /etc/my.cnf.d/client.cnf 在[client]后增加 default-character-set=utf8
[client]
default-character-set=utf8mb4
- vim /etc/my.cnf.d/mysql-clients.cnf在[mysql]增加default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
- vim /etc/my.cnf.d/server.cnf 在[server]后增加character-set-server=utf8mb4
[server]
character-set-server=utf8mb4
3. 重启MariaDB, 连接数据库,检查字符集.
- 重启MariaDB
systemctl restart mariadb
- 连接数据库.
# 回车输入密码即可
mysql -uroot -hlocalhost -P3306 -p
- 检查字符集
show variables like "%character%";
show variables like "%collation%";

4. 补充utf8mb4_unicode_ci和utf8mb4_general_ci区别, MySQL可以使用utf8mb4_0900_ai_ci, character_set_system是固定utf8不可更改.
- utf8mb4_unicode_ci和utf8mb4_general_ci区别
utf8mb4_unicode_ci: 基于标准的Unicode来排序和比较,能够在各种语言之间精确排序,在特殊情况下,Unicode排序规则为了能够处理特殊字符的情况,实现了略微复杂的排序算法,所以兼容度比较高,但是性能不高。
utf8mb4_general_ci: 没有实现Unicode排序规则,在遇到某些特殊语言或者字符集,排序结果可能不一致,但是在比较和排序的时候速度更快。
- MySQL可以使用utf8mb4_0900_ai_ci, 对应位置替换,show variables like “%character%";与MariaDB一致,show variables like “%collation%";有区别
collation_connection utf8mb4_0900_ai_ci
collation_database utf8mb4_general_ci
collation_server utf8mb4_0900_ai_ci
default_collation_for_utf8mb4 utf8mb4_0900_ai_ci
放个图
-
character_set_system固定utf8格式, 不可更改.
-
创建用户和授权, 全部本地远程访问%代替, ssh方式使用固定ip代替localhost.
CREATE DATABASE testdatabase;
CREATE USER 'testuser'@'%' IDENTIFIED BY 'password';
GRANT ALL ON testdatabase.* TO 'testuser'@'%';
FLUSH PRIVILEGES;