1.安装MySQL服务

sudo apt update
sudo apt install mysql-server

2.MySQL安装脚本

sudo mysql_secure_installation
-- 要记住你的密码

3.修改root用户密码

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;

4.修改MySQL密码强度

如果root用户密码修改失败,需要修改MySQL密码强度

SHOW VARIABLES LIKE 'validate_password%'; ---查看当前安全变量值

--修改密码策略
set global validate_password.policy=0;
set global validate_password.length=4;  -- 最低四位

重新改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';

5.如果忘记密码,修改密码

5.1 处理配置文件

# 修改配置文件 
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# 末尾添加 
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

# MySQL重启
system restart mysql 

5.2 将旧密码置空

执行mysql -u root -p登录数据库

use mysql;

update user set authentication_string = '' where user = 'root';

删掉步骤1的语句 skip-grant-tables

重启服务 systemctl restart mysql

5.3修改密码

mysql -uroot -p

use mysql;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';

失败还是需要修改密码强度