使用默认的 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 = MariaDBbaseurl = http://yum.mariadb.org/10.3/centos7-amd64gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDBgpgcheck=11.3 yum makecache fast1.4 yum install MariaDB-server MariaDB-clIEnt -y2 修改默认字符集
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
在[MysqL]中添加
default-character-set=utf8mb42.3 修改/etc/my.conf.d/server.cnf:
在[MysqLd]中添加
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-handshake2.4 全部配置完成,重启mariadb
systemctl restart mariadb2.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即可
数据库配置:
@Configurationpublic 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的安装及相关配置全部内容,希望文章能够帮你解决Mariadb的安装及相关配置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)