MySQL Access denied for user 'root'@'localhost'
Solution 1
If you're running n localhost, just type the command below in terminal:
mysql -u root -p
If you're using an external server, enter the host IP (xxx.xxx.xxx.xxx) as well:
mysql -hxxx.xxx.xxx.xxx -uroot -p
You will be prompted for your password, enter it and you will be able to access your MySQL prompt.
You can also look at this answer on how to reset your MySQL password.
Solution 2
I get the same symptoms when I update, but for me the fix (after killing mysql and restarting with --skip-grant-tables to get in) is to execute
update mysql.user set plugin = '';
The update process likes to set this column to "unix_socket". I don't know what that is supposed to accomplish, but for me it breaks everything.
Solution 3
The same as Ulrich Metzger, after upgrading my machine to Ubuntu 16.04 and Mysql-server-5.7, I couldn't log in with root because the plugin
column changed.
My problem was not solved with sudo dpkg-reconfigure mysql-server-5.7
+ removing + purging + cleaning.
I had to stop the mysql service:
# sudo service mysql stop
Then restart the mysql daemon with the --no-grant-tables
option:
# sudo mysqld_safe --no-grant-tables &
Then in another terminal, enter the mysql console (which now doesn't need authentication) with the command mysql
, and update the password and plugin columns via a SQL UPDATE:
UPDATE mysql.user
SET authentication_string=PASSWORD(''),
plugin='mysql_native_password'
WHERE User='root' AND Host='localhost';
Finally, kill the mysqld_safe
command, restart mysql service and connect to mysql normally:
# sudo service mysql start
# mysql
Solution 4
I solved my problem with this:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt install mysql-server
Be extremely responsible. With this solution, you are removing MySql server and all of its data.
Solution 5
Set a password for mysql
sudo dpkg-reconfigure mysql-server-5.x
Now open the terminal and type
mysql -uroot -p
give password and press enter
Related videos on Youtube
user253867
Updated on September 18, 2022Comments
-
user253867 over 1 year
In my server, everytime I try to access MySQL I get the error:
Access denied for user 'root'@'localhost' (using password: NO)
when I try
mysqladmin -u root -p password
I getAccess denied for user 'root'@'localhost' (using password: YES)
How can I access MySQL in my Ubuntu server 12.04?
-
Amias almost 8 yearsTry to avoid typing your password as a parameter on the command line, other users can find it via process listings. Much better to type it direct into the program or use a key
-
Melebius over 7 yearsThe syntax
-p password
is wrong, it has to be-ppassword
but is not recommended. More info: stackoverflow.com/questions/5131931/… -
Rathish Kumar B about 7 yearsCheck this page for possible causes of this error and workarounds to fix this error. rathishkumar.in/2017/04/…. It may help you.
-
-
user253867 about 10 yearsi got in but i still don't know how to change or chose a password
-
Parto about 10 yearsIn the answer I linked to, you have to replace the
5.x
at the end with the version of the MySQL server you're using. I'm using 5.5 so my command will besudo dpkg-reconfigure mysql-server-5.5
-
onalbi almost 8 yearsThis solve my problem on Ubuntu 16 LTS, after trying everything.. permissions, group user. Thanx.
-
phil294 over 7 yearsso, the problem is that OP sees
Access denied for user 'root'@'localhost'
. You suggest callingmysql -u root -p
. The result of this command is:Access denied for user 'root'@'localhost'
. -
TheVillageIdiot almost 6 yearsyes, I had run hardening script and it changed the plugin to
mysql_socket
so whatever I was set to usingPASSWORD('xxxxxx')
, was not working. -
Ivan Black over 5 yearsDoesn't work for me.
-
Ivan Black over 5 years
sudo mysql -u root -p
works for me, butmysql -u root -p
does not -
Matiss about 5 years!! Be extremely responsible. With this solution you are removing MySql server and all of it's data.
-
SteveFromAccounting almost 5 yearsThis is the one that's worked for me, and seems to be the root of the problem. Uninstalling and reinstalling won't do anything to fix the "problem".
-
Rodrigo almost 3 years"...and you will be able to access your MySQL prompt." No, we're not!
-
Bruno Leveque about 2 yearsThis was the only solution which worked for me! However, there's a typo, I had to use
--skip-grant-tables
instead of--no-grant-tables