I am an electrical engineering who mainly play around with power system instead of programming. Recently, I have been following a manual to install a software suite on Ubuntu. I have no knowledge on 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

me@ubuntu:~/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?

share|improve this question
1  
This does not seem to be strictly programming-related to me. I flagged it for migration to SuperUser – Uli Köhler Feb 21 '14 at 20:55

15 Answers 15

up vote 82 down vote accepted

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.

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

share|improve this answer
1  
@FarticlePilter The -p flag specifies the password, so after you change your root password you would do like mysql -u root -p[newpassword]. The < [filename] is using std input to execute an SQL file at the path given via the user credential you provide. – Mike Brant Feb 21 '14 at 21:00
7  
I tried as told mysqladmin -u root password abc1234, but I got mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: NO)'. Thank you so much! – Sibbs Gambling Feb 21 '14 at 23:18
2  
@Kanagaroo in this question, the OP states they just installed MySQL for the first time and then tried to access via MySQL command line client. That sounds like first time access to me. – Mike Brant Dec 13 '14 at 13:17
14  
Actually for mysql community server 5.7, the default root password is randomly generated when you install. Check your /var/log/mysqld.log for a line talking about a "temporary password". Saves hours of messing around. – Phil Jan 8 '16 at 10:04
1  
@BraianMellor This does solve the problem for MySQL < 5.7 in that the default (post-installation) root password on Ubuntu is blank (no password), where in the original question the poster was trying to use root/root. The question was not about lost password, changing password, or similar. For newer versions of MySQL the other answer I referenced is the correct one, as a user must look in the error logs to see the randomly-generated root password. – Mike Brant Jul 25 '17 at 19:36

I was able to solve this problem by executing this statement

sudo dpkg-reconfigure mysql-server-5.5

Which will change the root password.

share|improve this answer
1  
This is the magic sauce for users of .deb-based distros. – Jasen Dec 28 '14 at 20:16
1  
MAGIC!!!!!!!!!!!!!! – efwjames Sep 5 '15 at 21:57
4  
Just what I needed... but not everyone has version 5.5 of server. use dpkg --get-selections | grep sql to get your version – Balmipour Sep 10 '15 at 11:18
1  
@Balmipour what should be the equivalent command to check the sql version in Rhel 6.x – PrathamN Sep 12 '15 at 4:00
    
@Divz what should be the command to reconfigure mysql server in Rhel/centos – PrathamN Sep 12 '15 at 4:03

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

Stop MySQL using

sudo service mysql stop

or

$ 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';

Start MySQL using:

sudo service mysql start

or

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

your new password is 'password'.

share|improve this answer
1  
Please, NOT that way ! @Divz's answer is more secure. Quoting the doc about --skip-grant-tables : This enables anyone to connect without a password and with all privileges [...] Because this is insecure, [...] use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting. oh, and also, this one didn't work for me. Thats why I came here :p – Balmipour Sep 10 '15 at 12:16
    
so your saying use this 'sudo mysqld_safe --skip-grant-tables --skip-networking' – tkr Sep 10 '15 at 15:44
1  
If you go that way, yes. It didn't work for me, and following @Divz's answer seems way easier to me, anyway -- What I would suggest is using dpkg --get-selections | grep mysql-server- to get your exact MySQL version, then go for sudo dpkg-reconfigure mysql-server-5.x (replace 5.x with your server version, btw). I commented @Divz's answer with this precision, but it's masked by the several "thanks" comments. – Balmipour Sep 10 '15 at 16:13
2  
update user set authentication_string=password('1111') where user='root'; – Dejell Dec 17 '15 at 20:51
2  
This actually worked...the other solutions still gave me the same error. – eddy147 Jan 1 '17 at 16:49

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;
quit;

Stop MySQL Server:

/etc/init.d/mysql stop

Start MySQL server and test it:

mysql -u root -p
share|improve this answer
1  
Thank you. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) was also plaguing my server. 'mysqld_safe' also failed until I did a 'killall mysqld' AFTER your Step 1. – Marcos Aug 29 '14 at 10:09
    
