Mysql忘记密码时重置密码

方法一:

一个中心:停止服务,载入安全模式

killall -TERM mysqld

然后

mysqld --skip-grant-tables &

两个基本点:更新user表,刷新权限

use mysql;
 update user set password=password("newpass") where user="root";
 flush privileges;

如果不方便用mysql的cli界面交互
调用mysqladmin即可

mysqladmin -u root flush-privileges password "newpassword"

 

方法二(debian亲测):

直接使用/etc/mysql/debian.cnf文件中[client]节提供的用户名和密码 备用

mysql -u debian-sys-maint -p
Enter password: <输入[client]节的密码>

mysql> show databases;

mysql> use mysql;

mysql> show tables;

这个时候使用user表

mysql> UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;

Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit

# mysql -u root -p
Enter password: <输入新设的密码newpassword>
mysql>

方法三(centos 6.2亲测,等同于方法一):

1.确认服务器处于安全的状态,在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护,不连网操作最好,实在不行挑个好时候,操作要快
2.修改MySQL的登录设置:

# vi /etc/my.cnf

在[mysqld]的段中加上一句:skip-grant-tables
例如:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-grant-tables

保存并且退出vi。
3.重新启动mysqld

# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

4.登录并修改MySQL的root密码

[root@vps ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.61 Source distribution
Copyright (c) 2000, 2011, 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=password('jpuyy') where user = 'root';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye

5.将MySQL的登录设置修改回来

# vi /etc/my.cnf

将刚才在[mysqld]的段中加上的skip-grant-tables删除
保存并且退出vi。
6.重新启动mysqld

# /etc/init.d/mysqld restart
Stopping MySQL: [ OK ]
Starting MySQL: [ OK ]

附加windows下的重置方法

开始-运行-cmd

net stop mysql

然后进入到mysql的安装位置,运行如下命令

mysqld-nt.exe –skip-grant-tables

再打开一个cmd窗口

运行msyql -u root -p,不用输入密码即可进入

> use mysql

> UPDATE user SET Password=PASSWORD(‘newpassword’) where USER=’root’;

>flush privileges;

最后在任务管理器里将mysql-nt的任务和进程都停止掉(如果不停掉会出现“mysql服务无法启动。系统出错。系统发生1067错误。进程意外中止。”),接下来

net start mysql

密码重置完成。

发表评论

电子邮件地址不会被公开。 必填项已用*标注