MySQL数据库忘记root账号密码解决方法

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: mysql5.7密码忘记解决方法1.环境变量ENV:

mysql5.7密码忘记解决方法

1.环境变量

ENV:

[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@localhost ~]# rpm -qa mysql
[root@localhost ~]# 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

2.错误描述

登陆时出错:

[root@localhost ~]# 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的密码,但这种方法存在安全隐患,毕竟密码在命令行上显示了,不建议但不反对。

如果是忘记,修改如下:

3.处理方法

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

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

3.2.空密码直接进入mysql;

[root@localhost ~]# mysql
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> 

3.3.修改密码

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)

3.4改回/etc/my.cnf

注释掉 #skip-grant-tables

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

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

[root@localhost ~]# 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>

3.6.更改root密码

更改root密码: alter user ‘root’@‘localhost’ identified by ‘密码’;

修改用户密码;

ALTER USER testuser 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)

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

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
目录
相关文章
|
6月前
|
Ubuntu 关系型数据库 MySQL
mysql8.0安装初始化不需要设定root密码?
uubntu 的mysql安装完成后无法设定root密码
375 10
|
NoSQL 关系型数据库 MySQL
2024Mysql And Redis基础与进阶操作系列(4-2)作者——LJS[含MySQL非空、唯一性、PRIMARY KEY、自增列/自增约束举例说明等详解步骤及常见报错问题对应的解决方法]
24MySQL非空、唯一性、PRIMARY KEY、自增列/自增约束举例说明等详解步骤及常见报错问题对应的解决方法(4-2) 学不会你来砍我!!!
|
8月前
|
SQL 关系型数据库 MySQL
【YashanDB 知识库】YashanDB 支持 MySQL 多表更新语句的解决方法
【YashanDB 知识库】YashanDB 支持 MySQL 多表更新语句的解决方法
|
8月前
|
SQL 关系型数据库 MySQL
【YashanDB知识库】YashanDB 支持MySQL多表更新语句的解决方法
本文介绍了在YashanDB中处理MySQL多表更新语句的方法。由于YashanDB默认不支持多表更新(报错YAS-04344),可通过启用兼容性参数`SQL_PLUGIN = 'MYSQL'`解决,或改写SQL语句实现相同功能。文章通过具体示例说明了多种改写方法,包括根据共同列更新单个或多个字段、添加过滤条件以及基于多个共同列的更新场景。涉及的表结构和数据展示了实际操作过程,帮助用户顺利迁移和使用YashanDB。
|
NoSQL 安全 关系型数据库
2024Mysql And Redis基础与进阶操作系列(6)作者——LJS[含MySQL 多表之一对一/多;多对多;多表联合查询等详解步骤及常见报错问题所对应的解决方法]
MySQL 多表之一对一/多;多对多;多表联合之交叉连接;内连接;左、右、外、满、连接;子查询及关键字;自连接查询等详解步骤及常见报错问题所对应的解决方法
|
SQL NoSQL 关系型数据库
2024Mysql And Redis基础与进阶操作系列(5)作者——LJS[含MySQL DQL基本查询:select;简单、排序、分组、聚合、分组、分页等详解步骤及常见报错问题所对应的解决方法]
MySQL DQL基本查询:select;简单、排序、分组、聚合、分组、分页、INSERT INTO SELECT / FROM查询结合精例等详解步骤及常见报错问题所对应的解决方法
|
SQL NoSQL 关系型数据库
|
存储 SQL 关系型数据库
2024Mysql And Redis基础与进阶操作系列(1)作者——LJS[含MySQL的下载、安装、配置详解步骤及报错对应解决方法]
Mysql And Redis基础与进阶操作系列(1)之[MySQL的下载、安装、配置详解步骤及报错对应解决方法]
|
存储 SQL NoSQL

热门文章

最新文章

推荐镜像

更多