mysql ERROR 1044 (42000): Access denied for user ''@'localhost' to database

时间:2022-01-24 03:13:04

新安装的mysql密码是空的。

 

./mysql -u root -p
use mysql
SELECT `Host`,`User` FROM user;
UPDATE user SET `Host` = '%' WHERE `User` = 'root' LIMIT 1;
FLUSH PRIVILEGES;

GRANT ALL PRIVILEGES ON *.* TO 'mysql'@'%' IDENTIFIED BY 'test' WITH GRANT OPTION;

 

方法一:
1.关闭mysql
   # service mysqld stop
2.屏蔽权限
   # mysqld_safe --skip-grant-table
   屏幕出现: Starting demo from .....
3.新开起一个终端输入
   # mysql -u root mysql
   mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
   mysql> FLUSH PRIVILEGES;//记得要这句话,否则如果关闭先前的终端,又会出现原来的错误
   mysql> \q

 

 
方法
mysql> use mysql
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'mysql'
mysql> exit
Bye
[root@testtest ~]# service mysqld stop 
Stopping mysqld:                      [ OK ]
[root@testtest ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 
 
[root@testtest ~]# mysql -u root -p -hlocalhost
Enter password: 
 
mysql> use mysql
 
mysql> SELECT host,user,password,Grant_priv,Super_priv FROM mysql.user;
 
mysql> UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';
 
mysql> FLUSH PRIVILEGES;
 
mysql> GRANT ALL ON *.* TO 'root'@'localhost';
 
mysql> GRANT ALL ON *.* TO 'root'@'cn.cn.cn.cn';
 
mysql> GRANT ALL ON *.* TO 'root'@'245.245.245.245';
 
mysql> GRANT ALL ON *.* TO 'root'@'127.0.0.1';
 
mysql> FLUSH PRIVILEGES;
 
 
mysql> quit
Bye
[root@testtest ~]# service mysqld start 

 

 

方法三:

在my.ini的[mysqld]字段加入: 
skip- grant -tables
?
重启mysql服务,这时的mysql不需要密码即可登录数据库
然后进入mysql 
?
mysql>use mysql;
mysql> update user set password = password ( '新密码' ) WHERE User = 'root' ;
mysql>flush privileges ;
运行之后最后去掉my.ini中的skip-grant-tables,重启mysqld即可。
 
 
 
 
mysql> use mysql
ERROR 1044 (42000): Access denied for user 'root' @ 'localhost' to database 'mysql'
mysql> exit
Bye
[root@testtest ~]# service mysqld stop
Stopping mysqld:                      [ OK ]
[root@testtest ~]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
 
[root@testtest ~]# mysql -u root -p -hlocalhost
Enter password :
 
mysql> use mysql
 
mysql> SELECT host, user , password ,Grant_priv,Super_priv FROM mysql. user ;
 
mysql> UPDATE mysql. user SET Grant_priv= 'Y' , Super_priv= 'Y' WHERE User = 'root' ;
 
mysql> FLUSH PRIVILEGES ;
 
mysql> GRANT ALL ON *.* TO 'root' @ 'localhost' ;
 
mysql> GRANT ALL ON *.* TO 'root' @ 'cn.cn.cn.cn' ;
 
mysql> GRANT ALL ON *.* TO 'root' @ '245.245.245.245' ;
 
mysql> GRANT ALL ON *.* TO 'root' @ '127.0.0.1' ;
 
mysql> FLUSH PRIVILEGES ;
 
 
mysql> quit
Bye
[root@testtest ~]# service mysqld start