Building a local webserver involves installing all your dependencies either manually or using a preset stack like LAMP or XAMPP. The problem with these stacks is that they’re meant to be used in development environments and while they make the whole process easier, you can’t quite use them in a production environment.
The reason being these stacks aren’t exactly great from a security perspective. For that, you’re going to have to install individual web server components and set them up on your own.
However, when setting up a database such as MySQL or MariaDB, you might run into the “Error 1698 28000 access denied for user root localhost” issue. In this article, we’re talking about this error, what causes it and how you can fix this.
Also read: Debian vs Ubuntu: Which Linux distro should you pick?
What causes this error?
Usually, when you install a database management system otherwise known as DBMS such as MySQL or MariaDB, you’re asked to set a root password. This password is then used to log in to the database in order to make changes or review entries.
However, at times you might not be asked for a password in which case you’ll run into this issue. This is caused because, on some systems such as Ubuntu, MySQL uses the UNIX auth_socket plugin by default.
What this means is that any database user trying to access the database will be authenticated by the system user credentials.
Also read: RM command in Linux explained with examples
How to resolve the situation?
There are two ways you can tackle this error.
- You can set the root user to use the native MySQL password.
- You can create a new database user with your system user credentials. This is the recommended way.
Setting the root user to use the native MySQL password
Follow these steps if you’re willing to follow through on the first option.
Step 1: Type the following command in the terminal to launch MySQL as root.
sudo mysql -u root
Step 2: Once the MySQL console opens up, type the following commands one after the other pressing enter after each command.
USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE user='root';
FLUSH PRIVIILEGES;
exit:
Step 3: Now restart the MySQL service using this command.
sudo service mysql restart
Try accessing your database again it should work just fine.
Creating a new database user
In the commands below, replace your_user with your username.
Step 1: Type the following command in the terminal to launch MySQL as root.
sudo mysql -u root
Step 2: Once the MySQL console opens up, type the following commands one after the other pressing enter after each command.
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'password_here';
GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'localhost';
UPDATE user SET plugin='auth_socket' WHERE User='your_user';
FLUSH PRIVILEGES;
exit;
Step 3: Now restart the MySQL service using this command.
sudo service mysql restart
Try accessing your database again it should work just fine.
Also read: How to add a user to a group in Linux?
You can solve a problem to access root accessing as root.