环境:

系统Windows 10
MySQL-8.0

操作步骤:

  1. 停止MySQL服务。
    net stop MySql80

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

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

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

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

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

    //根据自己的服务改成相应的名字sc delete MySql80
  7. 接下来新建my.ini配置文件代码如下:

    //简易版[mysql]default-character-set=utf8[mysqld]default-storage-engine=INNODBbasedir=C:/Program Files/MySQL/MySQL Server 8.0datadir=C:/Program Files/MySQL/MySQL Server 8.0/data
    //复杂版# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[client]default-character-set = utf8mb4[mysql]default-character-set = utf8mb4[mysqld]character-set-client-handshake = FALSEcharacter-set-server = utf8mb4collation-server = utf8mb4_bininit_connect='SET NAMES utf8mb4'# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.basedir = C:\Program Files\MySQL\MySQL Server 8.0datadir =  C:\Program Files\MySQL\MySQL Server 8.0\dataport = 3306# server_id = .....# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.join_buffer_size = 128Msort_buffer_size = 16Mread_rnd_buffer_size = 16Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

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

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

    mysqld --initialize-insecure --user=mysql

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

    //后面的路径改成自己的ini文件的路径,这个操作是安装MySql服务,同时设置绑定my.ini配置文件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';