mysql 8.0 忘记登录密码怎么办
将近两个月没有碰MYSQL 了,刚刚捡起来,结果发现登录密码忘记了,试了N次,没办法求助网络,但是单凭一篇教程还真是解决不了问题,最后综合若干个教程才重新设置了我的密码。
MySQL 版本 8.0.28
一、无密码进入MySQL
忘记了密码,只能通过越过登录进入 MySQL 。
A. 以管理员方式运行CMD
B.关闭MySQL服务
语句:net stop mysql
C:\Windows\system32>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。
C.更改无密码进入方式
语句:mysqld --console --skip-grant-tables --shared-memory
C:\Windows\system32>mysqld --console --skip-grant-tables --shared-memory
2022-08-16T12:46:38.901983Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO
_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with st
rict mode. They will be merged with strict mode in a future release.
2022-08-16T12:46:38.901986Z 0 [System] [MY-010116] [Server] C:\Program Files\mys
ql-8.0.28-winx64\bin\mysqld.exe (mysqld 8.0.28) starting as process 4292
2022-08-16T12:46:38.964384Z 1 [System] [MY-013576] [InnoDB] InnoDB initializatio
n has started.
2022-08-16T12:46:39.291986Z 1 [System] [MY-013577] [InnoDB] InnoDB initializatio
n has ended.
2022-08-16T12:46:39.447985Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx repor
ted: 'All I/O interfaces are disabled, X Protocol won't be accessible'
2022-08-16T12:46:39.557187Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p
em is self signed.
2022-08-16T12:46:39.572784Z 0 [System] [MY-013602] [Server] Channel mysql_main c
onfigured to support TLS. Encrypted connections are now supported for this chann
el.
2022-08-16T12:46:39.619588Z 0 [System] [MY-010931] [Server] C:\Program Files\mys
ql-8.0.28-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.28' socke
t: '' port: 0 MySQL Community Server - GPL.
D. 以普通身份打开另一个CMD窗口
E. 进入MySQL
语句:mysql
C:\Users\zkz>mysql
mysql>
至此,我们就实现了无密码进入MySQL
二、修改密码
A. 清空密码
语句:update user set authentication_string=‘’ where user=‘root’;
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
B. 刷新
语句:flush privileges;
mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)
C. 更改密码
语句:ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH
mysql_native_password BY '123';
Query OK, 0 rows affected (0.02 sec)
三、用新的密码进入MySQL
A. 以管理员身份进入CMD
B. 启动MySQL服务
语句:net start mysql
C:\Windows\system32>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
C. 用新密码登录
C:\Windows\system32>mysql -u root -p
Enter password: ***
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>