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

你可能感兴趣的:(MySQL reset password when forgot)