2021-01-20 16:48

1. 免密码登陆

[wyhcli]➜  ~ vim /etc/my.cnf #配置文件目录
skip-grant-tables   #添加
[wyhcli]➜  ~ systemctl restart mysqld

2. 将旧密码置空

[wyhcli]➜  ~ mysql -uroot -p         
Enter password: #直接回车
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.22 MySQL Community Server - GPL

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> use mysql; # 选择数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string = '' where user = 'root';  #将密码置空
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> quit
Bye

3. 去除免密码登陆

  • 删除skip-grant-tables
  • 重启服务 service mysqld restart

4. 修改密码

[wyhcli]➜  ~ mysql -uroot -p    # 提示输入密码时直接敲回车,刚刚已经将密码置空了      
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22

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> alter user 'root'@'localhost' identified by '新密码(码形式过于简单则会报错)';
Query OK, 0 rows affected (0.01 sec)

mysql> 
```