mysql忘记密码的解决办法

来源:互联网 发布:遗传算法 船舶调度 编辑:程序博客网 时间:2024/05/21 11:11

第一种办法:修改配置文件

1.修改配置文件/etc/my.cnf

在【mysqld】添加一行

skip-grant-tables


2.重启mysqld服务

[root@mysql-server ~]# service mysqld restart

3.登录数据库

[root@mysql-server ~]# mysql
修改root用户密码:

mysql> update mysql.user set authentication_string=password('redhat') where user='root' and Host = 'localhost';
重启数据库:

[root@mysql-server ~]# service mysqld restart

4.用root用户登录数据库

[root@mysql-server ~]# mysql -uroot -p

第二种办法:在命令行上进行修改


1.关闭mysqld服务

[root@mysql1 ~]# service mysqld stop

2.进入数据库的安全模式,此时不需要输入密码

[root@mysql1 ~]# mysqld_safe --skip-grant-tables &
若为了安全则可执行下面这条命令,这样就不会被远程连接:

[root@mysql1 ~]# mysqld_safe --skip-grant-tables --skip-networking &

3.登录数据库修改密码

[root@mysql1 ~]# mysql
mysql> update mysql.user set authentication_string=password('redhat') where user='root' and Host = 'localhost';


4.重启服务
[root@mysql1 ~]# service mysql restart

此时就可以用新密码重新登录了



重新登录之后,任意输入一个命令,会出现下面的情况:

mysql> select version();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

我们按他说的做:

mysql> alter user 'root'@'localhost' identified by 'redhat';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

出现了这个错,我们需要修改的密码规则:

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> alter user 'root'@'localhost' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)