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

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

How to implement Mysql switching data storage directory

How to implement Mysql switching data storage dir...

JS implements a simple counter

Use HTML CSS and JavaScript to implement a simple...

XHTML introductory tutorial: Application of table tags

<br />Table is an awkward tag in XHTML, so y...

Native JS to achieve cool paging effect

This article uses an example to share with you a ...

Tips for making web table frames

<br />Tips for making web table frames. ----...

PyTorch development environment installation tutorial under Windows

Anaconda Installation Anaconda is a software pack...

How to deeply understand React's ref attribute

Table of contents Overview 1. Creation of Refs ob...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...

Python3.6-MySql insert file path, the solution to lose the backslash

As shown below: As shown above, just replace it. ...

Install OpenSSL on Windows and use OpenSSL to generate public and private keys

1. OpenSSL official website Official download add...

Why Google and Facebook don't use Docker

The reason for writing this article is that I wan...