欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解mysql5.7密码忘记解决方法

程序员文章站 2023-11-13 08:03:34
env: [root@centos7 ~]# uname -r 3.10.0-514.el7.x86_64 [root@centos7 ~]# cat /et...

env:

[root@centos7 ~]# uname -r
3.10.0-514.el7.x86_64
[root@centos7 ~]# cat /etc/redhat-release 
centos linux release 7.3.1611 (core) 
[root@centos7 ~]# rpm -qa mysql
[root@centos7 ~]# rpm -qa |grep mysql
mysql-community-common-5.7.26-1.el7.x86_64
mysql-community-client-5.7.26-1.el7.x86_64
mysql57-community-release-el7-11.noarch
mysql-community-server-5.7.26-1.el7.x86_64
mysql-community-libs-5.7.26-1.el7.x86_64
mysql-community-libs-compat-5.7.26-1.el7.x86_64

登陆时出错:

[root@centos7 ~]# mysql -u root -p
enter password: 
error 1045 (28000): access denied for user 'root'@'localhost' (using password: no)

密码忘记了(和刚安装后不知道密码一样)

网上有人说mysql的密码是空密码,其实在mysql5.7版本之后,密码不再是空密码了,

如果是刚安装的,可以在mysql的日志文件找到

grep 'temporary password' /var/log/mysqld.log

补充:如果找到mysql提供的密码,可以使用

mysqladmin -u root -p 'mysql提供的密码' password ‘自己的新密码' 

直接修改mysql的密码,但这种方法存在安全隐患,毕竟密码在命令行上显示了,不建议但不反对。

如果是忘记,修改如下:

1.修改 /etc/my.cnf,加入 skip-grant-tables;

[root@centos7 ~]# vim /etc/my.cnf

空白位置加入,并保存退出;

[mysqld]
     
skip-name-resolve
skip-grant-tables
[root@centos7 ~]# systemctl restart mysqld

2.空密码直接进入mysql;

[root@centos7 ~]# mysql -u root -p
enter password: (这里是空密码,直接回车)
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 2
server version: 5.7.26 mysql community server (gpl)
 
copyright (c) 2000, 2019, oracle and/or its affiliates. all rights reserved.
 
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
 
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
 
mysql>

进入mysql库;

mysql> use mysql
reading table information for completion of table and column names
you can turn off this feature to get a quicker startup with -a
 
database changed
mysql>

这里的mysql并不是没变,数据库位置是变化的;

3.修改密码:update user set authentication_string=password('newpassword') where user='root';

mysql> update user set authentication_string=password('newpassword') where user='root';
query ok, 1 row affected, 1 warning (0.00 sec)
rows matched: 1 changed: 1 warnings: 1
 
mysql> 
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
 
mysql>

4改回/etc/my.cnf

注释掉 #skip-grant-tables

[root@centos7 ~]# vim /etc/my.cnf
[mysqld]
     
skip-name-resolve
#skip-grant-tables
[root@centos7 ~]# systemctl restart mysqld

5.用新的密码再进入mysql;

[root@centos7 ~]# mysql -u root -p 
enter password: (之前演示为newpassword)
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 6
server version: 5.7.26
 
copyright (c) 2000, 2019, oracle and/or its affiliates. all rights reserved.
 
oracle is a registered trademark of oracle corporation and/or its
affiliates. other names may be trademarks of their respective
owners.
 
type 'help;' or '\h' for help. type '\c' to clear the current input statement.
 
mysql>

6.更改root密码,更改root密码: alter user 'root'@'localhost' identified by '密码';

修改用户密码;

alter user testuser identified by '123456';

修改当前登录用户

alter user user() identified by '123456';
mysql> alter user user() identified by 'linuxpassword!@#';
error 1819 (hy000): your password does not satisfy the current policy requirements
mysql> alter user user() identified by 'linux123password!@#';
query ok, 0 rows affected (0.00 sec)
 
mysql> 

可以看出,密码的复杂度有了很大的要求;

7.修改完成后就可以继续操作mysql了

mysql> show databases;
+--------------------+
| database      |
+--------------------+
| information_schema |
| mysql       |
| performance_schema |
| sys        |
+--------------------+
4 rows in set (0.00 sec)
 
mysql> exit
bye

以上所述是小编给大家介绍的mysql5.7密码忘记解决方法详解整合,希望对大家有所帮助