Mysql forgot password how to reset password (detailed steps)

Each method has its applicable situation, choose the appropriate method according to the specific situation. Whichever method you choose, be sure to delete the temporary user and restart the service promptly after resetting the password MySQL.

1. Use to mysqladminreset password

  • Out of service

    # systemctl 启动的使用这个停止
    $ sudo systemctl stop mysql
    
    # mac 本机,可以使用这个或可视化界面停止
    $ sudo mysql.server stop
    
  • Use mysqld_safethe command to start MySQLthe service, skip the permission check, and you may need to press Enter after execution

    $ sudo mysqld_safe --skip-grant-tables &
    
  • Log in to MySQLthe service, and use mysqladminthe command to reset the password

    # 无需密码,会直接登入
    $ mysql -u root  
    
    # 刷新权限
    mysql> FLUSH PRIVILEGES;  
    
    # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    
    # 修改好后退出 MySQL
    mysql> exit
    
  • Stop MySQLthe service and restart the password to log in

    # 1、停止 MySQL 服务上面有,第一条就是
    
    # 2、重启启动 MySQL 服务,并使用密码登入
    # systemctl 启动的使用这个重新启动
    $ sudo systemctl start mysql
    # mac 本机,可以使用这个或可视化界面启动
    $ sudo mysql.server start
    
    # 3、密码登录
    $ mysql -u root -p
    
    # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
    

2. Use to mysqldreset password

  • Out of service

    # systemctl 启动的使用这个停止
    $ sudo systemctl stop mysql
    
    # mac 本机,可以使用这个或可视化界面停止
    $ sudo mysql.server stop
    
  • Use mysqld_safethe command to start MySQLthe service, skip the permission check, and press Enter after execution

    $ sudo mysqld_safe --skip-grant-tables &
    
  • Log in to MySQLthe service, and create a new temporary user with rootpermissions

    # 无需密码,会直接登入
    $ mysql -u root  
    
    # 刷新权限
    mysql> FLUSH PRIVILEGES;  
    
    # 创建一个临时用户 temp_root 及 temp_password
    mysql> CREATE USER 'temp_root'@'localhost' IDENTIFIED BY 'temp_password';  
    
    # 设置权限
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'temp_root'@'localhost' WITH GRANT OPTION;
    
    # 修改好后退出 MySQL
    mysql> exit
    
  • Stop MySQLthe service, restart the password login, and use ALTERthe command to modify rootthe user password

    # 1、停止 MySQL 服务上面有,第一条就是
    
    # 2、重启启动 MySQL 服务,并使用密码登入
    # systemctl 启动的使用这个重新启动
    $ sudo systemctl start mysql
    # mac 本机,可以使用这个或可视化界面启动
    $ sudo mysql.server start
    
    # 3、临时用户密码登录
    $ mysql -u temp_root -p
    
    # 4、这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    
    # 5、修改好后退出 MySQL
    mysql> exit
    
    # 6、停止 MySQL 服务上面有,第一条就是
    
    # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
    
  • mysqld_safeUse the command again to start MySQLthe service, skip the permission check, and delete the temporary user

    $ sudo mysqld_safe --skip-grant-tables &  
    
    $ mysql -u root  
    
    mysql> FLUSH PRIVILEGES;  
    
    mysql> REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'temp_root'@'localhost';  
    
    mysql> DROP USER 'temp_root'@'localhost';  
    
    mysql> exit
    
  • Restart MySQLthe service again and rootlog in with the modified password

    # 1、停止 MySQL 服务上面有,第一条就是
    
    # 2、重启启动 MySQL 服务,并使用密码登入
    # systemctl 启动的使用这个重新启动
    $ sudo systemctl start mysql
    # mac 本机,可以使用这个或可视化界面启动
    $ sudo mysql.server start
    
    # 3、密码登录
    $ mysql -u root -p
    

3. Use MySQLthe security mode to reset the password

  • Out of service

    # systemctl 启动的使用这个停止
    $ sudo systemctl stop mysql
    
    # mac 本机,可以使用这个或可视化界面停止
    $ sudo mysql.server stop
    
  • start MySQLsafe mode

    $ sudo mysqld_safe --skip-grant-tables --skip-networking &
    
  • Log in to MySQLthe service, and use mysqladminthe command to reset the password

    # 无需密码,会直接登入
    $ mysql -u root  
    
    mysql> FLUSH PRIVILEGES;  
    
    # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    
    # 修改好后退出 MySQL
    mysql> exit
    
  • stop MySQLsafe mode

    $ sudo killall mysqld_safe
    
    # 如果在本地机器上执行上面命令无法停止安全模式,可以通过杀死进程方式解决
    # 1、列出 mysqld_safe 的进程
    $ ps aux | grep mysqld_safe
    
    # 2、拿到 PID 全部杀死,应该是第二列,就几位纯数字的,如果不知道可以单独输出 $ ps aux 看一下。
    $ sudo kill -9 xxx xxx ...
    
  • Stop MySQLthe service and restart the password to log in

    # 1、停止 MySQL 服务上面有,第一条就是,如果通过 $ sudo kill 可以一起就杀死了。
    
    # 2、重启启动 MySQL 服务,并使用密码登入
    # systemctl 启动的使用这个重新启动
    $ sudo systemctl start mysql
    # mac 本机,可以使用这个或可视化界面启动
    $ sudo mysql.server start
    
    # 3、密码登录
    $ mysql -u root -p
    
    # 附:如果再次使用 mysql -u root 这样免密登录是不成功的,停止重启后会失效。
    

4. Use to my.cnfreset password

  • This method 方式一is basically the same as above, except that the skip permission instruction in the command is written in my.cnf, and then removed after modification.

  • Before modifying,  my.cnf be sure to close  mysql the process and close it  mysql, otherwise you may encounter  mysql problems  sock that cannot be connected!

  • Out of service

    # systemctl 启动的使用这个停止
    $ sudo systemctl stop mysql
    
    # mac 本机,可以使用这个或可视化界面停止
    $ sudo mysql.server stop
    
  • open my.cnf, create one if none

    $ sudo vim /etc/my.cnf
    

    After entering vimthe editor to edit, find it [mysqld], press ithe key on the keyboard to enter the editing mode, and [mysqld]add in any line after skip-grant-tablesto skip the password verification process. Press ESCto exit editing, key in :wq, and press Enter to save and exit.

    [mysqld]
    skip-grant-tables
    
  • start mysqldservice

     # systemctl 启动的使用这个重新启动
    $ sudo systemctl start mysql
    
    # mac 本机,可以使用这个或可视化界面启动
    $ sudo mysql.server start
    
  • Log in to MySQLthe service, and use mysqladminthe command to reset the password

    # 无需密码,会直接登入
    $ mysql -u root  
    
    # 这里的 'new_password' 是要设置的新密码,可以根据需要进行修改。此命令将修改 root 用户在本地 MySQL 服务上的密码。
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
    
    # 刷新权限
    mysql> FLUSH PRIVILEGES;  
    
    # 修改好后退出 MySQL
    mysql> exit
    
  • Open it again my.cnf, remove what you just added skip-grant-tables, save and restart MySQLthe service, and log in with the new password.

Guess you like

Origin blog.csdn.net/zz00008888/article/details/131935462