MySQL异常:access denied for user 'root'@'localhost'

正确重置MySQL密码

# /etc/init.d/mysql stop

  # mysqld_safe --user=mysql --skip-grant-tables --skip-networking &   # mysql -u root mysql

   mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’; 

  mysql> FLUSH PRIVILEGES; 

  mysql> quit   # /etc/init.d/mysql restart


下面是演示:


[root@localhost ~]# service mysqld stop

停止 mysqld: [确定]

[root@localhost ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 

[1] 2711

[root@localhost ~]# 131211 19:34:16 mysqld_safe Logging to '/var/lib/mysql/localhost.localdomain.err'.

131211 19:34:16 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@localhost ~]# mysql


mysql> use mysql;

Database changed

mysql> update user set password=password("123456") where user="root";

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3  Changed: 3  Warnings: 0


mysql> flush privileges

    -> ;

Query OK, 0 rows affected (0.00 sec)


mysql> quit