默认的Debian存储库中没有提供MySQL(世界上最流行的开源关系数据库管理系统),而MariaDB是Debian 10的默认数据库系统。

本教程说明了如何在Debian 10上从MySQL Apt存储库安装和保护MySQL。

配置MySQL存储库

要将MySQL APT存储库添加到系统中,请转到存储库下载页面,然后使用以下命令下载最新的发行版包: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版本8 . 0 .按OK键并点击Tab键Enter ,

安装MySQL

通过运行以下命令更新包列表并安装MySQL服务器包:sudo apt updatesudo 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_installationSecuring 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:

你被要求配置用于测试MySQL用户密码强度,有三个级别的密码验证策略,低,中和强,如果你不想设置验证密码插件,请按ENTER。Please set the password for root here.

New password:

Re-enter new password:

在下一个提示中,你被要求为MySQL root用户设置密码。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!

设置root密码后,脚本还会要求你删除匿名用户,限制root用户访问本地计算机并删除测试数据库。对于所有问题,你都应该回答"Y"。

连接到MySQL服务器

要通过终端与MySQL交互,请使用mysql客户端,它作为MySQL服务器包的一个依赖项安装,

如果你选择默认认证方法以root用户类型登录到MySQL服务器:sudo mysql

否则,如果选择旧版身份验证方法以登录类型:mysql -u root -p

系统将提示你输入以前在运行root脚本时设置的mysql_secure_installation密码,输入密码后,显示MySQL shell,如下所示: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 shell。

如果你的应用程序没有特定的要求,你应该坚持使用MariaDB,它是Debian 10默认的数据库系统。