计算机知识分享网

【mysql】windows中忘记MySQL初始化密码

关闭服务,卸载安装的服务

net stop mysql3307

mysqld -remove mysql3307
删除MySQL目中的data文件夹
安装MySQL服务
mysqld install mysql3307  
初始化mysql
mysqld --initialize
启动、停止MySQL服务,可以再服务中也可以再dos终端

// 启动MySQL服务
net start mysql3307

// 关闭MySQL服务
net stop mysql3307
mysql初始化时,会生成一个随机密码,

初始密码在上图data文件夹下的xxx.err文件中,可以用记事本打开找到

[Note] A temporary password is generated for root@localhost: yoRked2wy:s2hz
连接MySQL数据库,修改密码
mysql -uroot -p -h192.154.1.43 -P3307

Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.40

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>  SET PASSWORD = PASSWORD('davidZheng123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> ALTER USER ‘root‘@‘localhost‘ PASSWORD EXPIRE NEVER;
ERROR 1396 (HY000): Operation ALTER USER failed for '‘root‘'@'‘localhost‘'
mysql>  ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected (0.00 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

#Mysql