MySQL
Error 1698
Access Denied
Root User
Database Connection

ERROR 1698 28000 Access denied for user 'root''localhost'

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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

When working with MySQL, one might encounter several errors related to user authentication and privileges. One such common error is ERROR 1698 (28000): Access denied for user 'root'@'localhost'. This error is particularly prevalent in MySQL installations that use Unix socket authentication, especially on more secure installations that follow modern security practices. In this article, we will explore the reasons behind this error, provide technical explanations, and suggest solutions with practical examples.

Causes of ERROR 1698 (28000)

Unix Socket Authentication

One primary cause for this error is the use of Unix socket authentication. Unix socket authentication allows MySQL to authenticate users based on the Unix credentials of the client process connecting to it. This means that the MySQL server can be configured to permit a Linux user to connect as a specific MySQL user without needing a password, as long as the connection is made via the localhost.

Security Practices

Modern security configurations encourage more secure authentication methods. In many MySQL installations, particularly on Linux distributions like Ubuntu, MySQL is configured to use auth_socket plugin for the root user by default instead of traditional password authentication. This means that only the Linux user root can access the MySQL root account when connected from localhost, without specifying a password.

How to Verify the Plugin in Use

To understand which authentication method is in use for a MySQL user, you can run the following query:

sql
SELECT user, host, plugin FROM mysql.user WHERE user = 'root';

This will return the authentication plugins associated with the root user. If you see auth_socket listed, then Unix socket authentication is indeed in use.

Solutions to ERROR 1698 (28000)

There are several solutions one can pursue to resolve this error, depending on the desired security profile:

Solution 1: Use sudo to Access MySQL as Root

The simplest way to interact with MySQL when Unix socket authentication is enabled for the root user is to access MySQL using sudo:

bash
sudo mysql -u root

This command elevates your privileges to the root user and leverages socket authentication to gain access.

Solution 2: Create a New MySQL Administrative User

Instead of modifying the root user's configuration and potentially reducing the security of your MySQL instance, you can create a new MySQL user with administrative privileges:

sql
-- Log into MySQL using `sudo mysql -u root`
CREATE USER 'admin_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'admin_user'@'localhost' WITH GRANT OPTION;

Solution 3: Change Authentication Method

If socket authentication is not needed, you can change the root user's authentication plugin back to mysql_native_password:

sql
-- Log into MySQL using `sudo mysql -u root`
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';
FLUSH PRIVILEGES;

Note: Changing the authentication plugin may impact the security of your MySQL instance. Always evaluate potential security risks and adhere to best practices.

Key Points Summary

IssueCauseSolution
ERROR 1698 (28000)Unix socket authentication for root user on localhostUse sudo mysql -u root
Security ConfigurationMySQL configured with auth_socket plugin for rootCreate new MySQL administrative user
Authentication MethodUse of auth_socket instead of password-based authenticationChange auth plugin to mysql_native_password

Conclusion

The ERROR 1698 (28000): Access denied for user 'root'@'localhost' is not a bug or an error in the MySQL software itself; rather, it's a reflection of intentional security measures implemented in certain MySQL configurations. By understanding the underlying causes of this error and the rationale behind Unix socket authentication, administrators can effectively manage MySQL access while maintaining a desired level of security. Always remember that security should be a priority, and potential changes to user authentication should be reviewed with security implications in mind.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.