ERROR 1045 : Fix MySQL Access Denied for Root User

|
Facebook
ERROR 1045 : Fix MySQL Access Denied for Root User

Introduction

In this guide, I will cover the steps to fix MySQL Access Denied for Root User (ERROR 1045), so that you can log in to your MySQL root account again without any hassle.

Seeing ERROR 1045: Access denied for user 'root'@'localhost' (using password: YES) while logging in to MySQL is quite common. This usually occurs due to incorrect credentials or permission issues.

MySQL Access Denied for Root User

I faced an access issue when I tried to log in to one of my MySQL databases to perform the health check.

ERROR 1045 : Fix MySQL Access Denied for Root User

Solution to resolve access denied for user root localhost

Command : - ps -ef | grep mysqld

Output

Check Server status

In the case of the Binary Method

Command : - sudo systemctl stop mysql@MYS00999

In the case of the RPM  (repository or without repository) based method.

Command : - sudo systemctl stop mysqld

Output

sudo systemctl stop mysql@service.name

Step 3) Add MySQL User entry in the configuration file

Add the user entry if not present in the configuration file.

Add MySQL User entry in the configuration file
vi /tmp/MYS00999_root_pwd.txt

alter user 'root@localhost' identified by 'Admin123#';

If you are using the binary method.

/local/mysql/8.0.36/mysqlhome/bin/mysqld --defaults-file=/local/mysql/MYS00999/data/MYS00999.cnf --init-file= /tmp/MYS00999_root_pwd.txt &

If you are using the RPM (repository or without repository) method.

/local/mysql/8.0.36/mysqlhome/bin/mysqld --init-file=/tmp/MYS00999_root_pwd.txt &

Note: – There are chances you will face the datadir not found issue during execution of the initialize command. In that case, you can use the datadir path as well and execute it.

Error :

2026-03-24T07:07:07.560562Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to '/local/mysql/8.0.36/mysqlhome/data/' (OS errno: 2 - No such file or directory)
2026-03-24T07:07:07.560615Z 0 [ERROR] [MY-010119] [Server] Aborting

Correct command to use :

/local/mysql/8.0.36/mysqlhome/bin/mysqld --defaults-file=/local/mysql/MYS00999/data/MYS00999.cnf --datadir=/local/mysql/MYS00999/data --init-file=/tmp/MYS00999_root_pwd.txt &

Output

Start the server to initialize with above created file

Check the MySQL process status

ps -ef | grep mysqld

Output

Check the MySQL process status

In case you are unable to log in even after proper initialization and getting the below issue, then you need to follow the steps below to log in properly.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket

a) First, you need to check the mysqld server process using the command below.

ps -ef | grep mysqld

Output

[MYS00999@ind003 /local/mysql/MYS00999/admin/log]$ ps -ef | grep mysqld
mysql     4323  3797  0 12:51 pts/2    00:00:07 /local/mysql/8.0.36/mysqlhome/bin/mysqld --defaults-file=/local/mysql/MYS00999/data/MYS00999.cnf --datadir=/local/mysql/MYS00999/data --init-file=/tmp/MYS00999_root_pwd.txt

b) next you need to check if the socket file is present using the command below

 ls -l /local/mysql/MYS00999/admin/subsys/MYS00999.sock

Output

[MYS00999@ind003 /local/mysql/MYS00999/admin/log]$ ls -l /local/mysql/MYS00999/admin/subsys/MYS00999.sock
ls: cannot access /local/mysql/MYS00999/admin/subsys/MYS00999.sock: No such file or directory

As the Socket file is not created, due to which you are unable to log in, a new password has been set by using the initialize command, whereas the server has not started because in the error log, it shows it has shut down after resetting the password.

Output

Server has been shut down

c) Next, you need to kill the existing MySQL process, which we saw in step a, and again need to start using the systemctl command.

kill -9 4323

d) Start the MySQL server again using the systemctl command and also check the socket file status.

Output

Start the MySQL server again using the systemctl command

e) Now, try to take the connection using the command below.

mysql -u root -p

Output

mysql -u root -p

Now, reset the password to fix the MySQL Access Denied for Root User using the command below.

alter user root@localhost identified by 'root';

Output

Reset the root user password

Conclusion

By following this approach, you can easily fix the MySQL Access Denied for Root User.

If you enjoyed the article, please leave a comment and share it with your friends. Also, let me know which Oracle and MySQL topics you'd like to see covered in future articles.

Note: – If you want to practice this whole activity in your home lab, then you'll need a platform to perform the installation. To set that up, you first need to download and install Oracle VirtualBox, followed by the operating system, the MySQL binary software, and finally, MySQL server creation.

1. Mysql Installation using Repository method (RPM-based)

2. Mysql Installation without using the repository method (RPM-based)

3. Storage engines in MySQL

4. Start the MySQL server as a service.

5. Example of Storage Engine in MySQL 8

DBAStack

I’m a database professional with more than 10 years of experience working with Oracle, MySQL, and other relational technologies. I’ve spent my career building, optimizing, and maintaining databases that power real-world applications. I started DBAStack to share what I’ve learned — practical tips, troubleshooting insights, and deep-dive tutorials — to help others navigate the ever-evolving world of databases with confidence.

Keep Reading

Installation of MySQL Server in Linux
|
by DBAStack
|
by DBAStack

Leave a Comment