Home  >  Article  >  Database  >  如何找回mysql密码(linux/windows)

如何找回mysql密码(linux/windows)

WBOY
WBOYOriginal
2016-06-07 17:51:171355browse

我们经常会把mysql root的密码给忘了,那么要如何找回来呢,重新安装太麻烦了难得配置,下面我来介绍一下关于在linux中和mysql中两种找回密码的办法吧。

Like *UNIX 版本:

下面是错误答案:

首先停止MySQL服务,然后使用skip-grant-tables参数启动它:

 代码如下 复制代码
shell> /etc/init.d/ stop
shell> mysqld_safe --skip-grant-tables &此时无需授权就可以进入到MySQL命令行,使用SQL重置MySQL密码:UPDATE mysql.user SET Password=PASSWORD('...') WHERE User='...' AND Host= '...';

FLUSH PRIVILEGES;为什么说它是错误答案?因为在单纯使用skip-grant-tables参数启动服务后,除非服务器屏蔽了外网访问,否则除了自己,其它别有用心的人也可能访问数据库,尽管重置密码所需的时间很短,但俗话说不怕贼偷就怕贼惦记着,任何纰漏都可能酿成大祸。

下面是正确答案:

关键点是:在使用skip-grant-tables参数的同时,还要加上skip-networking参数:

 代码如下 复制代码
shell> mysqld_safe --skip-grant-tables --skip-networking

&接着使用SQL重置密码后,记得去掉skip-networking,以正常方式重启MySQL服务:

 代码如下 复制代码
shell> /etc/init.d/mysqld restart

上面的方法需要重启两次服务,实际上还能更优雅一点,重启一次即可:

首先需要把用到的SQL语句保存到一个文本文件里(/path/to/init/file):

 代码如下 复制代码

UPDATE mysql.user SET Password=PASSWORD('...') WHERE User='...' AND Host= '...';
FLUSH PRIVILEGES;接着使用init-file参数启动MySQL服务,

shell> /etc/init.d/mysql stop
shell> mysqld_safe --init-file=/path/to/init/file

&此时,密码就已经重置了,最后别忘了删除文件内容,免得泄露密码。

提示:本文用到的参数都是通过命令行mysqld_safe传递的,实际上也可以通过my.cnf。

参考:关于重置密码,官方文档里有专门的描述:How to Reset the Root Password。

Windows版本:

1.以系统管理员身份登陆系统。
2.打开cmd-----net start 查看mysql是否启动。启动的话就停止net stop mysql.
3.我的mysql安装在d:mysql5bin下。
4.跳过权限检查启动mysql.
    d:mysql5binmysqld-nt --skip-grant-tables --skip-networking
5.重新打开cmd。进到d:mysql5bin下:
    d:mysql5binmysqladmin -uroot flush-privileges password "newpassword"
    d:mysql5binmysqladmin -u root -p shutdown  这句提示你重新输密码。
6.在cmd里net start mysql
7.搞定了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn