MariaDB 从 Yum 源安装与配置

slug
mariadb-setup-from-repo
tags
mariadb
yum
date
Aug 7, 2024
summary
使用yum 包管理器安装与配置 MariaDB。
status
Published
type
Post

一、安装 MariaDB

1.1. 添加 MariaDB Yum 源

方法一:官方一键设置脚本(推荐)
curl -sS <https://downloads.mariadb.com/MariaDB/mariadb_repo_setup> | bash
方法二:命令行工具
使用 MariaDB Repository Configuration Tool 生成一份配置,保存为 /etc/yum.repos.d/mariadb.repo
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
然后执行以下命令替换源地址:
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

sudo yum install -y MariaDB-server MariaDB-client MariaDB-shared sudo apt-get install mariadb-server mariadb-client MariaDB-shared
RHEL 8
yum module install mariadb -y rpm -qi mariadb-server systemctl enable --now mariadb
MariaDB MaxScale:(选做)
sudo yum install maxscale sudo apt-get install maxscale
Percona XtraBackup:
sudo yum install percona-xtrabackup sudo apt-get install percona-xtrabackup
cracklib_password_check:
sudo yum install MariaDB-cracklib-password-check

1.3. 启动 MariaDB

sudo systemctl start mariadb sudo systemctl enable mariadb

二、设置 MariaDB

2.1. 初始化

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

2.2. 解决 MariaDB 中文乱码

/etc/my.cnf
[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
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
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
# # 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
# # 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
[mariadb] plugin-load-add=auth_gssapi.so
/etc/my.cnf.d/enable_encryption.preset
# # !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

mysqladmin -u root -p version
查看效果
mysql -uroot -p SHOW VARIABLES WHERE Variable_name LIKE 'character\\_set\\_%' OR Variable_name LIKE 'collation%';
参考配置文件:/usr/share/mysql/ 数据文件夹:/var/lib/mysql/ 执行文件:/usr/bin/mysql
参考文档:
对于本文内容有任何疑问, 可与我联系.