跳过内容

第 4.1 部分 数据库服务器 MariaDB

MySQL、MariaDB 和 PostgreSQL 都是开源 RDBMS(关系型数据库管理系统)。

MariaDB 和 MySQL

在本节中,您将学习有关 RDBMS MariaDB 和 MySQL 的知识。


目标: 您将学习如何

✔ 安装、配置和保护 MariaDB 服务器和 MySQL 服务器;
✔ 对数据库和用户执行一些管理操作。

🏁 RDBMS数据库MariaDBMySQL

知识: ⭐ ⭐ ⭐
复杂度: ⭐ ⭐ ⭐

阅读时间: 30 分钟


概述

MySQL 由 Michael "Monty" Widenius(一位芬兰计算机科学家)开发,他在 1995 年创立了 MySQL AB。MySQL AB 于 2008 年被 SUN 收购,后者又在 2009 年被 Oracle 收购。Oracle 仍然拥有 MySQL 软件,并在双重 GPL 和专有许可下分发。

2009 年,Michael Widenius 离开 SUN,创立了 Monty Program AB,并启动了其 MySQL 社区分支 MariaDB 的开发,该分支在 GPL 许可下发布。MariaDB 基金会管理该项目,并确保它保持免费。

不久之后,大多数 Linux 发行版开始提供 MariaDB 软件包而不是 MySQL 软件包,主要的帐户(如维基百科和谷歌)也采用了社区分支。

MySQL 和 MariaDB 是世界上使用最广泛的 RDBMS(在专业人士和公众中),特别是对于 Web 应用程序(LAMP: Linux + Apache + Mysql-MariaDB + Php)。

Mysql-MariaDB 的主要竞争对手是

  • PostgreSQL,
  • OracleDB,
  • Microsoft SQL Server。

数据库服务是多线程和多用户的,可以在大多数操作系统(Linux、Unix、BSD、Mac OSx、Windows)上运行,并且可以从许多编程语言(PHP、Java、Python、C、C++、Perl 等)访问。

支持多种引擎,允许将不同的引擎分配给同一数据库中的不同表,具体取决于要求

MyISAM
最简单,但不支持事务或外键。它是一个索引顺序引擎。MyISAM 现在已弃用。
InnoDB
管理表完整性(外键和事务),但占用更多磁盘空间。自 MySQL 5.6 版起,这已成为默认引擎。它是一个事务引擎。
内存
表存储在内存中。
存档
插入时的压缩数据可以节省磁盘空间,但会减慢搜索查询(冷数据)。

根据需要采用引擎是一个问题:存档用于日志存储,内存用于临时数据,等等。

MariaDB/MySQL 使用端口 3306/TCP 进行网络通信。

本节将处理此版本,因为 Rocky 附带的默认版本是数据库的 MariaDB 社区版本。只特别处理 MySQL 和 MariaDB 之间的差异。

安装

使用 dnf 命令安装 mariadb-server

sudo dnf install -y mariadb-server

默认情况下,在 Rocky 9 上安装的版本是 10.5。

在启动时激活服务并启动它

sudo systemctl enable mariadb --now

您可以检查 mariadb 服务的状态

sudo systemctl status mariadb

要安装更新的版本,您需要使用 dnf 模块

$ sudo dnf module list mariadb
Last metadata expiration check: 0:00:09 ago on Thu Jun 20 11:39:10 2024.
Rocky Linux 9 - AppStream
Name                          Stream                      Profiles                                        Summary
mariadb                       10.11                       client, galera, server [d]                      MariaDB Module

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

如果您尚未安装 mariadb 服务器,激活所需的模块版本就足够了

$ sudo dnf module enable mariadb:10.11
Last metadata expiration check: 0:02:23 ago on Thu Jun 20 11:39:10 2024.
Dependencies resolved.
============================================================================================================================================= Package                          Architecture                    Version                             Repository                        Size
=============================================================================================================================================
Enabling module streams:
 mariadb                                                          10.11

Transaction Summary
=============================================================================================================================================
Is this ok [y/N]: y
Complete!

您现在可以安装软件包。将自动安装所需的版本

sudo dnf install -y mariadb-server

关于默认用户

