1 安装Mariadb
使用默认的 yum install mariadb-server时,默认安装的是和MysqL相兼容的5.5版本。想要安装更高的版本,必须先指定yum源。
1.1 使用源配置向导
配置向导: https://downloads.mariadb.org/mariadb/repositories
可以选择合适的操作系统和想要安装的Mariadb版本,然后自动生成源配置。
1.2 生成Mariadb.repo
# tee /etc/yum.repos.d/Mariadb.repo << EOF
copy入以下源配置,
# MariaDB 10.3 CentOS repository list - created 2018-08-29 02:43 UTC # http://downloads.mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
1.3 yum makecache fast
1.4 yum install MariaDB-server MariaDB-client -y
2 修改默认字符集
MariaDB默认字符集都是lartin1, 后续调用的时候都需要修改配置文件来修改数据库的默认编码:
2.1 查看当前字符集
SHOW VARIABLES LIKE 'character_set_%'; SHOW VARIABLES LIKE 'collation_%';
查看某个数据库的字符集
show create database DBbase; show create table DBtable;
2.2 修改/etc/my.conf.d/MysqL-clients.cnf
default-character-set=utf8mb4
2.3 修改/etc/my.conf.d/server.cnf:
init_connect='SET collation_connection = utf8mb4_unicode_ci' init_connect='SET NAMES utf8mb4' character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci skip-character-set-client-handshake
2.4 全部配置完成,重启mariadb
systemctl restart mariadb
2.5 查看编码
show variables like "%character%";
and
show variables like "%collation%";
MariaDB [(none)]> 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 | utf8mb4 | | character_sets_dir | /usr/share/MysqL/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.001 sec) MariaDB [(none)]> show variables like "%collation%"; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8mb4_unicode_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +----------------------+-----------------+ 3 rows in set (0.001 sec)
3 应用
3.1 Springboot中使用utf8mb4字符集
第一种:在JPA建表是设置表的编码和排序规则
重写MysqL5InnoDBDialect#getTableTypeString()
public class MysqL5InnoDBDialectUtf8mb4 extends MysqL5InnoDBDialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci"; } }
配置hibernate.dialect
spring: jpa: properties: hibernate: dialect: com.xxx.MysqL5InnoDBDialectUtf8mb4
第二种:设置链接初始化sql
配置Druid连接池,如果为其他连接池,设置对应的connectionInitsqls即可
数据库配置:
@Configuration public class DruidConfig { @Value("${spring.datasource.druid.connection-init-sqls") private List connectionInitsqls; @Bean public DruidDataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setConnectionInitsqls(connectionInitsqls); return dataSource; } }
application.yml
spring: datasource: druid: connection-init-sqls: ["SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"]
最后按上述设置Spring Data Jpa 生成的表、字段就会是utf8mb4,utf8mb4_unicode_ci
3.2 MysqL的URL配置
还是需要设置characterEncoding=utf8mb4,不然会出现中文乱码
原文链接:/mariadb/413462.html