Please, NOT that way ! @Divz's answer is more secure. Quoting the doc about --skip-grant-tables: This enables anyone to connect without a password and with all privileges [...] Because this is insecure, [...] use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting. oh, and also, this one didn't work for me. Thats why I came here :p – Balmipour Sep 10 '15 at 12:19
2  
Its giving me: ERROR 1054 (42S22): Unknown column 'password' in 'field list' – MD. Mohiuddin Ahmed Dec 7 '15 at 12:15
    
Check answer by @Anshu it's more secure way of handling this – Mugoma J. Okomba Aug 29 '16 at 7:23
    
For me, the mysql version is 5.6+, and the OS is centOS6.5, I should use the /etc/init.d/mysqld stop not /etc/init.d/mysql stop. – StrongYoung Apr 12 '17 at 1:28

Am using Ubuntu-16.04 : installed mysql - 5.7. I Had the same issue : Login denied for root user.

Tried the below steps:

1. dpkg --get-selections | grep mysql (to get the version of mysql).

2. dpkg-reconfigure mysql-server-5.7

3. mysql -u root -p

Without -p that doesn't prompt you to ask password. Once you are in, you can create a user with a password by following steps :

CREATE USER 'your_new_username'@'your-hostname' IDENTIFIED BY 'your-password';

GRANT ALL PRIVILEGES ON *.* to 'your_new_username'@'your-hostname' WITH GRANT OPTION;

Exit from the root and login from the you gave above.

mysql -u <your_new_username> -p

For some reason still just typing mysql does not work. AT ALL. I suggest to make it a habit to use mysql -u -p.

share|improve this answer
    
this is a real answer! there goes my 10! – Braian Mellor Jul 25 '17 at 16:38

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 -uroot -p 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass5!'; 
share|improve this answer
1  
This looked promising, but it doesn't work for me. When I run sudo grep 'temporary password' /var/log/mysqld.log I get /var/log/mysqld.log: No such file or directory – Eric Hepperle - CodeSlayer2010 Apr 20 '17 at 16:17
2  
Try sudo grep 'temporary password' /var/log/mysql/error.log. @Lahiru solution worked for me. – artemisian Jun 9 '17 at 21:26
1  
I tried sudo grep 'temporary password' /var/log/mysql/error.log but it returns nothing. – learner Aug 22 '17 at 16:53

Please read the official documentation: Mysql: How to Reset the Root Password

If you have access to terminal:

MySQL 5.7.6 and later:

$ mysql
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';

MySQL 5.7.5 and earlier:

$ mysql
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
share|improve this answer
1  
Gives : ERROR 1131 (42000): You are using MySQL as an anonymous user and anonymous users are not allowed to change passwords – MD. Mohiuddin Ahmed Dec 7 '15 at 12:19
    
It's normal. Your user is anonymous. You need to execute > mysql -u {your_username} – d.danailov Dec 7 '15 at 16:22

I came across this very annoying problem and found many answers that did not work. The best solution I came across was to completely uninstall mysql and re-install it. On re-install you set a root password and this fixed the problem.

sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-5.5 mysql-client-core-5.5 sudo rm -rf /etc/mysql /var/lib/mysql sudo apt-get autoremove sudo apt-get autoclean

I found this code elsewhere so I take no credit for it. But it works. To install mysql after uninstalling it I think digital ocean has a good tutorial on it. Checkout my gist for this.
https://gist.github.com/JamesDaniel/c02ef210c17c1dec82fc973cac484096

share|improve this answer
    
Works on Raspberry Pi too :) – CodingYourLife Apr 12 '17 at 22:18

I am using mysql-5.7.12-osx10.11-x86_64.dmg in Mac OSX

The installation process automatically sets up a temporary password for root user. You should save the password. The password can not be recovered.

