MySQL ERROR 1045 (28000)错误

注:对于MySQL8,此方法已无效。
执行update mysql.user set plugin='' where user='root';之后,MySQL无法登录,报错:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

解决方法:
停止mysql服务
~$ sudo service mysql stop

以安全模式启动MySQL
~$ sudo mysqld_safe --skip-grant-tables &

此时可能会报错:mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists
解决办法还是很简单的:mkdir -p /var/run/mysqld,再执行chown mysql:mysql /var/run/mysqld即可

MySQL启动之后就可以不用密码登陆了(可能需要另外开一个shell)

1
2
3
4
~$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.10 MySQL Community Server (GPL)

执行:

1
2
3
4
5
6
mysql> update mysql.user set authentication_string=PASSWORD('newPwd'), plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)