I have installed MySql server on Ubuntu 14.04 LTS. I have prior experience to it too.

But whenever I login, using mysql -u root -p and after entering password it says:

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

I have entered the correct password. Same thing happened days back, but then I completely removed mysql and reinstalled it again.

Also I tried restarting the mysql server but no help!

Can anyone please help?

  • 3
    Hi, You can better reset the MySQL password by stopping the MySQL service first and start its services in safe mode. After that you can reset the MySQL password and then try login to MySQL with that password.
    – BDRSuite
    Jan 1, 2015 at 5:05
  • 2 thing: user "root". Are you sure about that? It is not your normal user name? And mind the caps-lock.
    – Rinzwind
    Jan 1, 2015 at 9:13
  • @rinzwind if not root then what? Because while installing it asked password for root, right?
    – Sam
    Jan 1, 2015 at 10:22
  • 1
    @vembutech: helped and resolved.
    – Sam
    Jan 1, 2015 at 10:52
  • 2
    We are glad to know the issue has resolved. Happy New Year !
    – BDRSuite
    Jan 1, 2015 at 10:53

3 Answers 3


Always read the contents of /usr/share/doc/package-name/ for packages you have installed. They contain important information for using and administrating packages.

So for mysql-server-5.5 there are important information in /usr/share/doc/mysql-server-5.5/README.Debian.gz. As it is gzipped, it is convenient to use zmore to read it.

There you can see that you can use the debian-sys-maint user to access the database for administration. Just as root use /etc/mysql/debian.cnf configuration file to login as that user. Notice that you NEVER should change that users password, unless you also change it in the file debian.cnf. If not, mysql will stop working.

So, to change password for the root user, try this.

$ sudo mysql --defaults-file=/etc/mysql/debian.cnf
mysql> UPDATE mysql.user SET Password=PASSWORD('*secret*') WHERE User='root';
mysql> quit;
$ mysql -u root -p

If you can use the *secret* password to login in as user root, you have managed to change the password for root.

Notice this use of the debian-sys-maint user only works for Debian based distributions.

Notice also that if you also have the package dbconfig-common installed and set to store the root password (see /etc/dbconfig/config), you need to reconfigure that package so it knows the password to administrat your databases for other packages.

Lastly, good information can also be read in Debians Wiki and in the free The System administrators Book

  • Don't mind. I don't know much, but are there any security issues with this?
    – Sam
    Jan 2, 2015 at 14:15
  • 1
    @Anders I have had to reset a few mysql root passwords in my time and have always used the method I posted, since that's what I found with a search. I have never had any problems, but your answer is definitely the correct one.
    – geoffmcc
    Jan 3, 2015 at 16:33
  • @SameerThigale, no that I know of that you wouldn't get anyway if someone get their hands on the root account. Because if you get you computer hacked that much, you do have larger problems than someone hacking your MySQL installation. See the other solution which uses the computers root account.
    – Anders
    Jan 5, 2015 at 1:57
  • @geoffmcc, any solution that works is right, but with this one you don't need to stop the MySQL server which could bee an advantage. And it is "The Debian Way™". ☺
    – Anders
    Jan 5, 2015 at 2:01

First stop the mysql server

 sudo service mysql stop

Run the following command that will start the server again and skip the grant tables which store the passwords.

 mysqld_safe --skip-grant-tables

Mysql server should now start. Next we log into root without a password.

 mysql -u root 

And then press enter

Now we run the following commands in mysql to reset the password

 update user set 
 where user='root';
 flush privileges;

Now restart the mysql server

 sudo service mysql restart 

You should now be able to log in with the password you just set

 mysql -u root -p

Press enter and you should get a prompt for root password and it should be accepted.

  • Is this a safe method?
    – Sam
    Jan 2, 2015 at 3:54
  • Yes. Only option I really know of. All your doing in starting mysql without user passwords and then manually setting a new password
    – geoffmcc
    Jan 2, 2015 at 3:56
  • The only "problem" is that you for a short time rum without passwords on the MySQL server. And that you need to stop the server twice.
    – Anders
    Jan 5, 2015 at 2:07
  • The MySQL docs (dev.mysql.com/doc/refman/5.7/en/…) also suggest running the server with --skip-networking.
    – mwfearnley
    Nov 20, 2016 at 11:53
  • Just a tip: in Debian before update I must select database: select database mysql; Feb 21, 2019 at 5:06

I had this problem and I solved it for me by adding sudo, e.g. sudo mysql -u root -p and using the password set at installation.

  • 1
    I've been working on this for hours; none of the guides I saw for installing mysql and running mysql_secure_installation mentioned this. And mysql_secure_installation didn't mention it. And FYI, I'm a longtime Ubuntu (and varients) user who instinctively things to throw sudo in front of a command that isn't working and I hadn't thought of it. Thank you, thank you.
    – Shai
    Jun 26, 2021 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .