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

MySQL 5.7忘记root密码后修改的详细教程

程序员文章站 2022-04-29 09:12:13
前言 一直以来,mysql的应用和学习环境都是mysql 5.6和之前的版本,也没有去关注新版本mysql 5.7的变化和新特性。今天帮人处理忘记root密码的时时候,发...

前言

一直以来,mysql的应用和学习环境都是mysql 5.6和之前的版本,也没有去关注新版本mysql 5.7的变化和新特性。今天帮人处理忘记root密码的时时候,发现以前的方法不奏效了。

具体情况如下所示:

案例环境如下:

        操作系统 : red hat enterprise linux server release 6.6 (santiago)

        数据库版本: 5.7.18 mysql community server (gpl)

忘记密码,输入错误的密码时遇到下面错误信息:

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

检查mysql服务是否启动,如果启动,关闭mysql服务

[root@mytestlnx02 ~]# ps -ef | grep -i mysql
root  22972  1 0 14:18 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 23166 22972 0 14:18 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root  23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql
[root@mytestlnx02 ~]# service mysqld stop
stopping mysqld: [ ok ]
[root@mytestlnx02 ~]# 

找到mysql的my.cnf配置文件,在/etc/my.cnf (有些版本是/etc/mysql/my.cnf)在里面增加下面一段信息:

[mysqld] 

skip-grant-tables 

然后启动mysql,进入mysql后,修改root密码,操作过程中遇到error 1054 (42s22): unknown column 'password' in 'field list',查了一下user表的表结构,发现原来mysql 5.7下,user表已经没有password字段。加密后的用户密码存储于authentication_string字段。

具体操作过程如下所示:

[root@mytestlnx02 ~]# service mysqld start
starting mysqld: [ ok ]
[root@mytestlnx02 ~]# mysql -u root 
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 4
server version: 5.7.18 mysql community server (gpl)
 
copyright (c) 2000, 2017, 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> 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> update user set password=password('kd8k&dfdl023')
 -> where user='root';
error 1054 (42s22): unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('kd8k&dfdl023') where user='root';
query ok, 1 row affected, 1 warning (0.00 sec)
rows matched: 1 changed: 1 warnings: 1
 
mysql> flush privileges;
query ok, 0 rows affected (0.00 sec)
 
mysql> exit

MySQL 5.7忘记root密码后修改的详细教程

在my.cnf文件中,把刚才加入的那一行“skip-grant-tables”注释或删除掉。 然后重启mysql服务后需要执行命令set password=password('newpassword');后,问题搞定。

[root@mytestlnx02 ~]# service mysqld start
starting mysqld: [ ok ]
[root@mytestlnx02 ~]# mysql -u root -p
enter password: 
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 4
server version: 5.7.18
 
copyright (c) 2000, 2017, 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> use mysql;
error 1820 (hy000): you must reset your password using alter user statement before executing this statement.
mysql> set password=password('kd8k&dfdl023');
query ok, 0 rows affected, 1 warning (0.00 sec)

后面查询了一下相关资料,发现mysql 5.7在安全方面有下一些新特性。

1. 用户表 mysql.user 的 plugin字段不允许为空, 默认值是 mysql_native_password,而不是 mysql_old_password,不再支持旧密码格式;

MySQL 5.7忘记root密码后修改的详细教程

2. 增加密码过期机制,过期后需要修改密码,否则可能会被禁用,或者进入沙箱模式; 是否启用密码过期由参数default_password_lifetime控制。

mysql> show variables like 'default_password_lifetime';
+---------------------------+-------+
| variable_name    | value |
+---------------------------+-------+
| default_password_lifetime | 0  |
+---------------------------+-------+
1 row in set (0.00 sec)
 
mysql>

3:增加了密码安全等级以及密码复杂度设置。参数如下:

mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| variable_name      | value |
+--------------------------------------+--------+
| validate_password_check_user_name | off |
| validate_password_dictionary_file |  |
| validate_password_length    | 8  |
| validate_password_mixed_case_count | 1  |
| validate_password_number_count  | 1  |
| validate_password_policy    | medium |
| validate_password_special_char_count | 1  |
+--------------------------------------+--------+
7 rows in set (0.00 sec)

4. 使用 mysql_install_db 初始化时,默认会自动生成随机密码,随机密码放在/var/log/mysqld.log中,并且不创建除 root@localhost和mysql.sys@localhost 外的其他账号,也不创建 test 库;

[root@mytestlnx02 mysql]# yum localinstall mysql-community-{server,client,common,libs}-* 
[root@mytestlnx02 mysql]# rpm -qa | grep -i mysql
mysql-community-client-5.7.18-1.el6.i686
mysql-community-libs-5.7.18-1.el6.i686
perl-dbd-mysql-4.013-3.el6.x86_64
mysql-community-server-5.7.18-1.el6.i686
mysql-community-common-5.7.18-1.el6.i686
mysql-community-libs-compat-5.7.18-1.el6.i686
[root@mytestlnx02 mysql]# service mysqld start
 
initializing mysql database: [ ok ]
installing validate password plugin: [ ok ]
starting mysqld: [ ok ]
[root@mytestlnx02 mysql]# 
[root@mytestlnx02 mysql]# grep 'temporary password' /var/log/mysqld.log
2017-05-05t06:10:57.802143z 1 [note] a temporary password is generated for root@localhost: w99s(m-q_ml:
 
mysql> select user ,host from user;
+-----------+-----------+
| user  | host  |
+-----------+-----------+
| mysql.sys | localhost |
| root  | localhost |
+-----------+-----------+
2 rows in set (0.00 sec)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。