前言
在远程访问mysql服务的时候发现连接失败,查看mysql日志发现Access denied for user 'root'@XXX,于是检查了mysql配置没发现什么问题,打开mysql库中的user表,查看发现root用户中authentication_string字段是空的,也就是密码是空的,也许这应该就是连接出错的原因吧。
正文
先在服务器上测试了以下命令:
mysql -h 172.x.x.x -u root -p
直接报以下错:
ERROR 1045 (28000): Access denied for user 'root'@'xxx' (using password: YES)
应该就是用户名密码错了导致的吧
于是进入mysql数据库修改user表中root用户密码,也就是authentication_string字段,同时也要修改plugin字段,使用以下语句:
update user set authentication_string=password('xxx') where user='root';
然后又报了以下错:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('123456') where user='root'' at line 1
查看了半天,原因应该就是mysql8.0无法使用password函数,但是不加password又不对,所以只能用以下方式来修改:
ALTER user 'root'@'%' IDENTIFIED BY 'xxx';
最后的就是密码了,再次用mysql命令测试发现用密码可以进入了。