mysql安装在centos7报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

时间:2022-06-07 22:33:20
【问题】:
mysql版本:5.7.13

首次在centos下安装mysql,客户端连接mysql时报错:

[root@localhost opt]# /usr/bin/mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

【解决】:
1、找到密码

[root@localhost opt]#  cat /var/log/mysqld.log  | grep password2016-07-16T05:01:47.771362Z 1 [Note] A temporary password is generated for root@localhost: q.)aq!YaH6y-
2016-07-16T05:02:32.000199Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)
2016-07-16T05:02:39.063852Z 3 [Note] Access denied for user 'root'@'localhost' (using password: NO)

2、重新连接mysql:

[root@zzs171 opt]# /usr/bin/mysql -u root -pEnter password: 
输入上面找到的密码,即可进入mysql客户端连接。

3、修改mysql的root的密码:
进入mysql客户端之后,show databases; 会提醒先修改密码:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
设置密码,以下两种方式均可:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Mysqlpassw0rd.');

alter user 'root'@'localhost' identified by 'Mysqlpassw0rd.';


OK,至此设置成功。注意mysql有密码复杂度的限制。密码太简单会提示:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');ERROR 1819 (HY000): Your password does not satisfy the current policy requirements


【其他补充】
网上还有几种方案,但是都不行。例如:

方案一:mysqladmin -u root password 123456,结果提示错误:error: 'Access denied for user 'root'@'localhost' (using password: NO)'

方案二:
用mysqld_safe skip-grant-tables进入mysql,然后sql语句修改root密码,还是提示错误:-bash: mysqld_safe: command not found