MySQL Access denied for user 'root' while using LAMP

24,485

Solution 1

Here's what worked for me:

Just append "sudo" to the beginning of that mysql command:

sudo mysql -uroot -p

Please note the "sudo" at the beginning of that command

Solution 2

  1. Set a password for mysql:

    sudo dpkg-reconfigure mysql-server-5.x
    
  2. Now open the terminal and type:

    mysql -uroot -p
    

Give password and press Enter.

Solution 3

I think it is important to note on this older post that MySQL has changed some things in the past 5 years. Digital Ocean states the following in their tutorial called How To Install MySQL on Ubuntu 18.04:

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password.

Therefore, run the following to install properly and securely:

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation

I personally answered y for every prompt from the mysql_secure_installation and created a strong password (option 2, if you set up the VALIDATE PASSWORD plugin). However, the particular choices here are your's to make. Either way, you will set a password under one of these prompts. Although, as you will see, you won't necessarily need the password.

After the prompts for mysql_secure_installation are complete. All you have to do is type the following to log in:

sudo mysql

This is because the native authentication method for MySQL 5.7 and later is auth_socket instead of mysql_native_password for better security. No need for a password, because you can only log in from the machine it's installed on.

If you need to use a password to log in from another source, or want to know more, I recommend reading How To Install MySQL on Ubuntu 18.04 in its entirety.

Share:
24,485
abdulsaboor
Author by

abdulsaboor

Updated on September 18, 2022

Comments

  • abdulsaboor
    abdulsaboor over 1 year

    I'm just want to install lamp server + phpmyadmin normally in case of when setting up mysql i get this error:

    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    

    I don't know what this is.
    I don't know how to run php code in ubuntu because it's my first deal with linux.

  • felipe.zkn
    felipe.zkn about 9 years
    It is important to remember that the password must be after -p, without any space.
  • Tachyons
    Tachyons about 9 years
    @felipe.zkn it is better to press enter after -p , so that password won't be displayed in the screen
  • felipe.zkn
    felipe.zkn about 9 years
    Thanks for the warning! As I am on a secure place, I always use the direct form.
  • pa4080
    pa4080 over 7 years
    You don't need sudo, because you just connecting to a server (MySQL in this case) from some host (localhost in this case), and making an authentication via user (MySQL's root in this case) and its password. More important thing here is the -p argument.
  • pelican
    pelican over 7 years
    Believe me, I just tried that on my linuxMint and it worked; I'm trying to install libreNMS and following instructions here: docs.librenms.org/Installation/Installation-Ubuntu-1604-Apac‌​he
  • pa4080
    pa4080 over 7 years
    If the username and the password (when you enter it through command line) consists of special characters, you can enclose them in single quote marks: mysql -u'ex@mp!e' -p'p@$$wd'.
  • SaintWacko
    SaintWacko over 7 years
    This worked for me, too. Not sure why.