跳到内容

MariaDB 数据库服务器

先决条件

  • Rocky Linux 服务器
  • 熟练使用命令行编辑器(本示例中使用 *vi*)
  • 对从命令行发出命令、查看日志和其他一般系统管理员职责有很高的舒适度
  • 了解 *mariadb-server* 数据库会有所帮助
  • 以 root 身份或使用 *sudo* 运行所有命令

介绍

*mariadb-server* 及其客户端 *mariadb* 是 *mysql-server* 和 *mysql* 的开源替代方案,它们共享命令结构。*mariadb-server* 在许多 Web 服务器上运行,因为流行的 Wordpress CMS 需要它。但是,这个数据库还有许多其他用途。

如果你想将它与其他工具一起使用来强化 Web 服务器,请参考 Apache 加固 Web 服务器指南

安装 mariadb-server

你需要安装 *mariadb-server*

dnf install mariadb-server

保护 mariadb-server

为了增强 *mariadb-server* 的安全性,你需要运行一个脚本,但在运行之前,你需要启用并启动 mariadb

systemctl enable --now mariadb

接下来,运行以下命令

mysql_secure_installation

提示

Rocky Linux 8.5 中默认启用的 mariadb-server 版本是 10.3.32。你可以通过启用模块来安装 10.5.13

dnf module enable mariadb:10.5

然后安装 mariadb。从 MariaDB 10.4.6 版本开始,可以使用 MariaDB 特定的命令来代替旧的 mysql 前缀命令。这些命令包括前面提到的 mysql_secure_installation,现在可以使用 MariaDB 版本 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, you will need the current
password for the root user.  If you have just installed MariaDB, and
you have not 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):

由于这是一个全新的安装,还没有设置 root 密码。只需在此处按 Enter 即可。

对话框的下一部分将继续

OK, successfully used password, moving on...

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

Set root password? [Y/n]

你绝对需要设置一个 root 密码。你需要确定它应该是什么,并在某个密码管理器中记录下来,以便在必要时可以调出它。首先按 Enter 接受默认的 "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",所以只需按 Enter 接受默认值即可。

对话框继续到允许 root 用户远程登录的部分

... 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]

root 应该只在机器上本地需要。所以也接受这个默认值,按 Enter

然后,对话框转到与 *mariadb-server* 自动安装的“test”数据库相关的部分

... 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]

同样,这里的答案是默认值,所以只需按 Enter 即可将其删除。

最后,对话框询问你是否要重新加载权限

- 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]

再次,按 Enter 执行此操作。如果一切顺利,你将收到以下消息

 ... 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 现在可以使用了。

Rocky 9.x 的变化

Rocky Linux 9.2 使用 mariadb-server-10.5.22 作为默认的 mariadb-server 版本。从 10.4.3 版本开始,服务器中自动启用了一个新插件,它更改了 mariadb-secure-installation 对话框。该插件是 unix-socket 身份验证。 这篇文章很好地解释了这个新功能。本质上,使用 unix-socket 身份验证使用已登录用户的凭据来访问数据库。这意味着,例如,如果 root 用户登录,然后使用 mysqladmin 创建或删除数据库(或任何其他功能),则不需要密码即可访问。mysql 也是如此。这也意味着没有密码可以被远程泄露。这取决于服务器上为所有数据库保护设置的用户安全性。

在为管理员用户设置密码后,mariadb-secure-installation 过程中的第二个对话框是

Switch to unix_socket authentication Y/n

这里的默认值是 "Y",但即使你回答 "n",在启用了插件的情况下,也不会要求用户输入密码,至少在命令行界面中不会。你可以指定密码或不输入密码,两者都可以工作

mysql

MariaDB [(none)]>
mysql -p
Enter password:

MariaDB [(none)]>

有关此功能的更多信息,请参考上面的链接。有一种方法可以关闭此插件并恢复到将密码作为必填字段,这也详细说明在该链接中。

结论

数据库服务器(例如 *mariadb-server*)可以用于许多用途。由于 Wordpress CMS 的普及,它经常出现在 Web 服务器上。但是,在你将数据库用于生产环境之前,最好加强其安全性。

作者: Steven Spencer

贡献者: Ezequiel Bruni, William Perron, Ganna Zhyrnova, Joseph Brinkman