请注意 mariadb 在首次启动时提供的日志(/var/log/messages

mariadb-prepare-db-dir[6560]: Initializing MariaDB database
mariadb-prepare-db-dir[6599]: Two all-privilege accounts were created.
mariadb-prepare-db-dir[6599]: One is root@localhost, it has no password, but you need to
mariadb-prepare-db-dir[6599]: be system 'root' user to connect. Use, for example, sudo mysql
mariadb-prepare-db-dir[6599]: The second is mysql@localhost, it has no password either, but
mariadb-prepare-db-dir[6599]: you need to be the system 'mysql' user to connect.
mariadb-prepare-db-dir[6599]: After connecting you can set the password, if you would need to be
mariadb-prepare-db-dir[6599]: able to connect as any of these users with a password and without sudo

配置

配置文件位于 /etc/my.cnf/etc/my.cnf.d/ 中。

/etc/my.cnf.d/mariadb-server.cnf 中设置了一些重要的默认选项。

[server]

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
...

如您所见,默认情况下数据存储在 /var/lib/mysql 中。该文件夹可能需要大量存储空间,并且会随着时间的推移而不断增加。因此建议将此文件夹挂载到专用的分区。

安全性

MariaDB 和 MySQL 包含一个脚本,可帮助您保护服务器。例如,它会删除远程 root 登录和示例用户以及安全性较低的默认选项。

使用 mariadb-secure-installation 来保护您的服务器。

sudo mariadb-secure-installation

该脚本将提示您为 root 用户提供密码。

注意

mysql_secure_installation 命令现在是 mariadb-secure-installation 命令的符号链接。

$ ll /usr/bin/mysql_secure_installation
lrwxrwxrwx. 1 root root 27 Oct 12  2023 /usr/bin/mysql_secure_installation -> mariadb-secure-installation

如果您每次使用 MariaDB 命令时都需要提供密码,那么您可以创建一个包含您的凭据的 ~/.my.cnf 文件,MariaDB 将默认使用该文件来连接到您的服务器。

[client]
user="root"
password="#######"

确保权限足够严格,只能允许当前用户访问。

chmod 600 ~/.my.cnf

警告

这不是最好的方法。另一种比将密码存储为纯文本更安全的解决方案。从 MySQL 5.6.6 开始,现在可以使用 mysql_config_editor 命令将您的凭据存储在加密的登录 .mylogin.cnf 中。

如果您的服务器运行防火墙(这是一件好事),您可能需要考虑打开它,但前提是您需要从外部访问您的服务。

sudo firewall-cmd --zone=public --add-service=mysql
sudo firewall-cmd --reload

注意

最好的安全措施是不将数据库服务器开放给外部世界(如果应用程序服务器托管在同一服务器上),或者仅将访问权限限制为授权的 IP 地址。

管理

mariadb 命令

mariadb 命令是一个简单的 SQL shell,支持交互式和非交互式使用。

mysql -u user -p [base]
选项信息
-u 用户名提供要连接的用户名称。
-p要求输入密码。
数据库名要连接的数据库。

注意

mysql 命令现在是 mariadb 命令的符号链接。

$ ll /usr/bin/mysql
lrwxrwxrwx. 1 root root 7 Oct 12  2023 /usr/bin/mysql -> mariadb

示例

$ sudo mariadb -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.003 sec)

mariadb-admin 命令

mariadb-admin 命令是用于管理 MariaDB 服务器的客户端。

mariadb-admin -u user -p command
选项信息
-u 用户名提供要连接的用户名称。
-p要求输入密码。
命令要执行的命令。

mariadb-admin 提供了许多命令,例如 versionvariablesstop-slavestart-slavescreate databasename 等。

示例

mariadb-admin -u root -p version

注意

mysqladmin 命令现在是 mariadb-admin 命令的符号链接。

$ ll /usr/bin/mysqladmin
lrwxrwxrwx. 1 root root 13 Oct 12  2023 /usr/bin/mysqladmin -> mariadb-admin

关于日志

MariaDB 提供了多种日志。

  • **错误日志**:包含服务启动和关闭时生成的消息以及重要事件(警告和错误)。
  • **二进制日志**:此日志(以二进制格式)记录所有修改数据库结构或数据的操作。如果您需要恢复数据库,则需要恢复备份并回放二进制日志以恢复数据库崩溃前的状态。
  • **查询日志**:所有客户端请求都记录在此处。
  • **慢查询日志**:慢查询(即执行时间超过设定时间的查询)将单独记录在此日志中。通过分析此文件,您可以采取措施来缩短执行时间(例如,设置索引或修改客户端应用程序)。

