Detailed explanation of the solution to forget the password in MySQL 5.7

Detailed explanation of the solution to forget the password in MySQL 5.7

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

Error while logging in:

[root@centos7 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

I forgot my password (same as not knowing the password after installation)

Some people on the Internet say that the password of MySQL is an empty password. In fact, after MySQL version 5.7, the password is no longer an empty password.

If it is newly installed, you can find it in the mysql log file

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

Supplement: If you find the password provided by mysql, you can use

mysqladmin -u root -p 'The password provided by mysql' password 'Your new password'

Directly modify the mysql password, but this method has security risks. After all, the password is displayed on the command line. It is not recommended but not opposed.

If you forget, modify it as follows:

1. Modify /etc/my.cnf and add skip-grant-tables;

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

Add in the blank position, save and exit;

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

2. Enter mysql directly with an empty password;

[root@centos7 ~]# mysql -u root -p
Enter password: (This is an empty password, just press Enter)
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>

Enter the mysql database;

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>

The mysql here is not unchanged, the database location is changed;

3. Change password: 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 Change back to /etc/my.cnf

Comment out #skip-grant-tables

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

5. Enter mysql again with the new password;

[root@centos7 ~]# mysql -u root -p 
Enter password: (newpassword in previous demonstration)
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. Change the root password: alter user 'root'@'localhost' identified by 'password';

Change user password;

ALTER USER testuser IDENTIFIED BY '123456';

Modify the currently logged in user

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> 

It can be seen that there are great requirements for the complexity of passwords;

7. After the modification is completed, you can continue to operate mysql

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

The above is a detailed explanation of the solution to the forgotten password of MySQL 5.7 introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Solve the problem of forgetting password in MySQL 5.7 under Linux
  • Summary of how to modify the root password in MySQL 5.7 and MySQL 8.0
  • Tutorial on installing and changing the root password of MySQL 5.7.20 decompressed version
  • MySQL 5.7.21 installation and password configuration tutorial
  • How to install and modify the initial password of mysql5.7.18 under Centos7.3
  • How to install and modify the initial password of mysql5.7.18
  • How to change password in MySQL 5.7.18
  • How to change the root password of Mysql5.7.10 on MAC
  • What to do if you forget the root password of Mysql5.7 (simple and effective method)
  • Mysql5.7.14 installation and configuration method operation graphic tutorial (password problem solution)

<<:  How to use the dig/nslookup command to view DNS resolution steps

>>:  Basic concepts and common methods of Map mapping in ECMAScript6

Recommend

How to deploy Confluence and jira-software in Docker

version: centos==7.2 jdk==1.8 confluence==6.15.4 ...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

Sample code for seamless scrolling with flex layout

This article mainly introduces the sample code of...

CentOS 7 configuration Tomcat9+MySQL solution

Configure Tomcat First install Tomcat Installing ...

How to enter directory/folder in Linux without using CD command

As we all know, without the cd command, we cannot...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

Scoring rules of YSlow, a webpage scoring plugin developed by Yahoo

YSlow is a page scoring plug-in developed by Yaho...

How to completely uninstall mysql under CentOS

This article records the complete uninstallation ...

Nginx/Httpd reverse proxy tomcat configuration tutorial

In the previous blog, we learned about the usage ...

Use overflow: hidden to disable page scrollbars

Copy code The code is as follows: html { overflow...

Detailed explanation of MySQL database transaction isolation levels

Database transaction isolation level There are 4 ...

How to change the dot in the WeChat applet swiper-dot into a slider

Table of contents background Target Effect Ideas ...

VUE+Express+MongoDB front-end and back-end separation to realize a note wall

I plan to realize a series of sticky note walls. ...

WeChat applet calculator example

WeChat applet calculator example, for your refere...