侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

数据库  /  管理员 发布于 6年前   156

前言

一直以来,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 -pEnter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)[root@mytestlnx02 ~]#

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

[root@mytestlnx02 ~]# ps -ef | grep -i mysqlroot  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=mysqlmysql 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.sockroot  23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql[root@mytestlnx02 ~]# service mysqld stopStopping 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 startStarting mysqld: [ OK ][root@mytestlnx02 ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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 namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> 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

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

[root@mytestlnx02 ~]# service mysqld startStarting mysqld: [ OK ][root@mytestlnx02 ~]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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,不再支持旧密码格式;

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 mysqlmysql-community-client-5.7.18-1.el6.i686mysql-community-libs-5.7.18-1.el6.i686perl-DBD-MySQL-4.013-3.el6.x86_64mysql-community-server-5.7.18-1.el6.i686mysql-community-common-5.7.18-1.el6.i686mysql-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.log2017-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)

总结

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


  • 上一条:
    Mysql 5.7.18安装方法及启动MySQL服务的过程详解
    下一条:
    Mysql中基本语句优化的十个原则小结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客