在 Debian 12、11 或 10 上安装 MariaDB 的两种方法

在 Debian 上安装 MariaDB 非常简单,它为您的环境带来了强大、灵活且高效的数据库管理系统。本指南将引导您完成安装过程,确保您可以访问 MariaDB 的最新稳定版本。

以下是使用 MariaDB 的功能和优势的快速概述:

  • 高性能: MariaDB 为数据库操作提供了卓越的速度和效率。
  • 可扩展性: 轻松处理大型数据集和高流量负载。
  • 安全: 高级安全功能,包括加密和访问控制。
  • 兼容性: 与 MySQL 的无缝兼容使得切换或集成变得简单。
  • 灵活性: 支持多种存储引擎和插件,适用于多种应用程序。
  • 社区支持: 丰富的文档和强大的社区支持。

MariaDB 的跨平台功能可确保流畅的体验,让您可以在任何系统上利用其强大的功能。让我们深入了解在 Debian 系统上安装 MariaDB 的技术步骤。

安装 MariaDB 的先决条件

要在 Debian 上成功安装 MariaDB,请确保满足以下要求:

系统要求和支持的 Debian 版本

成分最低要求
处理器2 GHz 或更快的双核处理器
内存4 GB 或更多
磁盘空间25 GB 可用空间
网络高速网络连接
支持的版本Debian 12(书虫), Debian 11(靶心), Debian 10(Buster)

其他要求

要求描述
网络连接下载 MariaDB 和应用更新所必需。
终端约定所有终端命令都应以普通用户身份执行 sudo 特权。
CLI 命令利用命令行界面 (CLI) 进行安装和设置。
数据库管理工具建议使用 phpMyAdmin 之类的工具来更好地管理数据库。

方法 1:通过默认存储库安装 MariaDB

更新软件包索引

首先,在您的终端中,更新软件包索引以确保您拥有可用软件包的最新信息:

sudo apt update

安装 MariaDB 服务器

从默认存储库安装 MariaDB 服务器包:

sudo apt install mariadb-server

此命令会在您的 Debian Linux 发行版上安装默认的 MariaDB 版本。要升级到符合您要求的最新 MariaDB,请查看下面的方法 2。

方法 2:通过 PPA 安装 MariaDB

导入 MariaDB.org APT 存储库

安装MariaDB安装所需的初始软件包

第一步是安装必要的依赖项。在终端中执行以下命令。

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

继续添加 MariaDB APT 存储库

本节介绍如何导入建议用于生产的长期支持 (LTS) 分支。对于那些喜欢在 WordPress 等内容管理系统 (CMS) 上使用最新版本的 MariaDB 并且不介意每 6-12 个月升级一次版本的人来说,短期发布是一个不错的选择。

安装之前,使用以下命令导入 GPG 密钥来验证 MariaDB 包。

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

导入 GPG 密钥后,下一步是导入存储库。这些命令适用于 Debian 11 和 Debian 10 发行版。

10.5(长期发布)EOL

终止日期:2025 年 6 月 24 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.5/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
10.6(长期发布)

终止日期:2026 年 7 月 6 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.6/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
10.11(长期发布)

终止日期:2028 年 2 月 16 日

echo "deb [signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.11/debian/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
11.1(短期发布)

终止日期:2024 年 8 月 21 日

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

终止日期:2024 年 11 月 21 日

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

终止日期:2025 年 2 月。

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

添加了版本支持终止日期。除非您准备好处理更改为较新版本的更新,否则请始终使用 LTS 版本进行生产。

刷新 APT 指数

导入存储库后,使用提供的命令更新您的 APT 存储库。

sudo apt update

完成MariaDB安装

在 MariaDB.org 设置完成后继续在 Debian 上安装 MariaDB

使用配置良好的存储库和 GPG 密钥安装 MariaDB 客户端和服务器包。使用以下命令安装 MariaDB:

sudo apt install mariadb-server mariadb-client

确认 MariaDB 安装

通过检查版本和构建来确认 MariaDB 的安装。

mariadb --version

安装 MariaDB 10.10 的示例输出:

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

启用 MariaDB 服务和状态检查

验证 MariaDB 服务状态

安装 MariaDB 后,您可以使用以下 systemctl 命令检查数据库软件的状态:

systemctl status mariadb

如果您尚未启动该服务,请使用以下命令立即启用它并在系统重启时启用它:

sudo systemctl enable mariadb --now

MariaDB Systemd 服务命令

以下是在 Debian 系统上使用 MariaDB 时需要注意的最常见的 systemd 服务命令。

启动MariaDB:

sudo systemctl start mariadb

停止MariaDB:

sudo systemctl stop mariadb

在系统启动时启用 MariaDB:

sudo systemctl enable mariadb

在系统启动时禁用 MariaDB:

sudo systemctl disable mariadb

重新启动 MariaDB 服务:

sudo systemctl restart mariadb

安装后:保护 MariaDB

安装新版本的 MariaDB 后,务必了解现成的设置可能无法提供强大的安全性。因此,经验丰富的数据库管理员通常建议收紧这些设置以防止漏洞。

运行安全脚本

MariaDB 提供了一个方便的脚本,名为 mysql_secure_installation 帮助管理员保护其安装。执行此脚本不仅仅是最佳实践;它还是加强数据库抵御潜在威胁的主动步骤。

执行以下命令来运行脚本:

sudo mysql_secure_installation

或者

sudo mariadb-secure-installation

配置 MariaDB 以增强安全性

在启动 mysql_secure_installation 脚本,您将被引导完成几个步骤来增强数据库的安全性:

  • 设置 Root 密码:为根用户选择一个强大而独特的密码至关重要,以确保只有授权人员才能进行关键更改。
  • 限制远程访问:拒绝来自外部来源的直接 root 登录有利于增强安全性。这可最大限度地降低来自远程位置的未经授权的访问尝试的风险。
  • 删除匿名用户:MariaDB 默认具有匿名用户帐户,任何人都可以使用这些帐户登录。消除这些帐户可以增加额外的安全层,确保只有已知和指定的用户才能访问系统。
  • 删除测试数据库: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 提供了专门的工具, mariadb-upgrade,专门用于审查和纠正由于版本转换可能出现的任何差异。

通过输入以下内容来启动该过程或检查数据库表的当前状态:

sudo mariadb-upgrade

例如,如果您遇到如下输出:

This installation of MariaDB is already upgraded to 10.10.2-MariaDB.
There is no need to run mysql_upgrade again for 10.10.2-MariaDB.
You can use --force if you still want to run mysql_upgrade

这表明升级工具已经为所述版本完成了工作。但即使您最近没有执行升级,运行此工具仍然是明智的。它会一丝不苟地列出和更新表,以确保它们与当前的 MariaDB 安装保持兼容。

卸载 MariaDB

在某些情况下,您可能希望从 Debian 系统中卸载 MariaDB。如果这是您的选择,则必须彻底卸载以防止遗留文件或配置。

首先删除核心 MariaDB 组件:

sudo apt auto mariadb-server mariadb-client

然后,要清除对 MariaDB 的所有引用,请删除关联的存储库和 GPG 密钥。

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

结论

总而言之,本文提供了详细的分步指南,帮助您使用最新的 MariaDB 稳定版本增强您的 Debian 系统。我们探索了两种方法:使用 Debian 默认存储库或官方 mariadb.org 存储库获取最新支持的版本。通过导入 mariadb.org 存储库,您可以在 10.x 或 11.x 版本系列中安装自定义的 MariaDB 版本。

此外,我们还介绍了帮助您有效管理 MariaDB 服务的基本服务命令。本指南还提供了安装新版本 MariaDB 后顺利升级数据库的必要步骤。对于需要恢复或进行更改的用户,我们讨论了删除 MariaDB 及其导入的 apt 存储库。有了这些知识,您就可以自信地管理、升级和优化 Debian 上的 MariaDB 安装。

有用的链接

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

  • MariaDB GitHub 存储库:访问 MariaDB GitHub 存储库以查看源代码、报告问题并为开发做出贡献。
  • MariaDB 知识库:探索知识库,获取有关 MariaDB 的详细文章、教程和用户指南。
  • MariaDB 文档:访问综合文档,获取有关安装、配置和使用 MariaDB 的详细指南。
  • MariaDB官方网站:访问 MariaDB 官方网站,了解有关数据库系统、其功能和下载选项的信息。
  • MariaDB 产品页面:了解 MariaDB 提供的各种产品和服务,包括社区服务器和企业解决方案。
Joshua James
跟我来
Joshua James 的最新帖子 (查看全部)

发表评论