Follow the instruction

  1. Go to cd /usr/local/mysql/bin/
  2. Enter the temporary password (which would look something like, "tsO07JF1=>3")
  3. You should get mysql> prompt.
  4. Run, SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{YOUR_PASSWORD}'); If you wish to set your password: "root" then the command would be, SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
  5. Run ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
  6. Run exit
  7. Run ./mysql -u root -p
  8. Type your password. In my case I would type, "root" (without quote)
  9. That's all.

For convenience, you should add "/usr/local/mysql/bin" to your PATH

Now from anywhere you can type ./mysql -u root -p and then type the password and you will get mysql> prompt.

Hope it helps.

share|improve this answer

Just one line and it solved my issue.

sudo dpkg-reconfigure mysql-server-5.5
share|improve this answer

BY default password will be null, so you have to change password by doing below steps.

connect to mysql

root# mysql

Use mysql

mysql> update user set password=PASSWORD('root') where User='root'; Finally, reload the privileges:

mysql> flush privileges; mysql> quit

share|improve this answer

In recent MySQL versions there is no password in mysql.user table.

So you need to execute ALTER USER. Put this one line command into the file.

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

And execute it as init file (as root or mysql user)

mysqld_safe --init-file=/home/me/mysql-init &

MySQL server need to be stopped to start mysqld_safe.

Also, there may be a problem with apparmor permissions to load this init file. Read more here https://blogs.oracle.com/jsmyth/entry/apparmor_and_mysql

share|improve this answer

In Ubuntu 16.04 (MySQL version 5.7.13) I was able to resolve the problem with the steps below:

  1. Follow the instructions from the in section B.5.3.2.2 Resetting the Root Password: Unix and Unix-Like Systems MySQL 5.7 reference manual

  2. When I tried #sudo mysqld_safe --init-file=/home/me/mysql-init & it failed. The error was in /var/log/mysql/error.log

    2016-08-10T11:41:20.421946Z 0 [Note] Execution of init_file '/home/me/mysql/mysql-init' started. 2016-08-10T11:41:20.422070Z 0 [ERROR] /usr/sbin/mysqld: File '/home/me/mysql/mysql-init' not found (Errcode: 13 - Permission denied) 2016-08-10T11:41:20.422096Z 0 [ERROR] Aborting

The file permission of mysql-init was not the problem, need to edit apparmor permission

  1. Edit by #sudo vi /etc/apparmor.d/usr.sbin.mysqld

    ....
      /var/log/mysql/ r,
      /var/log/mysql/** rw,
    
    
    # Allow user init file
      /home/pranab/mysql/* r,
    
      # Site-specific additions and overrides. See local/README for details.
      #include <local/usr.sbin.mysqld>
    }
    
  2. Do #sudo /etc/init.d/apparmor reload

  3. Start mysqld_safe again try step 2 above. Check /var/log/mysql/error.log make sure there is no error and the mysqld is successfully started

  4. Run #mysql -u root -p

    Enter password:

Enter the password that you specified in mysql-init. You should be able to log in as root now.

  1. Shutdown mysqld_safe by #sudo mysqladmin -u root -p shutdown

  2. Start mysqld normal way by #sudo systemctl start mysql

share|improve this answer

if the problem still exists try to force changing the pass

Stop MySQL Server (on Linux):

/etc/init.d/mysql stop

Stop MySQL Server (on Mac OS X):

mysql.server stop

Start mysqld_safe daemon with --skip-grant-tables

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;
quit;

Stop MySQL Server (on Linux):

/etc/init.d/mysql stop

Stop MySQL Server (on Mac OS X):

mysql.server stop

Start MySQL server service and test to login by root:

mysql -u root -p
share|improve this answer

The answer may sound silly, but after wasting hours of time, this is how I got it to work

mysql -u root -p

I got the error message

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

Even though I was typing the correct password(the temporary password you get when you first install mysql)

I got it right when I typed in the password when the password prompt was blinking

share|improve this answer

protected by Community Oct 18 '14 at 4:03

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.