Tutorial – After Installing MySQL on Ubutnu 18.04, Fix “Access denied for user ‘root’@’localhost'” Error

Recently, I came across an issue when installing mysql-server on Ubuntu 18.04. What I found is that after I installed mysql-server using my standard approach of sudo apt install mysql-server, and after running mysql_secure_installation, my root user was denied access to MySql when trying to access with mysql -u root -p. I would always get the following error:

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

Fixing Access denied for user ‘root’@’localhost’ Ubuntu

After hours of trying to come up with the right solution, this is what I did to fix:

  1. Stop MySQL Server from running:

    sudo service mysql stop
  2. We need to create a directory for MySQL to store access socket files so that we may use the mysqld command. Without this directory, the command will fail.

    sudo mkdir -p /var/run/mysqld
    sudo chown mysql:mysql /var/run/mysqld
  3. Manually start MySQL using the following command:

    sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
  4. The command we just ran will allow us to now access MySQL without having a password. For what ever reason, when MySQL was installed, the password we installed it with did not get applied to to the root user. Now you can manually change your password using the following `mysql` commands

    Log in to MySql without a password:

    mysql -u root
  5. Flush Privileges to apply the pending privileges to the root user created during the installation process:

  6. Update the password of the root user using the three following commands:

    mysql> USE mysql;
    mysql> UPDATE user SET authentication_string=PASSWORD("{PASSWORD_HERE}") WHERE User='root';
    mysql> UPDATE user SET plugin="mysql_native_password" WHERE User='root';
  7. End you MySQL session:

    mysql> quit
  8. Now that you’ve successfully reset your password, you will want to terminate mysqld:

    sudo pkill mysqld
  9. Restart your MySQL Server:

    sudo service mysql start


I’m not sure the exact reason why MySQL isn’t properly setting the root password and privileges when prompted for it while installing mysql-server. But I do know this was a pain for me to fix. If you run into this issue and found this tutorial helpful, please come back frequently to check for new tutorials.

Author: Joshua Johnson

My name is Joshua Johnson and I am the founder of UA1 Labs. I am passionate about helping other developers to become successful in their paths. When I started developing software, I didn't have many resources to help me out. UA1 Labs is my way of giving back to all those who helped me.

4 thoughts on “Tutorial – After Installing MySQL on Ubutnu 18.04, Fix “Access denied for user ‘root’@’localhost'” Error”

  1. Thanks, ive struggled five hours in this issue. Your tutorial was the onliest website to fix it. All other hints werent helpful at all.

  2. Thank you ever so much. As others have said, yours has been the ONLY one to actually address the issue and walk me through the solution.
    BIg kudos to you.

Leave a Reply

Your email address will not be published. Required fields are marked *