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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容