MySQL是最流行的开源关系数据库管理系统。它速度快、易于使用、可扩展,是流行的LAMP
and LEMP
stacks.
本指南介绍了如何在 Ubuntu 20.04 上安装 MySQL 并确保其安全。
先决条件#
确保您以以下身份登录具有 sudo 权限的用户
.
在 Ubuntu 上安装 MySQL#
在撰写本文时,Ubuntu 存储库中可用的最新 MySQL 版本是MySQL版本8.0。要安装它,请运行以下命令:
sudo apt update
sudo apt install mysql-server
安装完成后,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 Tue 2020-04-28 20:59:52 UTC; 10min ago
Main PID: 8617 (mysqld)
Status: "Server is operational"
...
保护 MySQL#
MySQL 安装附带一个名为mysql_secure_installation
让您轻松提高数据库服务器的安全性。
不带参数调用脚本:
sudo mysql_secure_installation
系统会要求您配置验证密码插件用于测试MySQL用户密码的强度,提高安全性:
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
密码验证策略分为低、中、强三个级别。按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: 2
在下一个提示中,系统将要求您为 MySQL root 用户设置密码:
Please set the password for root here.
New password:
Re-enter new password:
如果您设置了验证密码插件,脚本将显示新密码的强度。类型y
确认密码:
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
接下来,系统会要求您删除匿名用户、限制 root 用户对本地计算机的访问、删除测试数据库并重新加载权限表。你应该回答y
对所有问题。
以 root 身份登录#
要从命令行与 MySQL 服务器交互,请使用 MySQL 客户端实用程序,该实用程序作为 MySQL 服务器包的依赖项安装。
在 MySQL 8.0 上,root 用户通过以下方式进行身份验证:auth_socket
默认插件。
The auth_socket
插件对从以下位置连接的用户进行身份验证localhost
通过Unix套接字文件。这意味着您无法通过提供密码来进行 root 身份验证。
要以 root 用户身份登录 MySQL 服务器,请键入:
sudo mysql
您将看到 MySQL shell,如下所示:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.19-0ubuntu5 (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
如果您想使用外部程序(例如 phpMyAdmin)以 root 身份登录 MySQL 服务器,您有两个选择。
第一个是将身份验证方法从auth_socket
to mysql_native_password
。您可以通过运行以下命令来做到这一点:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;
第二个推荐选项是创建一个新的专用管理用户,可以访问所有数据库:
GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';
结论#
我们已经向您展示了如何在 Ubuntu 20.04 上安装 MySQL。现在您的数据库服务器已启动并正在运行,您的下一步可能是学习如何管理MySQL用户帐户和数据库
.
如果您有任何问题或反馈,请随时发表评论。