如何在 Debian 11 上安装 MySQL

在本教程中,我们将向您展示如何在 Debian 11 上安装 MySQL。对于那些不知道的人,MySQL 是一个基于 SQL(结构化查询语言)的关系数据库管理系统。 它是使用它的几个众所周知的应用程序中使用最广泛的数据库软件之一。 MariaDB 可用作 Debian 11 中的默认数据库。因此,您需要在 Debian 11 系统上安装 MySQL 服务器及其所有依赖项。

本文假设您至少具有 Linux 的基本知识,知道如何使用 shell,并且最重要的是,您在自己的 VPS 上托管您的站点。 安装非常简单,假设您在 root 帐户中运行,否则您可能需要添加 ‘sudo‘ 获取 root 权限的命令。 我将向您展示如何在 Debian 11 (Bullseye) 上逐步安装 MySQL 8 数据库。

在 Debian 11 Bullseye 上安装 MySQL

步骤 1. 在我们安装任何软件之前,请务必通过运行以下命令来确保您的系统是最新的 apt 终端中的命令:

sudo apt update sudo apt upgrade

步骤 2. 在 Debian 11 上安装 MySQL。

MySQL 在默认的 Debian 存储库中不可用。 您需要在 Debian 11 上安装 MySQL APT 存储库:

wget https://dev.mysql.com/get/mysql-apt-config_0.8.18-1_all.deb sudo dpkg -i mysql-apt-config_0.8.18-1_all.deb

在 MySQL 存储库安装过程中,如果出现提示,请选择 Debian buster 的存储库,然后按 TAB 键选择 Ok。 按 ENTER 继续。

接下来,通过运行以下命令更新包列表并安装 MySQL 服务器包:

sudo apt updatesudo apt install mysql-server

在安装过程中,会出现一个新的弹出窗口,提示您输入数据库 root 密码。

在 Debian 11 Bullseye 上安装 MySQL

安装完成后,MySQL服务将自动启动,您可以通过键入以下内容进行验证:

sudo systemctl status mysql

步骤 3. 保护 MySQL。

MySQL 安装完成后,您可能需要保护新 MySQL。 默认情况下,MySQL 未加固。 您可以使用 mysql_secure_installation 脚本。 您应该仔细阅读以下每个步骤,这些步骤将设置 root 密码、删除匿名用户、禁止远程 root 登录以及删除测试数据库和访问安全 MySQL:

mysql_secure_installation

像这样配置它:

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success.  By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y Success.  All done!

要登录 MySQL,请使用以下命令(请注意,它与登录 MySQL 数据库所用的命令相同):

mysql -u root -p

恭喜! 您已成功安装 MySQL。 感谢您使用本教程在 Debian 11 Bullseye 上安装最新版本的 MySQL 8。 如需更多帮助或有用信息,我们建议您查看 MySQL 官方网站.