重設MySQL root密碼

好久沒碰過更換root密碼的這爛事了,今天重溫了一遍。。。

一般上,伺服器一旦運行了,最好的情況是永遠無需碰它的設置。但有時候真的無可避免。這回,在使用了往常用的修復手法后,卻被系統直接KO,被系統提醒我 “請仔細詳讀用戶手冊中,Security這欄目。。。”

找了來看,一頭霧水。最後去問神了,結果還是一直被KO。直到最後在Stack Overflow的這篇文章内,才找到真正的答案。。。

是的,系統升級了,往常用的mysqld_safe已經不存在了。現在更方便的衹需設置好伺服器的運作環境常量,重啓即可。長話短說,流程轉載在下方:

1. Stop mysql:

> systemctl stop mysqld

2. Set the mySQL environment option

> systemctl set-environment MYSQLD_OPTS=”–skip-grant-tables”

3. Start mysql usig the options you just set

> systemctl start mysqld

4. Login as root

> mysql -u root

5. Update the root user password with these mysql commands:

mysql> UPDATE mysql.user SET authentication_string = PASSWORD(‘MyNewPassword’) -> WHERE User = ‘root’ AND Host = ‘localhost’;

mysql> FLUSH PRIVILEGES;

mysql> quit

(for 5.7.6 and later, you should use mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewPass’;)

6. Stop mysql

> systemctl stop mysqld

7. Unset the mySQL envitroment option so it starts normally next time

> systemctl unset-environment MYSQLD_OPTS

8. Start mysql normally:

> systemctl start mysqld

9. Try to login using your new password:

> mysql -u root -p

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.