mysql忘记登录密码解决方案
解决思路:通过编辑mysql的配置文件,添加skip-grant-tables的标识,然后无密码登录,在登录mysql数据库重置密码,之后恢复配置文件,再用重置的密码进行登陆。
Windows系统
当window系统中,登录mysql时忘记密码,会提示需要正确输入密码:
接下来就教会大家如何设置无密码登录。
停止mysql服务
使用管理员身份打开命令行工具,输入net stop mysql
命令停止mysql服务:
net stop mysql
修改my.ini配置文件
使用编辑工具打开my.ini
配置文件,该文件在安装包的根目录下,如我的mysql安装在D:\Server\Mysql\Mysql Server 5.0
,则my.ini
就在Mysql Server 5.0目录下。
在[mysqld]
下方添加以下内容:
skip-grant-tables=true
重启mysql服务
net start mysql
无密码登录mysql
mysql -uroot
随便输入密码也可以登录,因为此时不验证密码。
mysql重置密码
update mysql.user set Password = password('新密码') where user = 'root';
停止mysql服务,恢复my.ini
配置文件,去掉刚刚添加的skip-grant-tables=true
,重启mysql服务,尝试用新的密码重新登陆。
Linux系统
修改my.cnf配置文件
修改/etc/my.cnf
配置文件,在[mysqld]
下方添加skip-grant-tables
vi /etc/my.cnf
按住shift
+:
,输入wq,按回车保存配置文件。
重启mysql服务
service mysqld restart
无密码登陆mysql
mysql -uroot
或
mysql -uroot -p
提示输入密码直接按回车即可。
mysql重置密码
update mysql.user set authentication_string=password('新密码') where user = 'root';
恢复my.cnf
配置文件,在刚刚配置的skip-grant-tables
前面加上#
表示注释改行或者直接删掉,重启mysql服务,尝试用新的密码重新登陆。
注意:无论是windows还是linux,低版本mysql修改密码是set Password
,高版本是set authentication_string