除了二进制日志之外,这些日志都是文本格式,因此可以直接使用!

要启用长时间请求的日志记录,请编辑 my.cnf 配置文件以添加以下行。

slow_query_log      = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time     = 2

long_query_time 变量的最小值为 0,默认值为 10 秒。

重新启动服务以使更改生效。

日志文件填满后,您可以使用 mariadb-dumpslow 命令分析它。

mariadb-dumpslow [options] [log_file ...]
选项信息
-t n仅显示前 n 个查询。
-s 排序类型按查询数量排序。
-r反转结果显示。

排序类型可以是

选项信息
c按请求数量排序。
tat按执行时间或平均执行时间排序(a 代表平均)。
lal按锁定时间或其平均值排序。
raR按返回行数或其平均值排序。

关于备份

与任何 RDBMS 一样,数据库备份是在数据修改处于离线状态时进行的。您可以通过以下方式进行:

  • 停止服务,称为离线备份;
  • 服务运行时,通过暂时锁定更新(挂起所有修改)。这是一种在线备份。
  • 使用 LVM 文件系统的快照,启用冷文件系统的数据备份。

备份格式可以是 ASCII(文本)文件,以 SQL 命令的形式表示数据库及其数据的状态,也可以是与 MySQL 存储文件相对应的二进制文件。

虽然可以使用诸如 tar 或 cpio 等常用实用程序备份二进制文件,但 ASCII 文件需要使用诸如 mariadb-dump 等实用程序。

mariadb-dump 命令可以执行数据库转储。

在该过程中,数据访问将被锁定。

mariadb-dump -u root -p DATABASE_NAME > backup.sql

注意

不要忘记在恢复完整备份后,恢复二进制文件(binlog)才能完成数据的重建。

生成的该文件可用于恢复数据库数据。数据库必须已经存在,或者您必须事先重新创建它!

mariadb -u root -p DATABASE_NAME < backup.sql

图形工具

存在一些图形工具,可以简化数据库数据的管理和管理。以下是一些示例:

研讨会

在本研讨会中,您将安装、配置和保护 MariaDB 服务器。

任务 1:安装

安装 MariaDB-server 包。

$ sudo dnf install mariadb-server
Last metadata expiration check: 0:10:05 ago on Thu Jun 20 11:26:03 2024.
Dependencies resolved.
============================================================================================================================================= Package                                       Architecture            Version                              Repository                  Size
=============================================================================================================================================
Installing:
 mariadb-server                                x86_64                  3:10.5.22-1.el9_2                    appstream                  9.6 M
Installing dependencies:
...

安装会将 mysql 用户添加到系统中,其主目录为 /var/lib/mysql

$ cat /etc/passwd
...
mysql:x:27:27:MySQL Server:/var/lib/mysql:/sbin/nologin
...

启用并启动服务。

$ sudo systemctl enable mariadb --now
Created symlink /etc/systemd/system/mysql.service  /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service  /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service  /usr/lib/systemd/system/mariadb.service.

检查安装。

$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; preset: disabled)
     Active: active (running) since Thu 2024-06-20 11:48:56 CEST; 1min 27s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 6538 ExecStartPre=/usr/libexec/mariadb-check-socket (code=exited, status=0/SUCCESS)
    Process: 6560 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir mariadb.service (code=exited, status=0/SUCCESS)
    Process: 6658 ExecStartPost=/usr/libexec/mariadb-check-upgrade (code=exited, status=0/SUCCESS)
   Main PID: 6643 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 9 (limit: 11110)
     Memory: 79.5M
        CPU: 1.606s
     CGroup: /system.slice/mariadb.service
             └─6643 /usr/libexec/mariadbd --basedir=/usr

Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: The second is mysql@localhost, it has no password either, but
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: you need to be the system 'mysql' user to connect.
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: After connecting you can set the password, if you would need to be
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: able to connect as any of these users with a password and without sudo
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: See the MariaDB Knowledgebase at https://mariadb.com/kb
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: Please report any problems at https://mariadb.org.cn/jira
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: The latest information about MariaDB is available at https://mariadb.org.cn>Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: Consider joining MariaDB's strong and vibrant community:
Jun 20 11:48:56 localhost.localdomain mariadb-prepare-db-dir[6599]: https://mariadb.org.cn/get-involved/
Jun 20 11:48:56 localhost.localdomain systemd[1]: Started MariaDB 10.5 database server.

尝试连接到服务器。

$ sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)

