当前位置: 首页 > news >正文

mysql数据库root密码遗忘后,修改root密码

目录

方式一:

方式二:

2.1 也可以像我这样,普通用户登录进去后

2.2 执行如下命令,将已知的user1的加密密文更新到root中

2.3 查询数据库

2.4 用root用户登录

2.5 登录正常,但这会root登录进去后,无法执行具体的sql,只要有操作,会报如下的错:

2.6 需要重新ALTRE下用户信息

2.7 退不退出没关系,在重新执行sql命令时,已显示正常


时间久了,root密码已遗忘,只要普通用户的密码记得。

不幸中的幸运是,创建普通用户时,赋予普通用户的权限足够大。不然就得更改数据库配置,配置文件末尾添加skip-grant-tables属性修改了。那比较麻烦,网络上也有很多博客介绍

方式一:

  1. 停数据库服务
  2. 修改mysql.cnf文件,添加属性skip-grant-tables
  3. 启动数据库,root免密登陆
  4. 此时,alter user 'root'@'localhost' IDENTIFIED BY 'xxxxx' ;,更新root用户密码
  5. 停mysql服务
  6. 修改mysql.cnf文件,去掉属性skip-grant-tables
  7. 启动mysql服务。

root密码修改完成。

方式二:

2.1 也可以像我这样,普通用户登录进去后

SELECT Host, User,authentication_string FROM mysql.user ;
+-----------+---------------+-------------------------------------------+
| Host      | User          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *1B3C2E06495E4EA09B0212594330B69A3FF6330B |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | user1         | *BE707F4785F1FD02DCF2BF85AED55095BE461D94 |
| %         | user2         | *BE707F4785F1FD02DCF2BF85AED55095BE461D94 |
+-----------+---------------+-------------------------------------------+

2.2 执行如下命令,将已知的user1的加密密文更新到root中

# UPDATE mysql.user SET authentication_string='*BE707F4785F1FD02DCF2BF85AED55095BE461D94' WHERE User='root';
# FLUSH PRIVILEGES;

2.3 查询数据库

mysql> SELECT Host, User,authentication_string FROM mysql.user ;
+-----------+---------------+-------------------------------------------+
| Host      | User          | authentication_string                     |
+-----------+---------------+-------------------------------------------+
| localhost | root          | *BE707F4785F1FD02DCF2BF85AED55095BE461D94 |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| %         | user1         | *BE707F4785F1FD02DCF2BF85AED55095BE461D94 |
| %         | user2         | *BE707F4785F1FD02DCF2BF85AED55095BE461D94 |
+-----------+---------------+-------------------------------------------+

2.4 用root用户登录

2.5 登录正常,但这会root登录进去后,无法执行具体的sql,只要有操作,会报如下的错:

mysql> SELECT Host, User,authentication_string FROM mysql.user ;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> SELECT *  FROM mysql.user ;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

2.6 需要重新ALTRE下用户信息

mysql> shwo databases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'shwo databases' at line 1
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' IDENTIFIED BY '嘻嘻嘻嘻嘻嘻嘻嘻' ;
Query OK, 0 rows affected (0.03 sec)mysql> commit;
Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

2.7 退不退出没关系,在重新执行sql命令时,已显示正常

# mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 156907
Server version: 5.7.24 MySQL Community Server (GPL)Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| finance            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

http://www.lryc.cn/news/136018.html

相关文章:

  • 网络安全(黑客)快速入门~
  • 华为OD机试 - 数字颠倒(Java 2023 B卷 100分)
  • leetcode做题笔记87扰乱字符串
  • 第一章 初识Linux(含VMware安装Ubuntu、CentOS、Windows、FinalShell、快照)
  • MATLAB算法实战应用案例精讲-【图像处理】OCR识别方法-CRNN
  • 无涯教程-PHP - preg_grep()函数
  • 【Linux】Nginx解决跨域问题
  • 无涯教程-PHP - preg_split()函数
  • B. Spreadsheets
  • matlab面向对象
  • 01、Cannot resolve MVC View ‘xxxxx前端页面‘
  • 时空智友企业流程化管控系统文件上传漏洞复现
  • 【已解决】Authenticator:无法添加账户请验证激活代码是否正确以及您的设备是否已为此应用启用推送通知
  • 聊聊springboot tomcat的maxHttpFormPostSize
  • java并发:synchronized锁详解
  • Unity 之NavMeshAgent 组件(导航和路径寻找的组件)
  • 装箱和拆箱
  • 等保测评--安全通信网络--测评方法
  • 统计学补充概念11-tsne
  • Linux_11_系统启动和内核管理
  • 【从零学习python 】65. Python正则表达式修饰符及其应用详解
  • QA2
  • centos7卸载docker
  • 【计算机视觉】递归神经网络在图像超分的应用Deep Recursive Residual Network for Image Super Resolution
  • Centos 7 安装系列(8):openGauss 3.0.0
  • NOIP真题讲解 传球游戏 接水问题
  • 《论文阅读18》 SSD: Single Shot MultiBox Detector
  • NOIP2016普及组第四题 魔法阵
  • uniapp-滑块验证组件wo-slider
  • NPM 管理组织成员