忘記MySQL的root密碼的解決方案

來源:互聯網
上載者:User

標籤:免密碼   sql   roo   options   com   use   word   添加   update   

經常會有朋友或者同事問起,MySQL 的 root 密碼忘了,不知道改怎麼辦。

其實解決方案很簡單,下面是詳細的操作步驟。

 

(1)修改設定檔my.cnf,在設定檔[mysqld]下添加 skip-grant-tables,重啟MySQL服務即可免密碼登入  其中--skip-grant-tables 選項前面曾經介紹過,意思是啟動 MySQL 服務的時候跳過許可權表認證。 啟動後,串連到 MySQL 的 root 將不需要口令。
# SERVER SECTION# ----------------------------------------------------------------------## The following options will be read by the MySQL Server. Make sure that# you have installed the server correctly (see above) so it reads this # file.#[mysqld]skip-grant-tables
(2)用空密碼的 root 使用者串連到 MySQL,並且更改 root 口令:
[[email protected] mysql]# mysql -urootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 53Server version: 5.0.41-community-log MySQL Community Edition (GPL)Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.mysql>mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘123‘;  
 ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql> update user set authentication_string = NULL where user = ‘root‘;
Query OK, 1 row affected (0.00 sec)

上面是先用grant的方式修改root密碼,但是由於使用了配置了skip-grant-tables 選項,使用“alter user”命令更改密碼失敗,直

接更新 user 表的 authentication_string欄位後更改密碼成功。

(3)到my.cnf 中刪除skip-grant-tables選項,然後直接用mysql -uroot免密碼登入後,在執行如下語句重設密碼
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘123‘;  
(4)重新用 root 登入時,必須輸入新口令。   MySQL 中,密碼丟失後無法找回,只能通過上述方式修改密碼。

忘記MySQL的root密碼的解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.