[MySQL]mysql忘记密码的解决办法和实际操作演示

mysql忘记root密码解决办法

 

一、可以登录进去的修改,限于软件记录了密码但是自己忘了

方法1: 用SET PASSWORD命令 

首先登录MySQL。 
格式:mysql> set password for 用户名@localhost = password('新密码'); 
例子:mysql> set password for root@localhost = password('123'); 

方法2:用mysqladmin 
格式:mysqladmin -u用户名 -p旧密码 password 新密码 
例子:mysqladmin -uroot -p123456 password 123 

方法3:用UPDATE直接编辑user表 
首先登录MySQL。 
mysql> use mysql; 
mysql> update user set password=password('123') where user='root' and host='localhost'; 
mysql> flush privileges; 
二、无法登录的修改方式,限于有权限修改mysql安装文件(此方法适用于linux)
方法4:在忘记root密码的时候,可以这样 
以windows为例: 
1.进入mysql安装目录,打开my.ini文件

2.找到[mysqld]配置行(注意,不是[mysql]),在其下一行输入:skip-grant-tables(或者:--skip-grant-tables,两者区别是有时两个横线会导致服务无法重启需要测试应当添加否。)

3.cmd命令:services.msc,找到mysql服务并重启(使修改的配置文件生效)

4.mysql命令行:use mysql(进入系统配置库)

5.mysql命令行:update user set password=password("123") where user="root";(别忘了最后加分号,分号意味着命令的结束) 

6.mysql命令行:flush privileges;(刷新缓存,更新数据库读取的密码别忘了最后加分号,分号意味着命令的结束)

7.退出mysql,或重启服务,密码设置成功。

 

 

实际操作:
1.更改配置文件my.ini,在[mysqld]下面增加skip-grant-tables
 

2.cmd中重启mysql服务。 net stop mysql; net start mysql;

3.无密码登陆进mysql服务端,修改root用户密码,

C:\WINDOWS\system32>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

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 for 'root'@'localhost' =password('root');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges ;
Query OK, 0 rows affected (0.02 sec)

mysql> set password for 'root'@'localhost' =password('root');
Query OK, 0 rows affected (0.00 sec)

 

 

PS: 修改默认的密码
使用 set password for 'username'@'host' = password('newpassword') 命令修改新的密码。

根据网友Marksmanbat评论,如果在执行该步骤的时候出现ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 错误。则执行下 flush privileges 命令,再执行该命令即可。

 

4. 把配置文件my.ini的文件注释掉新加行,保存。

5.重启mysql。

 

posted on 2020-03-11 19:51  gaomatlab  阅读(457)  评论(0编辑  收藏  举报

导航