好久没登录数据库,突然发现密码忘了,在csdn上了找了好久,整理一套完整版解决方案:
1、查看本地mysql安装目录,是否有这两个文件:
如果没有,就需要配置
2、停止mysql服务,进入本地mysql安装目录,选定地址栏,输入cmd,
在cmd下 (以管理员身份运行)输入以下命令执行:
net stop mysql
3、跳过MySql密码验证登录服务,以管理员身份运行cmd,进入mysql安装目录下的bin目录,然后输入如下命令:
//8.0版本以下有效
mysqld --skip-grant-tables
出现以上提示,代表设置跳过密码验证登录成功。
如果不正常执行,那么就需要检查一下你的mysql路径下的文件了,是否缺少data和my.ini这两个文件。
4、先删除自己的mysql服务。
//根据自己的服务改成相应的名字
sc delete MySql80
5、如果本地mysql安装目录下,的确缺少以上data,my.ini两个文件,那么需要配置:
//简易版
[mysql]
default-character-set=utf8
[mysqld]
default-storage-engine=INNODB
basedir=C:/Program Files/MySQL/MySQL Server 8.0
datadir=C:/Program Files/MySQL/MySQL Server 8.0/data
其中basedir和databir记得改成自己mysql安装目录的路径。
//复杂版
# 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 = 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
6、接着打开cmd窗口,在mysql的bin目录下执行如下语句:
mysqld --initialize-insecure --user=mysql
//后面的路径改成自己的ini文件的路径,这个操作是安装MySql服务,同时设置绑定my.ini配置文件
mysqld --install "MySql80(这里是你的服务名)" --defaults-file="C:\Program Files\MySQL\MySQL Server 8.0\my.ini(是你本地的my.ini目录)"
7、打开MySql服务
net start MySql80
MySql80改为你的服务名
8、cmd下输入 :
mysql -u root -p
不需要密码输入了!
9、修改新密码:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
成功解决!