1

忘记了Mysql下root密码,如何重置新密码?

Posted by 撒得一地 on 2015年12月16日 in Mysql优化

忘了你的Mysql root用户的密码?别担心,下面将教你如何解决。

当你尝试登陆Mysql下root用户,而没有输入密码时(root账号已被设置密码),你可能会得到"Access Denied"消息提示,表示需要登陆密码。

本文阐述了当你忘记了Mysql旧密码时,如何重新设置一个新密码来恢复root账户。

当你不记得密码,输入密码错误时,你会得到下面的MySQL错误消息:

# mysql -u root mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

下面是解决方法详细步骤

1.停止Mysql服务

作为第一步,停止Mysql服务器可以使用下面的方法。

# service mysqld stop

(or)

# /etc/rc.d/init.d/mysqld stop

2.添加–-skip-grant-tables到mysqld_safe启动命令中

打开mysql启动脚本,添加–skip-grant-tables,如下所示。

# vi /etc/rc.d/init.d/mysqld

Old Line(修改前): $bindir/mysqld_safe –datadir=$datadir –pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

New Line(修改后): $bindir/mysqld_safe –skip-grant-tables –datadir=$datadir –pid-file=$server_pid_file $other_args >/dev/null 2>&1 & 

在文件中,将上面Old Line 内容修改为    NEW Line 内容。

3.用带有–skip-grant-tables参数开启Mysql服务器

重新开启Mysql服务器

# service mysqld start

Starting MySQL.  [  OK  ]

注意:开启时使用到了刚更新过的/etc/rc.d/init.d/mysqld 脚步文件

4.不用密码登录到Mysql下root用户

因为上面已经跳过了授权表,所以当你尝试登录到Mysql时,这时候不会要求你输密码。

# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.25-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
[Note: MySQL did not ask for any password]

5.用Update命令对root设置新密码

mysql>use mysql
mysql> UPDATE user SET password=PASSWORD('newpassword') WHERE user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

6.停止Mysql服务器

用下面命令停止服务器

# service mysqld stop

(or)

# /etc/rc.d/init.d/mysqld stop

7.在/etc/rc.d/init.d/mysql脚步文件去掉-–skip-grant-table    

# vi /etc/rc.d/init.d/mysql

Old Line: $bindir/mysqld_safe –skip-grant-tables –datadir=$datadir –pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

New Line: $bindir/mysqld_safe –datadir=$datadir –pid-file=$server_pid_file $other_args >/dev/null 2>&1 &

8.启动Mysql服务器

在没有–skip-grant-tables选项下启动Mysql服务器。这时候Mysql会提示你输入密码。

# service mysql start

Starting MySQL. [  OK  ]        

9.用新密码登录Mysql账户

# mysql -u root -pnewpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1

Server version: 5.1.25-rc-community MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> 

英文原文:http://www.tutorialized.com/tutorial/How-to-Reset-a-MySQL-Root-Password/84798

译文地址:http://coderschool.cn/1304.html

转载请注明英文原文地址和译文地址

标签:,

上一篇:

下一篇:

相关推荐

1 Comment

发表评论

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

6 + 0 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.