MariaDB [(none)]> exit
Bye
$ sudo mariadb-admin version
mysqladmin  Ver 9.1 Distrib 10.5.22-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.5.22-MariaDB
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 7 min 24 sec

Threads: 1  Questions: 9  Slow queries: 0  Opens: 17  Open tables: 10  Queries per second avg: 0.020

如您所见,root 用户不需要提供密码。您将在下一个任务中纠正此问题。

任务 2:保护您的服务器

启动 mariadb-secure-installation 并按照说明进行操作。

$ sudo mariadb-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
haven't set the root password yet, you should just press enter here.

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

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the 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 -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

$ mariadb -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

配置防火墙。

sudo firewall-cmd --zone=public --add-service=mysql --permanent
sudo firewall-cmd --reload

任务 3:测试安装

验证您的安装。

$ mysqladmin -u root -p version
Enter password:
mysqladmin  Ver 9.1 Distrib 10.5.22-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version          10.5.22-MariaDB
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 29 min 18 sec

Threads: 1  Questions: 35  Slow queries: 0  Opens: 20  Open tables: 13  Queries per second avg: 0.019

version 会为您提供有关服务器的信息。

任务 4:创建新数据库和用户

创建新数据库。

MariaDB [(none)]> create database NEW_DATABASE_NAME;

创建新用户,并授予其对该数据库所有表的全部权限。

MariaDB [(none)]> grant all privileges on NEW_DATABASE_NAME.* TO 'NEW_USER_NAME'@'localhost' identified by 'PASSWORD';

如果要授予来自任何地方的访问权限,请将 localhost 替换为 %,或者如果可能,请将它替换为 IP 地址。

您可以限制授予的权限。有不同类型的权限可供用户使用。

  • **SELECT**:读取数据。
  • **USAGE**:连接到服务器的授权(在创建新用户时默认授予)。
  • **INSERT**:向表中添加新元组。
  • **UPDATE**:修改现有元组。
  • **DELETE**:删除元组。
  • **CREATE**:创建新表或数据库。
  • **DROP**:删除现有表或数据库。
  • **ALL PRIVILEGES**:所有权限。
  • **GRANT OPTION**:向其他用户授予或撤销权限。

不要忘记重新加载并应用新权限。

MariaDB [(none)]> flush privileges;

检查。

$ mariadb -u NEW_USER_NAME -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| NEW_DATABASE_NAME  |
| information_schema |
+--------------------+
2 rows in set (0.001 sec)

将示例数据添加到您的数据库中。

$ mariadb -u NEW_USER_NAME -p NEW_DATABASE_NAME
MariaDB [NEW_DATABASE_NAME]> CREATE TABLE users(
    id INT NOT NULL AUTO_INCREMENT,
    first_name VARCHAR(30) NOT NULL,
    last_name VARCHAR(30) NOT NULL,
    age INT DEFAULT NULL,
    PRIMARY KEY (id));
Query OK, 0 rows affected (0.017 sec)

MariaDB [NEW_DATABASE_NAME]> INSERT INTO users (first_name, last_name, age) VALUES ("Antoine", "Le Morvan", 44);
Query OK, 1 row affected (0.004 sec)

任务 5:创建远程用户

在本任务中,您将创建新用户、授予来自远程的访问权限,并测试使用该用户进行的连接。

MariaDB [(none)]> grant all privileges on NEW_DATABASE_NAME.* TO 'NEW_USER_NAME'@'%' identified by 'PASSWORD';
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.004 sec)

使用此用户和 -h 选项远程连接到您的服务器。

$ mariadb -h YOUR_SERVER_IP -u NEW_USER_NAME -p NEW_DATABASE_NAME
Enter password:
...

MariaDB [NEW_DATABASE_NAME]>

任务 6:执行升级

启用所需的模块。

$ sudo dnf module enable mariadb:10.11
[sudo] password for antoine:
Last metadata expiration check: 2:00:16 ago on Thu Jun 20 11:50:27 2024.
Dependencies resolved.
============================================================================================================================================= Package                          Architecture                    Version                             Repository                        Size
=============================================================================================================================================Enabling module streams:
 mariadb                                                          10.11

Transaction Summary
=============================================================================================================================================
Is this ok [y/N]: y
Complete!

升级软件包。

