MariaDB 从 Yum 源安装与配置

目录

一、安装 MariaDB

https://lug.ustc.edu.cn/wiki/mirrors/help/mariadb

1.1. 添加 MariaDB Yum 源

方法一:官方一键设置脚本(推荐)

shell
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash

方法二:命令行工具

使用 MariaDB Repository Configuration Tool 生成一份配置,保存为 /etc/yum.repos.d/mariadb.repo

shell
cat >/etc/yum.repos.d/mariadb.repo<<'EOF'
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos8-amd64
module_hotfixes=1
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOF

然后执行以下命令替换源地址:

shell
sudo sed -i 's#yum\.mariadb\.org#mirrors.ustc.edu.cn/mariadb/yum#' /etc/yum.repos.d/mariadb.repo
# 建议使用 HTTPS
sudo sed -i 's#http://mirrors\.ustc\.edu\.cn#https://mirrors.ustc.edu.cn#g' /etc/yum.repos.d/mariadb.repo

sudo dnf install MariaDB-server
sudo systemctl start mariadb

1.2. 安装 MariaDB

shell
sudo yum install -y MariaDB-server MariaDB-client MariaDB-shared
sudo apt-get install mariadb-server mariadb-client MariaDB-shared

RHEL 8

shell
yum module install mariadb -y
rpm -qi mariadb-server
systemctl enable --now mariadb

MariaDB MaxScale:(选做)

shell
sudo yum install maxscale
sudo apt-get install maxscale

Percona XtraBackup:

shell
sudo yum install percona-xtrabackup
sudo apt-get install percona-xtrabackup

cracklib_password_check:

https://mariadb.com/kb/en/cracklib-password-check-plugin/

shell
sudo yum install MariaDB-cracklib-password-check

1.3. 启动 MariaDB

shell
sudo systemctl start mariadb
sudo systemctl enable mariadb

二、设置 MariaDB

2.1. 初始化

shell
mysql_secure_installation
  1. 初始密码为空,直接回车。
  2. 设置 root 密码
  3. 是否移除匿名用户
  4. 是否禁止 root 远程登录
  5. 是否删除 ’test’ 测试数据库

2.2. 解决 MariaDB 中文乱码

https://mariadb.com/kb/en/mariadb/setting-character-sets-and-collations/

/etc/my.cnf

shell
[client]
...
default-character-set = utf8mb4
...
[mysql]
...
default-character-set = utf8mb4

...
[mysqld]
...
character-set-server  = utf8mb4
character-set-filesystem = utf8mb4
collation-server      = utf8mb4_general_ci
...

2.3. 完整配置文件

vim /etc/my.cnf

shell
cat >/etc/my.cnf<<'EOF'
#
# This group is read both by the client and the server
#
[client-server]
port=3306
socket=/dev/shm/mysql.sock

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
EOF

vim /etc/my.cnf.d/client.cnf

shell
cat >/etc/my.cnf.d/client.cnf<<'EOF'
#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#
[client]
default-character-set = utf8mb4

#
# This group is not read by mysql client library,
#
[client-mariadb]
EOF

/etc/my.cnf.d/mariadb-server.cnf

shell
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#

# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld/mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
character-set-server=utf8mb4
character-set-system=utf8mb4
character-set-filesystem=utf8mb4
collation-server=utf8mb4_general_ci

#
# * Galera-related settings
#
[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
#bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0

# this is only for embedded server
[embedded]

# This group is only read by MariaDB servers, not by MySQL.
# If you use the same .cnf file for MySQL and MariaDB,
# you can put MariaDB-only options here
[mariadb]

# This group is only read by MariaDB-10.3 servers.
# If you use the same .cnf file for MariaDB of different versions,
# use this group for options that older servers don't understand
[mariadb-10.3]

/etc/my.cnf.d/mysql-clients.cnf

shell
#
# These groups are read by MariaDB command-line tools
# Use it for options that affect only one utility
#

[mysql]
default-character-set=utf8mb4

[mysql_upgrade]

[mysqladmin]

[mysqlbinlog]

[mysqlcheck]

[mysqldump]

[mysqlimport]

[mysqlshow]

[mysqlslap]

/etc/my.cnf.d/auth_gssapi.cnf

shell
[mariadb]
plugin-load-add=auth_gssapi.so

/etc/my.cnf.d/enable_encryption.preset

shell
#
# !include this file into your my.cnf (or any of *.cnf files in /etc/my.cnf.d)
# and it will enable data at rest encryption. This is a simple way to
# ensure that everything that can be encrypted will be and your
# data will not leak unencrypted.
#
# DO NOT EDIT THIS FILE! On MariaDB upgrades it might be replaced with a
# newer version and your edits will be lost. Instead, add your edits
# to the .cnf file after the !include directive.
#
# NOTE that you also need to install an encryption plugin for the encryption
# to work. See https://mariadb.com/kb/en/mariadb/data-at-rest-encryption/#encryption-key-management
#
[mariadb]
aria-encrypt-tables
encrypt-binlog
encrypt-tmp-disk-tables
encrypt-tmp-files
loose-innodb-encrypt-log
loose-innodb-encrypt-tables

三、测试 MariaDB

shell
mysqladmin -u root -p version

查看效果

shell
mysql -uroot -p
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
other
参考配置文件:/usr/share/mysql/
数据文件夹:/var/lib/mysql/
执行文件:/usr/bin/mysql

参考文档:

ESC