如何在 Ubuntu Linux 中安装 MySQL

在本指南中,您将学习如何在 Ubuntu Linux 中安装 MySQL,以及如何设置 MySQL root 用户密码,最后如何访问 MySQL 服务器 root 用户。

下面给出的所有步骤都在新安装的 Ubuntu 22.04 LTS 服务器版本上进行了测试。

先决条件

在安装 MySQL 之前,请确保您的 Ubuntu 系统已使用最新软件包进行了更新:

$ sudo apt update
$ sudo apt upgrade

安装所有更新后,重新启动系统:

$ sudo reboot

1.在Ubuntu Linux中安装MySQL

MySQL 在 Ubuntu Linux 的默认存储库中可用。 要在 Ubuntu Linux 中安装 MySQL,请运行:

$ sudo apt install mysql-server

此命令安装所有必要的组件以在 Ubuntu 中设置 MySQL 数据库服务器。

要检查 MySQL 版本,请运行:

$ mysql --version

或者使用资本 V

$ mysql -V

样本输出:

mysql  Ver 8.0.29-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))

1.1。 启动、重启和停止 MySQL 服务

在 Ubuntu 中,一旦安装了 MySQL 服务器,就会自动启动 MySQL 服务。 要验证 MySQL 服务是否已启动,请运行:

$ sudo systemctl status mysql

样本输出:

* mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-09 07:47:00 UTC; 15min ago
    Process: 1217 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 1225 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 37964)
     Memory: 359.7M
        CPU: 1.939s
     CGroup: /system.slice/mysql.service
             `-1225 /usr/sbin/mysqld

May 09 07:47:00 ubuntu22ct systemd[1]: Starting MySQL Community Server...
May 09 07:47:00 ubuntu22ct systemd[1]: Started MySQL Community Server.
在 Ubuntu 22.04 Linux 中检查 MySQL 状态

如您所见,MySQL 服务已加载并运行! 如果由于某种原因没有启动,您可以使用命令启动 MySQL 服务:

$ sudo systemctl start mysql

要使 MySQL 在每次重新启动时自动启动,请运行:

$ sudo systemctl enable mysql

要重新启动 MySQL 服务,请运行:

$ sudo systemctl restart mysql

2.设置MySQL root用户密码

默认情况下,MySQL root 密码为空,不建议用于生产环境。 您必须设置一个强大且唯一的数据库管理用户,即 root 用户密码。

MySQL 附带一个名为 mysql_secure_installation 删除不安全的默认设置并保护数据库服务器。

mysql_secure_installation 脚本将执行以下操作:

  • 设置 VALIDATE PASSWORD COMPONENT,
  • 如果 VALIDATE PASSWORD COMPONENT 已启用,请设置您的首选 password validation 政策,
  • 删除匿名用户,
  • 不允许 root 用户远程登录,
  • 删除测试数据库并删除测试数据库的权限,
  • 最后重新加载权限表。

现在让我们运行这个脚本:

$ sudo mysql_secure_installation

在安装 MySQL 后第一次运行此命令时,您将使用空白密码连接到 MySQL。 然后会提示你是否要设置 VALIDATE PASSWORD PLUGIN.

VALIDATE PASSWORD PLUGIN 用于测试密码和提高安全性。 它检查密码强度并允许用户仅设置足够安全的密码。

设置验证密码组件是可选的。 禁用它是安全的. 但是,我建议您在创建用户角色时使用强且唯一的密码。 如果您不想启用 VALIDATE PASSWORD PLUGIN,只需按任意键即可跳过密码验证部分并继续其余步骤。

我想用这个 VALIDATE PASSWORD PLUGIN,所以我输入了 是的 是的:

 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: y

如果你选择 是的,您将提示选择密码策略级别。 共有三种密码验证策略可用。 他们是 低的, 中等的强的.

每个密码策略都包含一组密码规则,您应该在创建新数据库角色时使用这些规则。 为了 example 如果选择 MEDIUM,密码应至少包含 8 个字符,包括数字、小写和大写字母以及特殊字符。 选择任何适合您的密码策略。

我使用 LOW 级别的密码策略,所以我选择 0 (零):

 There are three levels of password validation policy:

 LOW    Length >= 8
 MEDIUM Length >= 8, numeric, mixed case, and special characters
 STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

接下来输入两次 MySQL root 用户密码。 输入密码后,将显示密码强度分数。 就我而言,我得到了 100。如果密码强度对您来说可以,请按 y 继续使用给定的密码。 否则,输入另一个强密码,然后按 Y 接着说。

 Please set the password for root here.

 New password: 

 Re-enter new password: 

 Estimated strength of the password: 100 
 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

请不要将此与您的系统的 root 帐户。 它是我们数据库服务器的管理帐户。

接下来,按 y 并按 ENTER 接受所有后续问题的默认值。 这将删除匿名用户,禁止远程 root 登录并删除测试数据库。

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 root 用户密码。

你忘记了你的 MySQL root 密码吗? 没问题! 您可以在 MySQL 中轻松重置 root 密码,如以下链接所述:

  • 如何在 Ubuntu Linux 上的 MySQL 8 中重置 Root 密码
  • 如何在 Linux 中重置 MySQL 根用户密码

3.登录MySQL

登录 MySQL 服务器 root 用户,只需运行:

$ sudo mysql

Enter 访问 MySQL 提示的用户密码:

[sudo] password for ostechnix: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 8
Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql> 
在 Ubuntu 22.04 Linux 中以 root 用户身份登录 MySQL

开始为您的项目创建用户角色和数据库。 我们将很快在单独的指南中介绍它。

要查看 help 部分,类型 help 或者 h 并按下 ENTER 钥匙:

mysql> help

要退出 MySQL 提示符并返回终端,请运行:

mysql> exit

4.更改MySQL root用户的认证方式

你有没有注意到我没有提供 MySQL root 用户的密码就登录了 MySQL 服务器。 我只是输入了我的系统用户帐户的密码,而不是 MySQL root 用户的密码。 这是因为 MySQL root 用户使用 auth_socket 在运行 MySQL 5.7 及更高版本的 Ubuntu 系统中使用 MySQL 服务器进行身份验证的插件。

auth_socket 插件对通过 Unix 套接字文件从本地主机连接到 MySQL 服务器的客户端进行身份验证。 所以你不能对 MySQL 服务器进行身份验证 root 用户通过提供密码。

如果要对 MySQL root 用户使用密码验证,则需要重新配置 MySQL 服务器以使用不同的验证方法,如下文所述。

? 更改 Ubuntu 中 MySQL 根用户的身份验证方法

结论

在本指南中,我们讨论了如何在 Ubuntu Linux 操作系统中安装 MySQL。 我们还学习了如何设置 MySQL root 用户密码以及如何访问或连接到 MySQL 服务器 root 用户。