已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

在Linux下安装完mysql的以下版本:

已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


安装完后,直接在命令行键入 mysql,出现以下结果:

已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


原因:

该版本的mysql安装完后, 第一次以root账户的身份登陆mysql时,密码不再是空,而是在安装的时候指定了某一个密码(具体是什么,我也不太清楚)

(还有可能的一个原因是:首先要检查mysql服务是否在运行,否则也会出现下面的结果)

已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


解决方法:

1)编辑 /etc/my.cnf 文件,在文件中添加语句 “skip-grant-tables”, 即登陆数据库的时候跳过密码检查,配置完成之后,重启mysql服务,

然后再次登陆mysql(此时的密码,直接按Enter键即可)就成功了。


已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


2)进入mysql数据库之后,修改root的密码 (改为自己想要的密码即可)

已解决 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


3)上述操作完成之后,记得将之前在配置文件 /etc/my.cnf 中添加的语句 “skip-grant-tables” 去掉,然后保存退出


4)再次重启mysql服务,就可以了。