This solves the problem! Change your forgotten MySQL root user password

Hello!
I'm Inoue, a Persian cat from Beyond Co., Ltd.'s Shikoku office.

One day, I was trying to log into MySQL on my local computer, but I couldn't. I entered my password and got an error message


This blog was born out of trial and error, as I was trying to figure out how to change my password to a new one even if I forgot it

In this case, the steps I took were as follows:

① Check the initial password
② Confirm that the password has been changed ③
Log in to MySQL and set a new password

Please watch until the end!

Where is the initial password?

To check the initial password, you need to check "mysqld.log" under /var/log.
The temporary password has been output to "mysqld.log" since version 5.7.
*Currently, versions up to 8.0.33 are available. For more information, click here

To check the MySQL client version, enter the following command:

[root@test-aws-harukainoue ~]# mysql --version mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper

I found that my local MySQL version is 5.7 or higher!

[root@test-aws-harukainoue log]# pwd /var/log [root@test-aws-harukainoue log]# less mysqld.log | grep "temporary password" 2019-04-08T02:32:37.248018Z 1 [Note] A temporary password is generated for root@localhost: x=B-:.9iaqc?

It's difficult to find the initial password when looking at it in less, so we
'll use "grep" to extract strings containing "temporary password."
From the execution results, we can see that the initial password for MySQL is the string "x=B-:.9iaqc?".
Now, let's try accessing MySQL!
The command to access MySQL is as follows:

mysql -u root -p

When I ran it, I was prompted to enter a password, so I entered "x=B-:.9iaqc?"

Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Huh??? This isn't supposed to happen...
No matter how many times I try, an error message appears and I can't log in to MySQL.
This means I can't change my MySQL password to a new one...
I thought maybe I had changed my MySQL password before but couldn't remember it, so
I tried a different method!

This solves the problem! Set a new password

Open "my.cnf" in vi and edit it

[root@test-aws-harukainoue etc]# vi /etc/my.cnf

Enter "skip-grant-tables" under [mysqld] and save the file.
, please refer to
the previous blog post [For Linux Beginners] "Done! Editing and Saving Files with the vi Command " Restart MySQL.

[root@test-aws-harukainoue etc]# systemctl restart mysqld

Enter MySQL

[root@test-aws-harukainoue etc]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 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>

Now that you can log in to MySQL, run the following to reflect the permissions and set a new password

mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Newpassword'; Query OK, 0 rows affected (0.00 sec)

Now that you have changed the password to a new one, exit MySQL

mysql> quit

Open "my.cnf" again in vi and edit it.
This time, delete the "skip-grant-tables" that you just added and save the file.
Also, restart MySQL.

[root@test-aws-harukainoue etc]# systemctl restart mysqld

Now let's log in to MySQL with the new password!

[root@test-aws-harukainoue etc]# mysql -u root -p Enter password:Newpassword Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.25 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>

Success! I was able to log into MySQL with my new password!

By the way, here's how to check the MySQL version after entering MySQL

mysql> select version(); +------------+ | version() | +------------+ | 5.7.25 | +------------+ 1 row in set (0.00 sec)

By the way, here is how to check the MySQL Server version.
To check the MySQL Server version, you will need your MySQL login password.

[root@test-aws-harukainoue ~]# mysqladmin -u root -p version Enter password:Newpassword mysqladmin Ver 8.42 Distrib 5.7.25, for Linux on x86_64 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. Server version 5.7.25 Protocol version 10 Connection Localhost via UNIX socket UNIX socket /var/lib/mysql/mysql.sock Uptime: 21 min 2 sec Threads: 1 Questions: 2 Slow queries: 0 Opens: 105 Flush tables: 1 Open tables: 98 Queries per second avg: 0.001

Finally

the reason I couldn't access MySQL with the initial password
is because I had previously changed the password and forgotten about it.
If you're testing in your local environment and you've forgotten your MySQL password, try this out. I
really don't understand MySQL...
I'd like to continue challenging myself without feeling like I'm not good at it!

Growing every day, progressing every day.
I have to update myself every day!!!
Thank you for reading to the end.

If you found this article useful, please click [Like]!
13
Loading...
13 votes, average: 1.00 / 113
77,858
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Ayaka Inoue

I'm a member of the System Solutions Department.
I joined Beyond as one of the founding members of the Shikoku office. I jumped into the IT industry with no experience.
As part of the education team, I create curriculum and conduct training for new graduates, mid-career hires, and existing members.
My main job is server operation and maintenance.
Above all, I care about our customers' content.
I'm also a member of the Web Content Division and YouTube team.