本文最先发布在:https://www.itcoder.tech/posts/how-to-i ... ntu-20-04/
MySQL是最流行的开源关系数据库管理系统。它速度快,容易使用,容易扩展,并且流行的LAMP和LEMP的一部分。
这篇指南讲解了如何在 Ubuntu 20.04上安装和保护 MySQL。
一、前提条件
确保你以 sudo 用户身份登录
二、在 Ubuntu 上安装 MySQL
在写作这篇文章的时候,Ubuntu 源仓库中最新的 MySQL 版本号是 MySQL 8.0。想要安装它,运行下面的命令:
代码: 全选
sudo apt update
sudo apt install mysql-server
代码: 全选
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_secure_installation的脚本,它允许你很容易地提高数据库服务器的安全性。
不带参数运行这个脚本:
代码: 全选
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: 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
代码: 全选
Please set the password for root here.
New password:
Re-enter new password:
代码: 全选
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 用户身份登录
使用 MySQl 客户端工具在命令行和 MySQL 服务器交互,这个 MySQL 客户端已经作为 MySQL 服务器安装包的依赖软件包被安装了。
在MySQL 8.0上,root 用户默认通过auth_socket插件授权。
auth_socket插件通过 Unix socket 文件来验证所有连接到localhost的用户。这意味着你不能通过提供密码,验证为 root。
以 root 用户身份登录 MySQL服务器,输入;
代码: 全选
sudo mysql
代码: 全选
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>
第一个就是将验证方法从auth_socket修改成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 用户和数据库。