如何在Debian 10 Linux上安装MySQL

时间:2020-03-05 15:30:21  来源:igfitidea点击:

默认的Debian存储库中不提供MySQL,这是世界上最受欢迎的开源关系数据库管理系统。
MariaDB是Debian 10中的默认数据库系统。

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

配置MySQL存储库

要将MySQL APT存储库添加到系统,请转到存储库下载页面,并使用以下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服务器和群集(当前选择:mysql-8.0)”,然后选择首选的MySQL版本。

我们将安装MySQL 8.0版。
通过按“ Tab”键选择“确定”,然后按“ Enter”键(如上图所示)。

如果不确定要选择哪个版本,请查阅要在服务器上部署的应用程序的文档。

安装MySQL

使用以下命令更新软件包列表,并通过运行以下命令安装MySQL服务器软件包:

sudo apt updatesudo apt install mysql-server

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

接下来,将向我们显示一条消息,通知我们有关新的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”。

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用户对本地计算机的访问并删除测试数据库。
我们应该对所有问题回答“是”(是)。

连接到MySQL服务器

要通过终端与MySQL进行交互,请使用安装为MySQL服务器软件包依赖项的“ mysql”客户端。

如果选择默认的身份验证方法以root用户身份登录到MySQL服务器,请执行以下操作:

sudo mysql

否则,如果我们选择传统身份验证方法登录,请输入:

mysql -u root -p

运行“ mysql_secure_installation”脚本时,系统将提示我们输入先前设置的根密码。
输入密码后,将显示MySQLshell,如下所示:

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