ERROR 1698 (28000): Access denied for user "root"@"localhost"

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for ERROR 1698 (28000): Access denied for user "root"@"localhost"

MySQL Error 1698: Access Denied for User 'root'@'localhost'

So you're trying to log into your MySQL database using the root user, but you keep getting hit with the dreaded ERROR 1698 (28000): Access denied for user 'root'@'localhost' error. Frustrating, isn't it? Don't worry, you're not alone. This is a common issue that many server administrators and developers face. But fear not, because I'm here to help you troubleshoot and resolve this problem.

Understanding the Error

Before we dive into the solutions, let's quickly understand what this error message means. Essentially, it's telling you that the user 'root' is being denied access to the MySQL database from the localhost (your own machine). There could be a few reasons for this, such as incorrect credentials or insufficient privileges assigned to the root user.

Solution 1: Resetting the Root Password

One possible cause of this error is an incorrect or forgotten password for the root user. To fix this, you can try resetting the root password. Here's how:

  1. Stop the MySQL service:

sudo service mysql stop
  1. Start the MySQL service in safe mode with the --skip-grant-tables option:

sudo mysqld_safe --skip-grant-tables
  1. Open a new terminal window and log into the MySQL database as the root user:

mysql -u root
  1. Once logged in, execute the following command to change the root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

Replace 'new_password' with your desired password.

  1. Flush the privileges to apply the changes:

FLUSH PRIVILEGES;
  1. Exit the MySQL prompt and stop the MySQL service in safe mode:

exit
sudo service mysql stop
  1. Finally, start the MySQL service normally:

sudo service mysql start

Now, try logging in with the root user using the new password. Hopefully, this resolves the access denied error for you.

Solution 2: Granting Sufficient Privileges

Another reason for the access denied error could be that the root user doesn't have the necessary privileges to access the database. To address this, we can grant the appropriate privileges to the root user. Here's how:

  1. Log into the MySQL database as a user with administrative privileges:

mysql -u admin_username -p
  1. Enter your password when prompted.

  2. Once logged in, execute the following command to grant all privileges to the root user:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION;
  1. Flush the privileges to apply the changes:

FLUSH PRIVILEGES;
  1. Exit the MySQL prompt:

EXIT;

Now, try logging in with the root user again. Hopefully, this time you will be granted access without any issues.

Solution 3: Checking MySQL Configuration

If the above solutions didn't work, there might be an issue with the MySQL configuration files. It's possible that the root user's permissions have been restricted or modified. To check and fix this:

  1. Open the MySQL configuration file, usually located at /etc/mysql/mysql.conf.d/mysqld.cnf:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
  1. Look for a section called [mysqld] and make sure it includes the following line:

skip-grant-tables

This line allows you to bypass the authentication checks temporarily.

  1. Save the file and exit the editor.

  2. Restart the MySQL service for the changes to take effect:

sudo service mysql restart

Now, try logging in with the root user once again. If this fixed the issue, you can revert the changes made to the configuration file by removing the skip-grant-tables line and restarting the MySQL service.

Conclusion and Call-to-Action

Congratulations! You've made it through the troubleshooting process and solved the MySQL access denied error. 🎉 Now you can log into your MySQL database with the root user without any issues. Remember to use strong passwords and ensure that the root user has the necessary privileges to perform the desired tasks.

If you found this guide helpful, don't be stingy! Share it with your fellow developers, sysadmins, or anyone else struggling with the same issue. Let's spread the knowledge and help others out. 💪

Have you encountered any other MySQL errors or faced any technical challenges? Let me know in the comments below! I'm here to assist you. 🤝

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my