linux安装mysql后root无法登录

时间:2021-10-19 00:22:09

[root@localhost mysql]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
解决方法:
方法一: 
# /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 
# mysql -uroot -p 
Enter password: <输入新设的密码newpassword> 
mysql>SET PASSWORD = PASSWORD('123456')

上面这一句必须要执行,要不然会出现“ERROR 1820 (HY000): You must SET PASSWORD before executing this statement” 的错误