本页内容

  • 准备工作
  • 开始之前
  • 在 Ubuntu 22.04 上安装 MySQL 8
  • 管理 MySQL 服务
  • 加固 MySQL 安装
  • 设置 MySQL root 密码
  • 在 MySQL 中创建数据库和用户
  • 卸载 MySQL 服务器
  • 总结

MySQL 是由 Oracle Cloud 提供支持的一款免费、开源的关系型数据库管理平台。它凭借久经验证的可靠性、快速的处理能力、易用性和灵活性而广受欢迎。它使用结构化查询语言(SQL)来添加、访问和管理数据库内容。MySQL 8.0 将其元数据存储在名为 InnoDB 的成熟事务型存储引擎中。它采用客户端/服务器架构,可安装在包括 Ubuntu、Windows、CentOS 和 Debian 在内的所有主流操作系统上。

本教程将向您展示如何在 Ubuntu 22.04 服务器上安装 MySQL 8。

准备工作

  • 一台运行 Ubuntu 22.04 的服务器。
  • 您的服务器上已设置 root 密码。

开始之前

首先,运行以下命令将所有系统软件包更新并升级到最新版本:

apt update -y
apt upgrade -y

所有软件包更新完成后,您可以继续进行下一步。

在 Ubuntu 22.04 上安装 MySQL 8

默认情况下,Ubuntu 默认软件源中已包含最新版本的 MySQL 服务器。您可以通过运行以下命令来安装它:

apt install mysql-server -y

MySQL 服务器安装完成后,您可以使用以下命令查看 MySQL 版本:

mysql --version

您应该会在以下输出中看到 MySQL 版本:

mysql  Ver 8.0.30-0ubuntu0.22.04.1 for Linux on x86_64 ((Ubuntu))

所有软件包更新完成后,您可以继续进行下一步。

管理 MySQL 服务

默认情况下,MySQL 服务由 systemd 管理。您可以使用 systemctl 命令轻松启动、停止并查看 MySQL 的状态。

要启动 MySQL 服务,请运行以下命令:

systemctl start mysql

要停止 MySQL 服务,请运行以下命令:

systemctl stop mysql

您可以使用以下命令查看 MySQL 服务的状态:

systemctl status mysql

您应该会看到类似以下的输出:

? mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-08-21 12:47:24 UTC; 28s ago
    Process: 26157 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
   Main PID: 26185 (mysqld)
     Status: "Server is operational"
      Tasks: 41 (limit: 2242)
     Memory: 359.8M
        CPU: 1.383s
     CGroup: /system.slice/mysql.service
             ??26185 /usr/sbin/mysqld

Aug 21 12:47:23 ubuntu2204 systemd[1]: Starting MySQL Community Server...
Aug 21 12:47:24 ubuntu2204 systemd[1]: Started MySQL Community Server.

默认情况下,MySQL 监听 3306 端口。您可以使用以下命令进行检查:

ss -antpl | grep -i mysql

您应该会在以下输出中看到 MySQL 的监听端口:

LISTEN 0      70         127.0.0.1:33060      0.0.0.0:*    users:(("mysqld",pid=26185,fd=21))                       
LISTEN 0      151        127.0.0.1:3306       0.0.0.0:*    users:(("mysqld",pid=26185,fd=23))                       

所有软件包更新完成后,您可以继续进行下一步。

加固 MySQL 安装

接下来,运行 mysql_secure_installation 脚本是一个很好的做法,它可以启用一些额外的安全功能,包括设置新的 MySQL root 密码、删除匿名用户以及禁止远程登录。

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

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

输入 Y 并按回车键以删除匿名用户。

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.

输入 Y 并按回车键以禁止 root 用户远程登录。

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

输入 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 命令行:

mysql

连接到 MySQL 命令行后,使用以下命令设置 MySQL 密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'securepassword';

接下来,运行以下命令以保存更改:

mysql> FLUSH PRIVILEGES;

接下来,使用以下命令退出 MySQL 命令行:

mysql> EXIT;

接下来,再次登录 MySQL 命令行以验证 root 密码:

mysql -u root -p

登录后,您将进入如下所示的 MySQL 命令行:

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30-0ubuntu0.22.04.1 (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> 

完成后,您可以继续进行下一步。

在 MySQL 中创建数据库和用户

让我们使用以下命令创建一个名为 db1 的数据库:

mysql> CREATE DATABASE db1;

您可以使用以下命令查看已创建的数据库:

mysql> SHOW databases;

您将看到类似以下的输出:

+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

要切换到 db1 数据库,请运行以下命令:

mysql> USE db1;

要创建一个名为 dbuser 的用户,请运行以下命令:

mysql> CREATE USER 'dbuser'@'%' IDENTIFIED BY 'password';

要为 dbuser 授予所有数据库的全部权限,请运行以下命令:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'dbuser'@'%' WITH GRANT OPTION;

要保存更改,请运行以下命令:

mysql> FLUSH PRIVILEGES;

您可以使用以下命令退出 MySQL 命令行:

mysql> EXIT;

完成后,您可以继续进行下一步。

卸载 MySQL 服务器

如果您想从服务器上移除 MySQL,请运行以下命令:

apt remove mysql-server --purge

接下来,使用以下命令删除所有不再需要的软件包:

apt autoremove

总结

在本文中,您学习了如何在 Ubuntu 22.04 上安装 MySQL 8,还学习了如何管理 MySQL 服务以及创建数据库和用户。现在,您可以在 MySQL 中开始创建新的数据库和数据库用户了。