mysql8忘記密碼怎麼辦

技術菜呀蔡發表於2020-11-22

最近在mac本上裝了mysql也沒怎麼用,今天打算來測試一個專案需要匯入一些資料,絞盡腦汁也想不出來密碼了。只好來破解密碼了。

其實重點就這一條語句我也不知道怎麼能扯這麼多內容

以安全模式登入後執行如下語句即可,然後重新啟動mysql服務 登入即可無需密碼。

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

如果您的問題已經解決了下面的內退就是廢話,就不用往下看了,時間寶貴。

處理說明

先說說處理方法,mysql8版本的密碼重置和之前的版本有所不同。
處理方法為如下步驟

  • 1、關閉mysql服務

  • 2、以安全模式啟動mysql服務 加上引數–skip-grant-tables --user=mysql

  • 3、完成第二步驟後即可不用密碼登入,登入後先將使用者的authentication_string 欄位置空。

  • 4、然後再以正常模式啟動mysql服務

  • 5、讓後以沒有密碼的方式登入,然後再按照修改密碼的方式改成你想要修改的密碼。。

處理過程

####  0、以下為密碼錯誤提示
mac@MacdeMacBook-Pro ~ % mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mac@MacdeMacBook-Pro ~ % mysql -uroot   
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#### 1、關閉mysql服務
mac@MacdeMacBook-Pro bin % killall mysqld
#### 2、以安全模式啟動mysql服務
mac@MacdeMacBook-Pro bin % mysqld_safe --skip-grant-tables --user=mysql &
[1] 1870
mac@MacdeMacBook-Pro bin % 2020-11-21T14:25:00.6NZ mysqld_safe Logging to '/usr/local/var/mysql/MacdeMBP.err'.
2020-11-21T14:25:00.6NZ mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
#### 3、登入mysql
mac@MacdeMacBook-Pro bin % mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.21 Homebrew

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.
#### 以下這些命令在mysql8之前都可以使用的目前也不行了
mysql> set password=password("111111");
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'password("111111")' at line 1
mysql> update mysql.user set authentication_string=password("111111") where user="root";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '("111111") where user="root"' at line 1

#### 4、重點在此直接將密碼置空即可。。。
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> exit
Bye
mac@MacdeMacBook-Pro bin % killall mysqld
mac@MacdeMacBook-Pro bin % 2020-11-21T14:47:27.6NZ mysqld_safe mysqld from pid file /usr/local/var/mysql/MacdeMBP.pid ended

[1]  + done       mysqld_safe --skip-grant-tables --user=mysql
mac@MacdeMacBook-Pro bin % mysqld start

mac@MacdeMacBook-Pro bin % mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Homebrew

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> alter user 'root'@'localhost' identified by 'Cmm111111%%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)




相關文章