【问题】:
mysql版本:5.7.13
首次在centos下安装mysql,客户端连接mysql时报错:
[plain] view plain copy [root@localhostopt]#/usr/bin/mysql-uroot ERROR1045(28000):Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
【解决】:
1、找到密码
[plain] view plain copy [root@localhostopt]#cat/var/log/mysqld.log|greppassword -07-16T05:01:47.771362Z1[Note]Atemporarypasswordisgeneratedforroot@localhost:q.)aq!YaH6y- -07-16T05:02:32.000199Z2[Note]Accessdeniedforuser'root'@'localhost'(usingpassword:NO) -07-16T05:02:39.063852Z3[Note]Accessdeniedforuser'root'@'localhost'(usingpassword:NO)
2、重新连接mysql:
[plain] view plain copy [root@zzs171opt]#/usr/bin/mysql-uroot-p Enterpassword: 输入上面找到的密码,即可进入mysql客户端连接。
3、修改mysql的root的密码:
进入mysql客户端之后,show databases; 会提醒先修改密码:
[plain] view plain copy mysql>showdatabases; ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatementbeforeexecutingthisstatement. 设置密码,以下两种方式均可: [plain] view plain copy SETPASSWORDFOR'root'@'localhost'=PASSWORD('Mysqlpassw0rd.'); alteruser'root'@'localhost'identifiedby'Mysqlpassw0rd.';
OK,至此设置成功。注意mysql有密码复杂度的限制。密码太简单会提示:
[plain] view plain copy mysql>SETPASSWORDFOR'root'@'localhost'=PASSWORD('123'); ERROR1819(HY000):Yourpassworddoesnotsatisfythecurrentpolicyrequirements
【其他补充】
网上还有几种方案,但是都不行。例如:
[plain] view plain copy 方案一: mysqladmin-urootpassword123456,结果提示错误:error:'Accessdeniedforuser'root'@'localhost'(usingpassword:NO)' 方案二: 用mysqld_safeskip-grant-tables进入mysql,然后sql语句修改root密码,还是提示错误:-bash:mysqld_safe:commandnotfound
本文从 /kuluzs/article/details/51924086 转载