Never worry about what to do if you forget your MySQL password-don't be afraid, this article is enough!

Reason: The MySQL on my server has not been used for a long time, and now I suddenly need it, but I forgot the password, so I write a solution for myself and you all for reference!

Solution

1. Check whether the mysql service is started, if it is started, close the mysql service

[root@localhost ~]# systemctl stop mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Sun 2020-10-11 07:59:51 EDT; 1s ago

2. Modify the mysql configuration file my.conf

[root@localhost ~]# vi /etc/my.cnf
## 在 [mysqld] 下添加最后一行代码
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
skip-grant-tables    ## 添加此行代码即可!
 == > > wq 保存退出

3. Start the database

[root@localhost ~]# systemctl start mysqld

4. Enter MySQL database

[root@localhost ~]# mysql -u root -p
Enter password:    ## 不需要输入密码,直接回车!!!

mysql> use mysql;
mysql> update mysql.user set authentication_string=password('password') where user='root';
## password替换成你想要的密码
mysql> exit;   
改完之后退出MySQL即可

5. Delete the previously added configuration file

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
skip-grant-tables    ## 现在删掉此行代码即可!
== >> wq 保存退出

[root@localhost ~]# systemctl restart mysqld  ## 在重启服务器

At this point, the change is complete, and then you only need to log in to MySQL normally.
Note that after changing the configuration file, you need to restart MySQL for the configuration file to take effect!

Guess you like

Origin blog.csdn.net/m0_46563938/article/details/109016100