重置MySQL密码:
停止MySQL服务,使用skip-grant-tables参数启动
1
2
<code>service mysqld stop;</code>
<code>mysqld_safe --skip-grant-tables --user=mysql&</code>
此时无需授权就可以进入到MySQL命令行
<code>mysql>UPDATE mysql.user SET Password=PASSWORD(</code><code>'...'</code><code>) WHERE User=</code><code>'...'</code> <code>AND Host= </code><code>'...'</code><code>;FLUSH PRIVILEGES;</code>
以上的操作还是会有一些纰漏:
重置密码这个时间段内,任何人都能连接MySQL(除非数据库服务器屏蔽了外网访问);
推荐方式:
3
4
<code>shell>service mysqld stop;</code>
<code>//</code><code>将UPDATE mysql.user SET Password=PASSWORD(</code><code>'...'</code><code>) WHERE User=</code><code>'...'</code> <code>AND Host= </code><code>'...'</code><code>;FLUSH PRIVILEGES;这句SQL语句写入到mysql有权限读取的目录;</code>
<code>mysql>mysqld_safe --init-</code><code>file</code><code>=</code><code>/path/to/init/file</code> <code>--user=mysql &</code>
<code>//</code><code>最后删除该文件,免得密码泄露;</code>
避免重启两次MySQL!!!
值得一提的是:在MySQL5.6中,使用mysql_config_editor加密.mylogin.cnf 进行登录;只要该文件存在,就不用考虑丢失密码的问题啦!
本文转自 位鹏飞 51CTO博客,原文链接:http://blog.51cto.com/weipengfei/1346062,如需转载请自行联系原作者