1. 首页 > 软件安装教程 > 正文

MariaDB安装-MariaDB 10.6 for RHEL 9安装配置及升级迁移详细过程

1. 硬件环境检查

在安装MariaDB之前,必须对服务器的硬件环境进行全面检查,确保满足MariaDB 10.6的最低要求。更多学习教程www.fgedu.net.cn

# 检查内存大小
# free -h
total used free shared buff/cache available
Mem: 16G 2.1G 12G 8.5M 1.8G 13G
Swap: 8G 0B 8G

# 检查磁盘空间
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 8G 0 8G 0% /dev
tmpfs 8G 0 8G 0% /dev/shm
tmpfs 8G 8.5M 8G 1% /run
tmpfs 8G 0 8G 0% /sys/fs/cgroup
/dev/sda1 50G 15G 36G 30% /
/dev/sdb1 200G 20G 180G 10% /data

# 检查CPU核心数
# nproc
8

# 检查系统架构
# uname -m
x86_64

生产环境建议:最小内存4GB(测试环境),生产环境建议16GB以上。磁盘空间:系统盘至少50GB,数据盘根据业务需求配置,建议至少100GB。CPU:测试环境至少4核心,生产环境建议8核心以上。

2. 操作系统检查

MariaDB 10.6支持RHEL 7.3+、RHEL 8.0+、RHEL 9.0+等操作系统。本文以RHEL 9为例。学习交流加群风哥微信: itpux-com

# 检查操作系统版本
# cat /etc/redhat-release
Red Hat Enterprise Linux release 9.0 (Plow)

# 检查内核版本
# uname -r
5.14.0-70.22.1.el9_0.x86_64

# 检查SELinux状态
# getenforce
Disabled

# 关闭SELinux(如未关闭)
# vi /etc/selinux/config
SELINUX=disabled

# 检查防火墙状态
# systemctl status firewalld

# 关闭防火墙(生产环境建议开放特定端口而非完全关闭)
# systemctl stop firewalld
# systemctl disable firewalld

3. 安装准备

在安装MariaDB之前,需要进行一系列准备工作,包括配置YUM仓库、创建用户和组、配置网络等。

# 配置MariaDB YUM仓库
# vi /etc/yum.repos.d/MariaDB.repo

[mariadb]
name = MariaDB
baseurl = https://yum.mariadb.org/10.6/rhel/9/x86_64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

# 清理YUM缓存
# yum clean all

# 创建数据目录
# mkdir -p /data/mariadb
# chown -R mysql:mysql /data/mariadb

# 配置网络
# 确保服务器IP地址为192.168.1.51
# ip addr show eth0
2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000
inet 192.168.1.51/24 brd 192.168.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe12:3456/64 scope link noprefixroute
valid_lft forever preferred_lft forever

风哥提示:MariaDB安装需要root权限,建议使用root账户进行安装。

4. MariaDB软件安装

现在开始安装MariaDB 10.6软件,按照以下步骤进行。

# 安装MariaDB
# yum install -y MariaDB-server MariaDB-client

# 启动MariaDB服务
# systemctl start mariadb

# 设置MariaDB服务开机自启
# systemctl enable mariadb

# 验证MariaDB服务状态
# systemctl status mariadb
● mariadb.service – MariaDB 10.6 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 1min ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 12345 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
Process: 12346 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
Main PID: 12347 (mysqld)
Status: “Taking your SQL requests now…”
Tasks: 30 (limit: 4915)
Memory: 76.0M
CGroup: /system.slice/mariadb.service
└─12347 /usr/sbin/mysqld

Mar 31 10:00:00 fgedudb01 systemd[1]: Starting MariaDB 10.6 database server…
Mar 31 10:00:00 fgedudb01 mariadb-prepare-db-dir[12345]: Database MariaDB is probably initialized in /var/lib/mysql already, nothing is done.
Mar 31 10:00:00 fgedudb01 systemd[1]: Started MariaDB 10.6 database server.

5. 数据库配置

配置MariaDB数据库,包括安全设置、参数配置等。

# 运行安全配置脚本
# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we’ll need the current
password for the root user. If you’ve just installed MariaDB, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
To log into MariaDB without having to have a user account created for
Them. This is intended only for testing, and to make the installation
Go a bit smoother. You should remove them before moving into a
Production environment.

Remove anonymous users? [Y/n] Y
… Success!

Normally, root should only be allowed to connect from ‘localhost’. This
Ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
… Success!

By default, MariaDB comes with a database named ‘test’ that anyone can
Access. This is also intended only for testing, and should be removed
Before moving into a production environment.

Remove test database and access to it? [Y/n] Y
– Dropping test database…
… Success!
– Removing privileges on test database…
… Success!

Reloading the privilege tables will ensure that all changes made so far
Will take effect immediately.

Reload privilege tables now? [Y/n] Y
… Success!

Cleaning up…

All done! If you’ve completed all of the above steps, your MariaDB
Installation should now be secure.

Thanks for using MariaDB!

# 编辑MariaDB配置文件
# vi /etc/my.cnf.d/server.cnf

# 添加以下配置
[mysqld]
datadir=/data/mariadb
socket=/var/lib/mysql/mysql.sock
bind-address=0.0.0.0
port=3306
max_connections=1000
innodb_buffer_pool_size=4G
innodb_log_file_size=512M
innodb_file_per_table=1
skip-name-resolve
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci

# 重启MariaDB服务
# systemctl restart mariadb

# 验证配置
# mysql -u root -p -e “SHOW VARIABLES LIKE ‘datadir’;”
Enter password:
+—————+—————-+| Variable_name | Value |+—————+—————-+| datadir | /data/mariadb/ |+—————+—————-+

6. 测试验证

创建测试数据库和表,验证MariaDB功能正常。学习交流加群风哥QQ113257174

# 连接MariaDB
# mysql -u root -p

# 创建数据库
MariaDB [(none)]> CREATE DATABASE fgedudb;

# 创建用户
MariaDB [(none)]> CREATE USER ‘fgedu’@’%’ IDENTIFIED BY ‘fgedu123’;

# 授权
MariaDB [(none)]> GRANT ALL PRIVILEGES ON fgedudb.* TO ‘fgedu’@’%’;

# 刷新权限
MariaDB [(none)]> FLUSH PRIVILEGES;

# 连接到测试数据库
MariaDB [(none)]> USE fgedudb;

# 创建测试表
MariaDB [fgedudb]> CREATE TABLE fgedu_employees (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(100),
-> salary DECIMAL(10,2)
-> );

# 插入测试数据
MariaDB [fgedudb]> INSERT INTO fgedu_employees (name, salary) VALUES (‘张三’, 5000.00);
MariaDB [fgedudb]> INSERT INTO fgedu_employees (name, salary) VALUES (‘李四’, 6000.00);
MariaDB [fgedudb]> INSERT INTO fgedu_employees (name, salary) VALUES (‘王五’, 7000.00);

# 查询测试数据
MariaDB [fgedudb]> SELECT * FROM fgedu_employees;
+—-+——+——–+
| id | name | salary |
+—-+——+——–+
| 1 | 张三 | 5000.0 |
| 2 | 李四 | 6000.0 |
| 3 | 王五 | 7000.0 |
+—-+——+——–+

7. 备份配置

配置MariaDB数据库备份策略,确保数据安全。

# 创建备份目录
# mkdir -p /backup/mariadb
# chown -R mysql:mysql /backup/mariadb

# 执行备份
# 以root用户执行
# mysqldump -u root -p fgedudb > /backup/mariadb/fgedudb_backup_20260331.sql

# 验证备份文件
# ls -la /backup/mariadb/
-rw-r–r– 1 root root 123456 Mar 31 10:20 fgedudb_backup_20260331.sql

# 配置自动备份
# 创建备份脚本
# vi /root/backup_mariadb.sh

#!/bin/bash

DATE=$(date +%Y%m%d)
BACKUP_DIR=”/backup/mariadb”

# 创建备份目录(如果不存在)
mkdir -p $BACKUP_DIR

# 执行备份
mysqldump -u root -p’your_password’ fgedudb > $BACKUP_DIR/fgedudb_backup_$DATE.sql

# 保留最近7天的备份
find $BACKUP_DIR -name “fgedudb_backup_*.sql” -mtime +7 -delete

# 给脚本添加执行权限
# chmod +x /root/backup_mariadb.sh

# 添加到crontab
# crontab -e

# 添加以下内容(每天凌晨2点执行备份)
0 2 * * * /root/backup_mariadb.sh

8. 升级迁移

MariaDB数据库的升级和迁移过程。

# 升级MariaDB版本
# 1. 备份数据库
# mysqldump -u root -p –all-databases > /backup/mariadb/all_databases_backup.sql

# 2. 停止MariaDB服务
# systemctl stop mariadb

# 3. 升级MariaDB
# yum update -y MariaDB-server MariaDB-client

# 4. 启动MariaDB服务
# systemctl start mariadb

# 5. 运行升级脚本
# mysql_upgrade -u root -p

# 6. 验证升级结果
# mysql -u root -p -e “SELECT VERSION();”
Enter password:
+—————-+| VERSION() |+—————-+| 10.6.15-MariaDB |+—————-+

# 数据库迁移
# 从旧版本MariaDB迁移到MariaDB 10.6

# 1. 在旧系统上创建备份
# mysqldump -u root -p –all-databases > /backup/mariadb/all_databases_backup.sql

# 2. 将备份文件复制到新系统
# scp /backup/mariadb/all_databases_backup.sql root@192.168.1.51:/backup/mariadb/

# 3. 在新系统上恢复备份
# mysql -u root -p < /backup/mariadb/all_databases_backup.sql # 4. 验证迁移结果 # mysql -u root -p -e "SELECT * FROM fgedudb.fgedu_employees;"

风哥提示:数据库升级和迁移前,建议先在测试环境进行演练,确保升级过程顺利。同时,备份所有重要数据,以防万一。

9. 总结

本文详细介绍了MariaDB 10.6 for RHEL 9的安装、配置、升级和迁移过程。通过按照本文的步骤操作,可以成功部署MariaDB数据库并确保其稳定运行。from:fengge

生产环境建议:
– 定期备份数据库,建议每天执行一次全备份
– 监控数据库性能,定期检查数据库日志和错误信息
– 定期更新MariaDB版本,确保系统安全性和性能
– 合理规划数据库存储,避免空间不足
– 配置合适的参数,优化数据库性能
– 对于生产环境,建议部署主从复制架构,提高可用性和可靠性

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息