如何在Ubuntu 20.04上安装MySQL 8.0
先决条件
确保您以具有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
系统将要求您配置VALIDATE PASSWORD PLUGIN
用来测试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上,auth_socket
默认情况下,root用户通过插件进行身份验证。
该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
为mysql_native_password
。您可以通过运行以下命令来做到这一点:
mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
mysql > FLUSH PRIVILEGES;
推荐的第二个选项是创建一个新的专用管理用户,该用户可以访问所有数据库:
GRANT ALL PRIVILEGES ON *.* TO 'administrator'@'localhost' IDENTIFIED BY 'very_strong_password';
结论
我们已经向您展示了如何在Ubuntu 20.04上安装MySQL。现在您的数据库服务器已启动并正在运行,下一步是学习如何管理MySQL用户帐户和数据库。
卸载
sudo apt-get autoremove --purge mysql-server sudo apt-get remove mysql-common
sudo rm -rf /etc/mysql/ /var/lib/mysql
清理残留数据
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
sudo apt autoremove
sudo apt autoclean