#vim /etc/my.cnf(注:windows下修改的是my.ini)
很多老铁,在开始时设置了 MySQL 的密码,后来一段时间没有用 MySQL之后,密码忘了~ QAQ,请别急,现在有以下方法解决密码忘了的情况。
1.首先我们需要跳过 MySQL 的密码认证:
(1)进入 /etc/my.cnf 文件
[root@localhost ~]# vim /etc/my.cnf
(2)在 [mysqld] 的下面添加 " skip-grant-tables " 用来跳过 MySQL 登录时候的密码验证:
[mysqld] skip-grant-tables
2.重启下 MySQL :
[root@localhost lib]# systemctl restart mysqld.service
3.进入 MySQL 中使用命令对密码进行修改:
//使用名字叫 mysql(或者自己创建过) 数据库
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
//这里我把 账号 root 密码设置为 root mysql> update mysql.user set authentication_string=password(‘root‘) where user=‘root‘ ; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1
// 刷新 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye
4.验证是否对密码重新设置成功:
[root@localhost /]# mysql -uroot -proot
//注意:这里一般直接输入密码登录,可能会不安全
mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.30 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>