MySQL Access denied for user 'root'@'localhost'

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 get

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

How can I access MySQL in my Ubuntu server 12.04?

3

7 Answers

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.

5

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.

2

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.

2

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
1

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

0

After upgrading from ubuntu 14.4LTS to 16.4LTS the mysql root user could no more login due a wrong entry in the mysql user table's plugin column. The upgrade sets the plugin value for the root@localhost user to auth_socket but the correct entry must be mysql_native_password, if you used mysql native password encryption before.

1

After completely reinstalling mysql (thinking that I was using the incorrect password) I ended up with the same problem, but eventually found Ivan Black's comment above which did the trick:

sudo mysql -u root -p

Why I should need elevated privileges to log in with the root user is beyond me.

You Might Also Like