$ sudo dnf update mariadb
Last metadata expiration check: 2:00:28 ago on Thu Jun 20 11:50:27 2024.
Dependencies resolved.
============================================================================================================================================= Package                            Architecture        Version                                                 Repository              Size
=============================================================================================================================================
Upgrading:
 mariadb                            x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream              1.7 M
 mariadb-backup                     x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream              6.7 M
 mariadb-common                     x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream               28 k
 mariadb-errmsg                     x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream              254 k
 mariadb-gssapi-server              x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream               15 k
 mariadb-server                     x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream               10 M
 mariadb-server-utils               x86_64              3:10.11.6-1.module+el9.4.0+20012+a68bdff7               appstream              261 k

Transaction Summary
=============================================================================================================================================
Upgrade  7 Packages

Total download size: 19 M
Is this ok [y/N]: y
Downloading Packages:
(1/7): mariadb-gssapi-server-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                               99 kB/s |  15 kB     00:00
(2/7): mariadb-server-utils-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                               1.1 MB/s | 261 kB     00:00
(3/7): mariadb-errmsg-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                                     2.5 MB/s | 254 kB     00:00
(4/7): mariadb-common-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                                     797 kB/s |  28 kB     00:00
(5/7): mariadb-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                                            5.7 MB/s | 1.7 MB     00:00
(6/7): mariadb-server-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                                     9.5 MB/s |  10 MB     00:01
(7/7): mariadb-backup-10.11.6-1.module+el9.4.0+20012+a68bdff7.x86_64.rpm                                     7.7 MB/s | 6.7 MB     00:00
---------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                         13 MB/s |  19 MB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction

...

Complete!

现在需要升级您的数据库(检查 /var/log/messages,因为服务会报错)。

mariadb-check-upgrade[8832]: The datadir located at /var/lib/mysql needs to be upgraded using 'mariadb-upgrade' tool. This can be done using the following steps:
mariadb-check-upgrade[8832]:  1. Back-up your data before with 'mariadb-upgrade'
mariadb-check-upgrade[8832]:  2. Start the database daemon using 'systemctl start mariadb.service'
mariadb-check-upgrade[8832]:  3. Run 'mariadb-upgrade' with a database user that has sufficient privileges
mariadb-check-upgrade[8832]: Read more about 'mariadb-upgrade' usage at:
mariadb-check-upgrade[8832]: https://mariadb.com/kb/en/mysql_upgrade/

不要忘记执行 MariaDB 提供的升级脚本。

sudo mariadb-upgrade
Major version upgrade detected from 10.5.22-MariaDB to 10.11.6-MariaDB. Check required!
Phase 1/8: Checking and upgrading mysql database
Processing databases
mysql
mysql.column_stats                                 OK
mysql.columns_priv                                 OK
mysql.db                                           OK
...
Phase 2/8: Installing used storage engines... Skipped
Phase 3/8: Running 'mysql_fix_privilege_tables'
Phase 4/8: Fixing views
mysql.user                                         OK
...
Phase 5/8: Fixing table and database names
Phase 6/8: Checking and upgrading tables
Processing databases
NEW_DATABASE_NAME
information_schema
performance_schema
sys
sys.sys_config                                     OK
Phase 7/8: uninstalling plugins
Phase 8/8: Running 'FLUSH PRIVILEGES'
OK

任务 6:执行转储

mariadb-dump 命令可以执行数据库转储。

mariadb-dump -u root -p NEW_DATABASE_NAME > backup.sql

验证。

cat backup.sql
-- MariaDB dump 10.19  Distrib 10.11.6-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: NEW_DATABASE_NAME
-- ------------------------------------------------------
-- Server version       10.11.6-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
...

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(30) NOT NULL,
  `last_name` varchar(30) NOT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `users`
--

LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES
(1,'Antoine','Le Morvan',44);
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

...
-- Dump completed on 2024-06-20 14:32:41

检查您的知识

✔ 默认安装哪个数据库版本?

  • MySQL 5.5
  • MariaDB 10.5
  • MariaDB 11.11
  • Mysql 8

✔ 使用哪个命令来应用权限更改?

  • flush rights
  • flush privileges
  • mariadb reload
  • apply

结论

在本章中,您安装并保护了 MariaDB 数据库服务器,并创建了数据库和专用用户。

这些技能是管理数据库的先决条件。

在下一节中,您将了解如何安装 MySQL 数据库而不是 MariaDB 分支。

作者:Antoine Le Morvan

贡献者:Steven Spencer、Ganna Zhyrnova