mysql root密码忘了怎么办?(MAC,也适用与unix和windows)

时间:2021-12-08 15:45:20

我相信无数安装mysql的同学和我一样,对于mysql那个极不人道的临时root密码极为不满。

我在mysql官网下载的mac版本的安装包,我用的是dmg格式的,版本是5.7.18。

附上官网链接:https://dev.mysql.com/downloads/mysql/

我的手比较贱,觉得dmg安装时傻瓜式的,一直按呀按,最后连临时root密码也没记得,根据官网介绍,这个临时密码如果你没记清楚,你就找不到了,而且你用wordkbench链接mysql时必须要用这个root密码,所以开始了辛酸的reset密码的过程。网上关于如何重置root密码的教程可谓是多如牛毛,但是我TM就一个也没试验成功的,为啥呢?后来我才明白,mysql版本不断的更新,reset密码的步骤一直在变,网上的那些教程都太老了,对于像5.7.18这样的新版本不适用。在对网上给出的那些教程彻底失望之后,我还是老老实实的到官网manual中寻找最新的解决方案,

我试验成功的解决步骤:

1,关掉已经在运行的mysql,MAC上可以在偏好设置里直接关掉或者通过命令行

sudo /usr/local/mysql/support-files/mysql.server stop

2,进入目录 

cd /usr/local/mysql/bin

3,获取权限

sudo su

4,使用下面的命令可以跳过验证直接启动服务器

./mysqld_safe --skip-grant-tables

5,重新开个新终端

6,输入命令mysql

mysql

7,输入命令FLUSH PRIVILEGES; 注意带上后面的";"号

FLUSH PRIVILEGES;

8,输入一下命令(凡是版本大于等于5.7.6的都这么输入,老版本的请看文章最后附上官网介绍),MyNewPass就是你的新密码

ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

现在你已经完成了root密码的重置,在workbench中连接吧。


附上链接:

https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html#resetting-permissions-unix


B.5.3.2.3 Resetting the Root Password: Generic Instructions

The preceding sections provide password-resetting instructions specifically for Windows and Unix and Unix-like systems. Alternatively, on any platform, you can reset the password using the mysql client (but this approach is less secure):

  1. Stop the MySQL server if necessary, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management statements such as ALTER
    USER
     and SET PASSWORD. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.

  2. Connect to the MySQL server using the mysql client; no password is necessary because the server was started with--skip-grant-tables:

    shell> mysql
  3. In the mysql client, tell the server to reload the grant tables so that account-management statements work:

    mysql> FLUSH PRIVILEGES;

    Then change the 'root'@'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different host name part, modify the instructions to use that host name.

    MySQL 5.7.6 and later:

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

    MySQL 5.7.5 and earlier:

    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');

You should now be able to connect to the MySQL server as root using the new password. Stop the server and restart it normally (without the --skip-grant-tables and --skip-networking options).

If the ALTER
USER
 statement fails to reset the password, try repeating the procedure using the following statements to modify the user table directly:

UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;