如何在Ubuntu上重置MySQL Root密碼
最後更新 2019年2月13日
在本文中,我們將通過使用該--skip-grant-tables
選項啓動MySQL來重置Ubuntu中的MySQL root密碼。
1.確認MySQL版本(http://www.1nt6.com)
首先,您必須確認您正在運行的Ubuntu上的哪個版本的MySQL,因爲命令會有所不同。
mysql -V
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
記下你的“Distrib”。在上面的例子中,我們使用的是MySQL 5.7。請稍後注意這一點。
2.使用skip-grant-table重新啓動MySQL()
爲了跳過授權表並重置root密碼,我們必須先停止MySQL服務。
sudo /etc/init.d/mysql stop
確保目錄 /var/run/mysqld
存在並更正所有者設置。
sudo mkdir /var/run/mysqld
sudo chown mysql /var/run/mysqld
現在用--skip-grant-tables
選項啓動MySQL 。這&
是必需的。
sudo mysqld_safe --skip-grant-tables&
你應該看到類似的東西:
[1] 1283 user@server:~$ 2019-02-12T11:15:59.872516Z mysqld_safe Logging to syslog. 2019-02-12T11:15:59.879527Z mysqld_safe Logging to '/var/log/mysql/error.log'. 2019-02-12T11:15:59.922502Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
您可能需要按ENTER
才能返回Linux BASH提示符。
3.更改MySQL Root密碼
您現在可以在沒有密碼的情況下登錄MySQL root帳戶。
sudo mysql --user=root mysql
登錄後,您將看到 mysql>
提示。
對於Ubuntu上的MySQL 5.7或更高版本,請運行此命令以更改root密碼。替換 your_password_here
爲您自己的。
mysql> update user set authentication_string=PASSWORD('your_password_here') where user='root';
對於Ubuntu上的MySQL 5.6或更低版本,請運行此命令以更改root密碼。替換 your_password_here
爲您自己的。
mysql> update user set Password=PASSWORD('your_password_here') where user='root';
重置root密碼時,還必須將auth插件更改爲 mysql_native_password
mysql> update user set plugin="mysql_native_password" where User='root';
刷新特權。
mysql> flush privileges;
退出MySQL。
mysql> exit
確保在再次啓動服務之前停止所有MySQL進程。
sudo killall -u mysql
再次啓動MySQL。
sudo /etc/init.d/mysql start
4.測試新密碼
再次登錄MySQL,現在應該提示您輸入密碼http://www.1nt6.com。
sudo mysql -p -u root
輸入您的密碼。如果正確,你應該看到
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)
你們都完成了!