如何在 Ubuntu 24.04、22.04 或 20.04 上安装 MariaDB

MariaDB 是一款功能强大的开源关系数据库管理系统,是 MySQL 的热门替代品。它提供高性能、可扩展性和丰富的功能集,适用于从小型个人项目到大型企业环境的各种应用。MariaDB 提供长期支持 (LTS) 版本,可确保长期稳定性和可靠性,还提供短期版本,可更频繁地引入新功能和改进。

在 Ubuntu 24.04、22.04 或 20.04 上,您可以使用 mariadb.org 提供的官方 apt 存储库安装 MariaDB。此存储库包含 MariaDB 长期版本的最新版本,包括版本 10.5、10.6、10.11 和 11.4。如果您更喜欢使用短期版本,则适用相同的安装方法,允许您选择最适合您需求的版本。本指南将引导您完成使用官方 apt 存储库在 Ubuntu 系统上设置 MariaDB 的过程,确保您可以访问最新的功能和安全更新。

MariaDB 安装前步骤

在安装 MariaDB 之前更新 Ubuntu

在安装 MariaDB 之前,更新 Ubuntu 系统至关重要。此步骤可确保所有现有软件包都是最新的,从而最大限度地减少潜在冲突。

运行以下命令来更新软件包列表:

sudo apt update

更新列表后,继续升级任何过时的软件包。此步骤对于系统稳定性和安全性至关重要:

sudo apt upgrade

安装最初所需的软件包

MariaDB 安装需要特定的依赖项。这些是 MariaDB 正常运行所需的基本软件包。使用以下命令安装它们:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl -y

导入 MariaDB APT 存储库

下一步涉及导入包含您要安装的 MariaDB 软件包的 MariaDB APT 存储库。有不同的分支可用:长期支持 (LTS),由于其稳定性而推荐用于生产环境;短期版本,适合定期更新系统(例如 WordPress 等内容管理系统)的用户。

导入 GPG 密钥

首先,导入 GPG 密钥来验证 MariaDB 包的真实性:

curl -fsSL http://mirror.mariadb.org/PublicKey_v2 | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mariadb.gpg > /dev/null

选择 MariaDB 版本存储库

根据您的需求选择合适的 MariaDB 版本:

选项 1:MariaDB 10.5 (LTS)

终止日期:2025 年 6 月 24 日。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.5/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
选项 2:MariaDB 10.6 (LTS)

终止日期:2026 年 7 月。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.6/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
选项 3:MariaDB 10.11 (LTS)

终止日期:2028 年 2 月。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.11/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
选项 4:MariaDB 11.4(长期版本)

终止日期:2030 年 2 月。

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.4/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list

导入 MariaDB 存储库后刷新 APT 包索引

选择并导入所需的存储库版本后,刷新 APT 存储库以识别新源:

sudo apt update

此步骤确保系统识别新添加的 MariaDB 存储库并已准备好安装。

完成 MariaDB 安装

安装 MariaDB 服务器和客户端

要在系统上设置 MariaDB,您必须安装服务器和客户端软件包。此安装集成了管理数据的数据库服务器和提供与服务器交互接口的客户端。

执行以下命令来安装这些包:

sudo apt install mariadb-server mariadb-client -y

验证MariaDB安装

安装后,必须确认 MariaDB 已正确安装。使用以下命令检查已安装的 MariaDB 版本和构建:

mariadb --version

如果安装成功,您将看到类似如下的输出:

mariadb  Ver x.xx Distrib xx.xx.x-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

启用 MariaDB 服务

检查MariaDB服务状态

安装后,验证 MariaDB 服务的状态。此命令检查 MariaDB 是否处于活动状态并正在运行:

systemctl status mariadb

启动 MariaDB 服务

虽然 MariaDB 通常会在安装后自动激活,但您可以根据需要手动启动它:

sudo systemctl start mariadb

停止MariaDB服务

要停止 MariaDB 服务,请使用以下命令:

sudo systemctl stop mariadb

在系统启动时启用 MariaDB

要使 MariaDB 在系统启动时自动启动:

sudo systemctl enable mariadb

在系统启动时禁用 MariaDB

要防止 MariaDB 自动启动:

sudo systemctl disable mariadb

重新启动MariaDB服务

