1、先找到安装 mysql 的目录,复制路径,如博主的是“ D:\mysql\mysql-8.0.23-winx64\bin ”。
2、先用管理员运行 DOS 界面,输入“net stop mysql”,停止 mysql 服务,再按键 “ Win + R ”,输入 “ cmd ”,即如下图:
PS C:\WINDOWS\system32> net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。PS C:\WINDOWS\system32>
C:\Users\lucky>D:D:\>cd D:\mysql\mysql-8.0.23-winx64\binD:\mysql\mysql-8.0.23-winx64\bin>
3、输入“ mysqld --console --skip-grant-tables --shared-memory ”,得到下图:(此步目的是为了无需做密码验证就可以直接登录 MySQL 服务器,并且拥有所有的操作权限。)
D:\mysql\mysql-8.0.23-winx64\bin>mysqld --console --skip-grant-tables --shared-memory
2021-05-11T03:36:24.022912Z 0 [System] [MY-010116] [Server] D:\mysql\mysql-8.0.23-winx64\bin\mysqld.exe (mysqld 8.0.23) starting as process 10948
2021-05-11T03:36:24.026432Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2021-05-11T03:36:24.147403Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-05-11T03:36:26.417209Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-05-11T03:36:26.888132Z 0 [Warning] [MY-011311] [Server] Plugin mysqlx reported: 'All I/O interfaces are disabled, X Protocol won't be accessible'
2021-05-11T03:36:26.995600Z 0 [System] [MY-010229] [Server] Starting XA crash recovery...
2021-05-11T03:36:27.016218Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
2021-05-11T03:36:27.290321Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-05-11T03:36:27.290876Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-05-11T03:36:27.349957Z 0 [System] [MY-010931] [Server] D:\mysql\mysql-8.0.23-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.23' socket: '' port: 0 MySQL Community Server - GPL.
4、按键 “ Win + R ”,输入 “ cmd ”,再打开一个新的 DOS 界面,重复第二步,再输入“ mysql ”,出现 “ Welcome to the MySQL monitor. ”等,即如下图:
C:\Users\lucky>D:D:\>cd D:\mysql\mysql-8.0.23-winx64\binD:\mysql\mysql-8.0.23-winx64\bin>mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 8.0.23 MySQL Community Server - GPL
5、此时,先设置密码为空,后面修改密码,即输入“ update mysql.user set authentication_string='' where user='root' and Host ='localhost'; ” ,如下图
mysql> update mysql.user set authentication_string='' where user='root' and Host ='localhost';
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6、再输入“ flush privileges; ” 刷新命令
mysql> flush privileges;
Query OK, 0 rows affected (0.17 sec)
7、输入“ quit ” 退出登录,再重新登录验证,如下图:
mysql> quit
Bye
8、此时,无需密码即可登录 mysql,直接在 D:\mysql\mysql-8.0.23-winx64\bin> 下输入“ mysql.exe -u root ”,按回车键。
D:\mysql\mysql-8.0.23-winx64\bin>mysql.exe -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23Copyright (c) 2000, 2021, 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.
9、然后输入“ ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; ”,修改 root 密码为 123456
别忘了还要再输入“ flush privileges; ” 刷新命令,最后输入 quit 退出即可。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.12 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.13 sec)mysql> quit
Bye
10、用管理员运行 DOS 界面,输入“net start mysql”,开启 mysql 服务,重新登录。
D:\mysql\mysql-8.0.23-winx64\bin>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPLCopyright (c) 2000, 2021, 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>
博主有话要说:
如果在第5步,输入的是“ update mysql.user set authentication_string='123456' where user='root' and Host ='localhost'; ”,博主登录时遇到如下图的错误,即出现“ Access denied for user 'root'@'localhost' using password:YES” 的错误。
D:\mysql\mysql-8.0.23-winx64\bin>mysql -u root -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
看了很多解决方法,最后博主用了上述方法来修改密码。原因的话,可能是 mysql 的版本问题,这里用的版本是8.0以上的。
所以,当查找的解决方法在 my.ini 里添加一行 “ –skip-grant-tables ” 来打开 mysql 时始终打不开。
在“ D:\mysql\mysql-8.0.23-winx64\Data ” 路径下,找到后缀为 err 文件,里面一直提示要用 caching_sha2_password
2021-05-11T04:29:46.354044Z 11 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
所以直接用 “ mysqld –console –skip-grant-tables –shared-memory ” 来不用输入密码直接启动服务,并删去 my.ini 里之前所添加一行 “ –skip-grant-tables ”。接下来就是上述修改密码的步骤了。