MySQL reset password when forgot

If you don't remember the password you set for root and need to reset it, follow these steps:
Stop the mysqld server, this varies per install
Run the server in safe mode with privilege bypass

sudo mysqld_safe --skip-grant-tables

In a new window connect to the database, set a new password and flush the permissions & quit:

mysql -u root

For MySQL older than MySQL 5.7 use:

UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root';

For MySQL 5.7+ use:

USE mysql;

UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root';

Refresh and quit:

FLUSH PRIVILEGES;

\q

Stop the safe mode server and start your regular server back. The new password should work now.
這些做完之后,還要在此修改密碼才能進行一些sql操作:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

查看sql 運行port:

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)

Reference:

https://stackoverflow.com/questions/6474775/setting-the-mysql-root-user-password-on-os-x
https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容