当前位置:   article > 正文

MySQL忘记密码了怎么办(完整解决步骤)

mysql忘记密码

环境:

系统Windows 10
MySQL-8.0

操作步骤:

  1. 停止MySQL服务。
    net stop MySql80

  2. 打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 

  3. 跳过MySql密码验证登录服务,打开命令窗口cmd,进入mysql安装目录下的bin目录,然后输入如下命令:

    1. //8.0版本以下有效
    2. mysqld --skip-grant-tables
    3. //mysqld –skip-grant-tables实测在mysql8.0中已失效,使用如下命令
    4. mysqld --console --skip-grant-tables --shared-memory
  4. 如果正常执行的话就不能输入了,需要重新打开一个cmd命令窗口,输入mysql命令就可以直接登录了,跳过了密码验证过程。

     

  5. 如果不正常执行,那么就需要检查一下你的mysql路径下的文件了,是否缺少data和my.ini这两个文件。

     

  6. 接下来我们需要先删除自己的mysql服务。

    1. //根据自己的服务改成相应的名字
    2. sc delete MySql80

  7. 接下来新建my.ini配置文件代码如下:

    1. //简易版
    2. [mysql]
    3. default-character-set=utf8
    4. [mysqld]
    5. default-storage-engine=INNODB
    6. basedir=C:/Program Files/MySQL/MySQL Server 8.0
    7. datadir=C:/Program Files/MySQL/MySQL Server 8.0/data
    1. //复杂版
    2. # For advice on how to change settings please see
    3. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    4. # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
    5. # *** default location during install, and will be replaced if you
    6. # *** upgrade to a newer version of MySQL.
    7. [client]
    8. default-character-set = utf8mb4
    9. [mysql]
    10. default-character-set = utf8mb4
    11. [mysqld]
    12. character-set-client-handshake = FALSE
    13. character-set-server = utf8mb4
    14. collation-server = utf8mb4_bin
    15. init_connect='SET NAMES utf8mb4'
    16. # Remove leading # and set to the amount of RAM for the most important data
    17. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
    18. innodb_buffer_pool_size = 128M
    19. # Remove leading # to turn on a very important data integrity option: logging
    20. # changes to the binary log between backups.
    21. # log_bin
    22. # These are commonly set, remove the # and set as required.
    23. basedir = C:\Program Files\MySQL\MySQL Server 8.0
    24. datadir = C:\Program Files\MySQL\MySQL Server 8.0\data
    25. port = 3306
    26. # server_id = .....
    27. # Remove leading # to set options mainly useful for reporting servers.
    28. # The server defaults are faster for transactions and fast SELECTs.
    29. # Adjust sizes as needed, experiment to find the optimal values.
    30. join_buffer_size = 128M
    31. sort_buffer_size = 16M
    32. read_rnd_buffer_size = 16M
    33. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

    其中basedir和databir记得改成自己mysql安装目录的路径。

  8. 接着打开cmd窗口,在mysql的bin目录下执行如下语句:

    mysqld --initialize-insecure --user=mysql

    会自动生成data文件夹。接着再输入如下命令:

    1. //后面的路径改成自己的ini文件的路径,这个操作是安装MySql服务,同时设置绑定my.ini配置文件
    2. mysqld --install "MySql80" --defaults-file="C:\Program Files\MySQL\MySQL Server 8.0\my.ini"

    最后打开MySql服务。

    net start MySql80

  9. 此时再重新登录mysql 会发现不需要密码就可以登录了,输入更改代码命令 改成你不会忘记的密码即可。

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
     

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/597874
推荐阅读
相关标签
  

闽ICP备14008679号