Linux下忘記MySQL root密碼解決方案,基於Ubuntu 12.04 LTS。
忘了mysql密碼,從網上找到的解決方案記錄在這裡。
編輯mysql的設定檔/etc/mysql/my.cnf,在[mysqld]段下加入一行“skip-grant-tables”。
重啟mysql服務
www.bkjia.com @ubuntu:~$ sudo service mysql restart
mysql stop/waiting
mysql start/running, process 18669
用空密碼進入mysql管理命令列,切換到mysql庫。
www.bkjia.com @ubuntu:~$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> use mysql
Database changed
執行update user set password=PASSWORD("new_pass") where user='root'; 把密碼重設為new_pass。退出資料庫管理。
mysql> update user set password=PASSWORD("new_pass") where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 0
mysql>quit
回到vim /etc/mysql/my.cnf,把剛才加入的那一行“skip-grant-tables”注釋或刪除掉。
再次重啟mysql服務sudo service mysql restart,使用新的密碼登陸,修改成功。
www.bkjia.com @ubuntu:~$ mysql -uroot -pnew_pass
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>