DEV Community

Lanalen
Lanalen

Posted on

Fix MySQL Error on Ubuntu

I installed mysql on ubuntu and tried to access mysql.

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

But I got the following error.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
Enter fullscreen mode Exit fullscreen mode

So I restarted mysql.

Sudo service mysql restart
Enter fullscreen mode Exit fullscreen mode

Then I tried to access mysql again, but faced with a new error.

ERROR 1045 (28000): Access denied for user 'root'@'localhost' 
Enter fullscreen mode Exit fullscreen mode

By googling, I read that this error appears because of the type of the password.
So I changed it.
Firstly, I got into mysql with sudo commend.

Sudo mysql -u root
Enter fullscreen mode Exit fullscreen mode

Then I checked the type of the password.

SELECT User, plugin FROM mysql.user;
Enter fullscreen mode Exit fullscreen mode

The password type of user 'root' was 'auth_socket'.
I changed this to 'mysql_native_password'.

UPDATE mysql.user SET plugin='mysql_native_password' WHERE User='root';

flush privileges;
Enter fullscreen mode Exit fullscreen mode

And then I tried to access mysql.

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

It worked well.

Top comments (0)