配置更改后通常需要重新启动服务。使用此命令重新启动 MariaDB:

sudo systemctl restart mariadb

运行 MariaDB 安全脚本

运行 MariaDB 安全脚本的重要性

设置 MariaDB 时,必须加强其默认配置,这些配置通常被认为是不安全的。此步骤对于保护数据库免受未经授权的访问和漏洞至关重要。运行 mysql_secure_installation 脚本是 MariaDB 安装过程中广泛认可的行业惯例。它可以增强 MariaDB 安装的安全性并降低与潜在恶意攻击相关的风险。

执行安全脚本

要启动此安全增强过程,请执行 mysql_secure_installation 脚本。此脚本执行几个与安全相关的操作,确保您的 MariaDB 安装拥有更安全的环境:

sudo mysql_secure_installation

配置安全设置

运行脚本后,您将按照以下几个步骤来提高 MariaDB 安装的安全性:

  • 设置根密码:系统将提示您为 MariaDB 的根用户创建一个强密码。此步骤至关重要,因为它可以防止未经授权的数据库访问。
  • 不允许远程根访问:脚本将询问您是否要禁用远程根登录,建议仅限制本地用户的访问。
  • 删除匿名用户帐户:这些帐户无需密码即可访问,存在安全风险。删除它们是保护数据库安全的重要步骤。
  • 删除测试数据库:MariaDB 有一个任何人都可以访问的默认测试数据库。删除此数据库可减少未经授权用户的潜在入口点。

遵循这些步骤可以显著改善 Ubuntu 上 MariaDB 安装的安全状况。

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 <---- Type Y then press the ENTER KEY.
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 <---- Type Y then press the ENTER KEY.
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 <---- Type Y then press the ENTER KEY.
 ... 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 <---- Type Y then press the ENTER KEY.
 ... 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 <---- Type Y then press the ENTER KEY.
 - 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 <---- Type Y then press the ENTER KEY.
 ... 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

运行 MariaDB 数据库表升级工具

如果您已将 MariaDB 升级到较新的版本(例如,从 10.5 升级到 10.10),则确保数据库表与新版本兼容至关重要。“mariadb-upgrade”工具就是为此目的而设计的。它会检查并更新表以符合升级版本的要求。

要启动此过程或检查数据库表的当前状态,请运行:

sudo mariadb-upgrade

即使您认为升级已成功,也建议运行此命令。如果已执行此命令,该工具将确认这一点。否则,它将提供有关所检查表和任何必要更新的详细报告。

删除 MariaDB

要从系统中完全删除 MariaDB,请使用以下命令:

sudo apt autoremove mariadb-server mariadb-client --purge -y

请注意,使用“–purge”标志不仅会删除 MariaDB,还会删除所有相关数据库数据。如果您打算保留数据,请省略“–purge”标志。

删除 MariaDB 后,还必须删除存储库和 GPG 密钥,以防止 MariaDB 存储库将来进行任何更新。可以使用以下命令完成此操作:

sudo rm /etc/apt/sources.list.d/mariadb.list /usr/share/keyrings/mariadb.gpg

结论

通过官方 apt 存储库在 Ubuntu 上安装 MariaDB,您可以确保您的数据库系统是最新版本,无论您选择长期版本还是短期版本。此方法提供了灵活性和安全性,允许您根据特定要求定制数据库环境。来自 mariadb.org 存储库的定期更新将确保您的安装安全且优化,为您的数据驱动应用程序提供可靠的基础。

有用的链接

以下是一些与使用 MariaDB 相关的有价值的链接:

  • MariaDB官方网站:访问 MariaDB 官方网站,了解有关数据库、功能和下载选项的信息。
  • MariaDB 知识库:访问 MariaDB 知识库,获取全面的文档、教程和用户指南。
  • MariaDB基金会:浏览 MariaDB 基金会网站,了解有关 MariaDB 社区、开发和最新消息的信息。
  • MariaDB 基础文档:查找 MariaDB 基金会提供的有关使用和配置 MariaDB 的详细文档。
  • MariaDB GitHub 存储库:您可以访问 MariaDB GitHub 存储库来查看源代码、报告问题并为开发做出贡献。
Joshua James
跟我来
Joshua James 的最新帖子 (查看全部)

发表评论