MySQL全球最流行的开源关系型数据库管理系统,它在默认的Debian存储库中不可用。 MariaDB是Debian 10中的默认数据库系统。

本教程说明了如何在Debian 10上启用MySQL存储库并使用apt安装和配置MySQL。并使用mysql初始化脚mysql_secure_installation配置root的密码。也介绍一些mysql_secure_installation脚本的问题。

配置MySQL存储库

要将MySQL存储库添加到您的系统,请访问存储库下载页面并使用以下wget命令下载最新的软件包:

wget http://repo.mysql.com/mysql-apt-config_0.8.13-1_all.deb

下载完成后,以具有sudo权限的用户身份安装软件包:

sudo apt install ./mysql-apt-config_0.8.13-1_all.deb

系统将显示配置菜单,从中可以选择要安装的MySQL版本。默认选中是MySQL 8.0,如果要安装MySQL 5.7,请选择MySQL Server & Cluster (Currently selected: mysql-8.0),然后选择首选的MySQL版本

我们将安装MySQL 8.0版。按Tab选择OK,然后按Enter。如果不确定要选择哪个版本。

安装MySQL

使用以下命令更新软件包列表,然后安装MySQL server软件包:

sudo apt update
sudo apt install mysql-server

安装程序将要求您设置MySQL root密码。现在不要设置密码(将其保留为空白),我们将在下一部分中进行设置。

接下来,您将看到一条消息,通知设置MySQL 8身份验证的信息。在选择默认的MySQL 8身份验证插件之前,请确保您的应用程序支持它。

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

sudo systemctl status mysql
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Fri 2019-07-26 13:23:25 PDT; 37s ago
   ...

配置MySQL

运行mysql_secure_installation命令来设置root密码并提高MySQL安装的安全性:

sudo mysql_secure_installation
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No:

将要求您配置VALIDATE PASSWORD PLUGIN,该工具用于测试MySQL用户密码的强度。密码验证策略分为三个级别:低,中和强。如果您不想设置验证密码插件,请按ENTER。在下一个提示符下,将要求您为MySQL根用户设置密码。

Please set the password for root here.

New password:

Re-enter new password:

一旦您设置了root密码,脚本还将要求您删除匿名用户,限制root用户对本地计算机的访问并删除测试数据库,具体回答取决于你的选择。但是最后一项Reload privilege tables now?重新加载权限表,请回答y。

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?禁用root用户远程登录 (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客户端,该客户端作为MySQL服务器软件包的依赖项安装。如果您没有输入密码,作为root用户登录到MySQL服务器,请输入下面的命令:

sudo mysql

如果您选择传统身份验证方法登录,请输入:

mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.17 MySQL Community Server - GPL
...

结论

在本教程中,您学习了如何在Debian 10服务器上安装和配置MySQL服务器。我们还向您展示了如何连接到MySQL 服务器。如果您的应用程序没有任何特定要求,也可以使用MariaDB ,这是Debian 10中的默认数据库系统。