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


I am an electrical engineer who mainly plays around with power systems instead of programming. Recently, I have been following a manual to install a software suite on Ubuntu. I have no knowledge of mySQL at all, actually. I have done the following installations on my Ubuntu.

sudo apt-get update
sudo apt-get install mysql-server-5.5
sudo apt-get install mysql-client-5.5
sudo apt-get install mysql-common
sudo apt-get install glade
sudo apt-get install ntp

Then I do

[email protected]:~/Desktop/iPDC-v1.3.1/DBServer-1.1$ mysql -uroot -proot <"Db.sql"

I ended up with the following error message.

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

How may I fix it and continue?

5/14/2019 11:24:45 PM

Accepted Answer

Note: For MySQL 5.7+ please see answer from @Lahiru to this question. That contains more current information.

For MySQL < 5.7:

The default root password is blank (i.e. empty string) not root. So you can just login as:

mysql -u root

You should obviously change your root password after installation

mysqladmin -u root password [newpassword]

In most cases you should also set up individual user accounts before working extensively with the DB as well.

4/16/2018 9:54:40 PM

You have to reset the password! steps for mac osx(tested and working) and ubuntu

Stop MySQL using

sudo service mysql stop


$ sudo /usr/local/mysql/support-files/mysql.server stop

Start it in safe mode:

$ sudo mysqld_safe --skip-grant-tables --skip-networking

(above line is the whole command)

This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:

$ mysql -u root

mysql> UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';

As per @IberoMedia's comment, for newer versions of MySQL, the field is called authentication_string:

mysql> UPDATE mysql.user SET authentication_string =PASSWORD('password') WHERE User='root';

Start MySQL using:

sudo service mysql start


sudo /usr/local/mysql/support-files/mysql.server start

your new password is 'password'.


I know this an old Question but i feel this might help someone. I was recently faced with the same problem but in my case, i remember my password quite alright but it kept on giving me the same error. I tried so many solutions but still none helped then i tried this

mysql -u root -p 

after which it asks you for a pass word like this

Enter password: 

and then i typed in the password i used. That's all


At the initial start up of the server the following happens, given that the data directory of the server is empty:

  • The server is initialized.
  • SSL certificate and key files are generated in the data directory.
  • The validate_password plugin is installed and enabled.
  • The superuser account 'root'@'localhost' is created. The password for the superuser is set and stored in the error log file.

To reveal it, use the following command:

shell> sudo grep 'temporary password' /var/log/mysqld.log

Change the root password as soon as possible by logging in with the generated temporary password and set a custom password for the superuser account:

shell> mysql -u root -p #Login to root user with some password

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass5!'; 

if the problem still exists try to force changing the pass

/etc/init.d/mysql stop

mysqld_safe --skip-grant-tables &

mysql -u root

Setup new MySQL root user password

use mysql;
update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
flush privileges;

Stop MySQL Server:

/etc/init.d/mysql stop

Start MySQL server and test it:

mysql -u root -p

It happens when your password is missing.

Steps to change password when you have forgotten:

  1. Stop MySQL Server (on Linux):

    sudo systemctl stop mysql
  2. Start the database without loading the grant tables or enabling networking:

    sudo mysqld_safe --skip-grant-tables --skip-networking &

    The ampersand at the end of this command will make this process run in the
    background so you can continue to use your terminal and run #mysql -u root, it will not ask for password.

    If you get error like as below:

    2018-02-12T08:57:39.826071Z mysqld_safe Directory '/var/run/mysqld' for UNIX
    socket file don't exists. mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket
    '/var/run/mysqld/mysqld.sock' (2) [1]+ Exit 1

  3. Make MySQL service directory.

    sudo mkdir /var/run/mysqld

    Give MySQL user permission to write to the service directory.

    sudo chown mysql: /var/run/mysqld
  4. Run the same command in step 2 to run mysql in background.

  5. Run mysql -u root you will get mysql console without entering password.

    Run these commands


    For MySQL 5.7.6 and newer

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

    For MySQL 5.7.5 and older

    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

    If the ALTER USER command doesn't work use:

    UPDATE mysql.user SET authentication_string = PASSWORD('new_password')     WHERE User = 'root' AND Host = 'localhost';

Now exit

  1. To stop instance started manually

    sudo kill `cat /var/run/mysqld/`
  2. Restart mysql

    sudo systemctl start mysql