不少老铁,在开始时设置了 MySQL 的密码,后来一段时间没有用 MySQL以后,密码忘了~ QAQ,请别急,如今有如下方法解决密码忘了的状况。数据库
1.首先咱们须要跳过 MySQL 的密码认证:vim
(1)进入 /etc/my.cnf 文件windows
[root@localhost ~]# vim /etc/my.cnf
(2)在 [mysqld] 的下面添加 " skip-grant-tables " 用来跳过 MySQL 登陆时候的密码验证:安全
[mysqld]
skip-grant-tables
2.重启下 MySQL :post
[root@localhost lib]# systemctl restart mysqld.service
3.进入 MySQL 中使用命令对密码进行修改:ui
//使用名字叫 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.验证是否对密码从新设置成功:this
[root@localhost /]# mysql -uroot -proot //注意:这里通常直接输入密码登陆,可能会不安全 mysql: [Warning] Using a password on the command line interface can be insecure. ......