Debian 11安装MariaDB完整教程:2023年详细步骤指南
引言
MariaDB是一个开源的关系数据库管理系统,通常作为MySQL的替代方案,作为流行的LAMP(Linux,Apache,MySQL,PHP/Python/Perl)堆栈中的数据库部分。它旨在成为MySQL的直接替代品。
这个安装指南的简短版本包括以下三个步骤:
- Update your package index using apt
- Install the mariadb-server package using apt. The package also pulls in related tools to interact with MariaDB
- Run the included mysql_secure_installation security script to restrict access to the server
- sudo apt update
- sudo apt install mariadb-server
- sudo mysql_secure_installation
本教程将解释如何在Debian 11服务器上安装MariaDB,并验证其是否正常运行并具有安全的初始配置。
先决条件
- To follow this tutorial, you will need a server running Debian 11. This server should have a non-root administrative user and a firewall configured with UFW. Set this up by following our initial server setup guide for Debian 11.
步骤1——安装MariaDB
截至撰写本文时,Debian 11的默认软件源包括MariaDB版本10.5.15。它被Debian MySQL/MariaDB打包团队标记为默认的MySQL变体。
要安装它,请使用apt更新您服务器上的软件包索引。
- sudo apt update
然后安装这个软件包。
- sudo apt install mariadb-server
这些命令将安装MariaDB,但不会提示您设置密码或进行任何其他配置更改。由于默认配置会使您的MariaDB安装不安全,因此您将使用mariadb-server软件包提供的脚本来限制对服务器的访问并删除未使用的账户。
步骤二 – 配置MariaDB
对于新的MariaDB安装,接下来的步骤是运行包含的安全脚本。此脚本会修改一些较不安全的默认选项,如远程根用户登录和示例用户。
运行安全脚本。
- sudo mysql_secure_installation
这将带领你通过一系列提示,你可以对MariaDB安装的安全选项进行一些更改。第一个提示将要求你输入当前的数据库根密码。由于你尚未设置密码,请按ENTER表示“无”。
OutputNOTE: 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, you'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):
在你会被要求是否要切换到Unix套接字认证时,由于你已经拥有受保护的根账户,你可以跳过这一步。输入n然后按回车键。
Output. . .
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] n
下一个提示会问你是否想更改根密码。在Debian 11上,MariaDB的根账户与自动化系统维护密切相关,因此不应更改该账户的配置认证方式。
这样做可能会导致软件包更新删除对管理员账户的访问权限,从而破坏数据库系统。请输入字母 n,然后按回车键。
OutputChange the root password? [Y/n]
以后,如果套接字身份验证在您的使用情况下不合适,您将学习如何设置一个额外的管理员帐户以进行密码访问。
从那里开始,你可以按下Y然后按下回车键来接受所有后续问题的默认设置。这将删除一些匿名用户和测试数据库,禁用远程root登录,并加载这些新规则,以便MariaDB立即实施你所做的更改。
有了这些,您已经完成了MariaDB的初始安全配置。下一步是一个可选步骤,但如果您希望使用密码进行身份验证到MariaDB服务器,我们建议您遵循此步骤。
步骤3 — (可选) 创建一个使用密码验证的管理员用户
在运行MariaDB 10.5的Debian系统上,默认情况下,root MariaDB用户使用unix_socket插件进行身份验证,而不是使用密码。这在许多情况下可以提供更高的安全性和可用性,但当您需要允许外部程序(例如phpMyAdmin)拥有管理员权限时,可能会增加一些复杂性。
由于服务器使用root账户来执行诸如日志轮换、启动和停止服务器之类的任务,最好不要更改root账户的认证信息。虽然在/etc/mysql/debian.cnf配置文件中更改凭据在初始阶段可能有效,但是软件包更新有可能会覆盖这些更改。与其修改root账户,软件包维护者建议创建一个单独的管理账户,用于基于密码的访问。
为此,我们将创建一个名为“admin”的新账户,具有与root账户相同的权限,但配置为密码验证。从您的终端打开MariaDB提示符:
- sudo mariadb
随后创建一个具有root权限和基于密码的访问的新用户。请确保更改用户名和密码以匹配您的偏好。
- GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
刷新权限以确保它们在当前会话中得以保存和生效。
- FLUSH PRIVILEGES;
完成后,请退出MariaDB shell。
- exit
最后,让我们测试一下MariaDB的安装。
步骤四 — 测试MariaDB
当从默认的资源库安装了MariaDB后,它会自动开始运行。为了测试这一点,检查它的状态即可。
- sudo systemctl status mariadb
您将收到类似于下面的输出结果:
Output● mariadb.service - MariaDB 10.5.15 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-03-11 22:01:33 UTC; 14min ago
Docs: man:mariadbd(8)
https://mariadb.com/kb/en/library/systemd/
. . .
如果MariaDB没有运行,您可以使用命令sudo systemctl start mariadb来启动它。
为了进行额外的检查,您可以尝试使用mysqladmin工具连接到数据库。这是一个客户端,允许您运行管理命令。例如,以下命令用于使用Unix套接字连接到MariaDB作为root用户,并返回版本信息。
- sudo mysqladmin version
你将收到类似这样的输出。
Outputmysqladmin Ver 9.1 Distrib 10.5.15-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Server version 10.5.15-MariaDB-0+deb11u1
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /run/mysqld/mysqld.sock
Uptime: 4 min 20 sec
Threads: 1 Questions: 72 Slow queries: 0 Opens: 32 Open tables: 25 Queries per second avg: 0.276
结论
在本指南中,您安装了MariaDB关系数据库管理系统,并使用其默认安装的mysql_secure_installation脚本进行了安全设置。您还可以选择在测试MariaDB服务器功能之前创建一个使用密码身份验证的新的管理用户。
现在您已经拥有一个运行且安全的MariaDB服务器,以下是一些示例,显示您可以采取的下一步操作来与服务器一起工作:
- Learn how to import and export databases
- Practice running SQL queries
- Incorporate MariaDB into a larger application stack