1、问题描述以及环境
打开navicat发现之前一直用的连接失效,错误弹窗如下。
- Windows10
- MySql8.0
操作之前先关闭MySQL服务:
Win + R 输入 services.msc
2、修改my.ini文件以及创建data文件夹
进入到C:\Program Files\MySQL\MySQL Server 8.0下,与网上搜的不同的是我没有.ini文件以及data文件夹,用管理员权限创建my.ini文件,内容如下:
# 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]
skip-grant-tables
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_bin
init_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.0
datadir = C:\Program Files\MySQL\MySQL Server 8.0\data
port = 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 = 128M
sort_buffer_size = 16M
read_rnd_buffer_size = 16M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
3、管理员权限打开命令行窗口
mysqld --console --skip-grant-tables --shared-memory
mysqld --skip-grant-tables
4、无密码进入mysql
然后再打开一个命令行窗口,输入mysql可以直接进;
show tables可以看见最下面有一个user表;
在user表中可以查看用户以及密码如下:
select user,host,authentication_string from user;
密码有可能是明文,有可能是加密的。
再输入以下:
flush privileges;
set password for root@localhost='root';
再用navicat连接,成功连接。
参考博客:
Mysql 80 解决忘记密码无法跳过错误 mysqld --console --skip-grant-tables --shared-memory 代码无效的问题
Mysql8.0数据库修改密码/无法修改密码【改用"mysqld --console --skip-grant-tables --shared-memory"修改密码】
windows下忘记MySQL用户名与密码的办法(最新亲测可用)
PS:
😥搭环境需谨慎orz