Forgot MySQL login password solution in centos

I forgot my password to log in to mysql

# 显示的输入密码, 查看自己的报错了
mysql -u root -p123456
Insert picture description here


The first time you install and use mysql, you can use MySQL's temporary password to log in to the system

# 基础linux 命令
# 查看文件内容
# | 管道符, 前者的输出作为后者的输入
# grep 包含或者说是 查找 相应 字段的行
cat /var/log/mysqld.log |grep password
Insert picture description here


Everyone is different.
When we change it for the first time, we will use our own password. Log in to the system for the first time to force a password, otherwise we can’t do anything.

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

mysql> update mysql.user set authentication_string=password('[email protected]') where user='root' ;

The password requires numbers, letters, symbols, and the length is more than eight digits,
so when you don’t pay attention and set it casually, it will be fun

solution

Enter mysql configuration file, skip password verification

vi /etc/my.cnf

Added skip-grant-tables under the [mysqld] configuration section

skip-grant-tables
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

skip-grant-tables

# Disabling symbolic-links is recommended to prevent assorted security risks
"/etc/my.cnf" 29L, 979C written

It's that simple, just skip the test

After saving the configuration file, restart the mysql service

systemctl restart mysqld.service

Use the mysql login command to enter directly and enter

mysql -u root -p
# 直接回车就好

Then re-modify the mysql login password

mysql>update mysql.user set authentication_string=password('[email protected]') where user='root' ;
mysql>flush privileges ;
mysql>quit

Edit the mysql configuration file again to
remove the skip-grant-tables that was added before.

Restart the mysql service.

systemctl restart mysqld.service

Reference document = https://www.cnblogs.com/yanziwen/p/9215264.html