Set a root password for socket and TCP access

suggest change

This recovers from Access denied for user 'root'@'localhost' (using password: YES). Stop MySQL:

sudo systemctl stop mysql

Restart mySQL, skipping grant tables:

sudo mysqld_safe --skip-grant-tables

Login:

mysql -u root

In the SQL shell, see which root accounts exist. On 8.0, --skip-grant-tables also enables --skip-networking unless you turn that off, and the password column is gone:

SELECT User, Host, plugin, authentication_string FROM mysql.user WHERE User = 'root';

Setting plugin to NULL does not enable every plugin. Pick one authentication plugin. mysql_native_password accepts a password over the socket and over TCP. It replaces auth_socket, which ignores the password. On 5.7:

UPDATE mysql.user
SET plugin = 'mysql_native_password',
    Password = PASSWORD('mypassword')
WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

PASSWORD() was removed in 8.0. Use ALTER USER after FLUSH PRIVILEGES. Prefer caching_sha2_password unless an old client needs mysql_native_password:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'mypassword';

In Unix shell stop mySQL without grant tables, then restart with grant tables:

sudo service mysql stop
sudo service mysql start

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents