Can't set root MySQL password to null

2019-09-07 22:08发布

问题:

When installing mysql, I didn't put anything in the root password fields, but when I ran mysql -u root I still got Access denied for user 'root'@'localhost'.

I started mysql up in safe mode without the grant tables, sudo mysql_safe --skip-grant-tables &, then jumped in with mysql -u root without a problem.

Then I did use mysql; update user set authentication_string=null where user='root'; and I got:

Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Anyone have any insight on why the password isn't updating?

回答1:

You forgot

FLUSH PRIVILEGES;

Step by step it will be like this

Stop the MySQL Server.

sudo /etc/init.d/mysql stop

Start the mysqld configuration.

sudo mysqld --skip-grant-tables &

Login to MySQL as root.

mysql -u root mysql

Replace YOURNEWPASSWORD with your new password!

UPDATE user SET Password=PASSWORD('YOURNEWPASSWORD') WHERE User='root'; FLUSH PRIVILEGES; exit;


回答2:

I had to set the plugin to mysql_native_password, so the password reset part looks like this:

update user set authentication_string=password(''), plugin='mysql_native_password' where user='root'; FLUSH PRIVILEGES;