nodejs连接mysql的坑:“Access denied for user ‘root‘@‘localhost‘ (using password: YES)

2,359 阅读1分钟

今天在使用Node的express框架连接mysql数据库时碰到了以下错误:

code: 'ER_ACCESS_DENIED_ERROR', errno: 1045, sqlMessage: "Access denied for user 'root'@'localhost' (using password: YES)", sqlState: '28000', fatal: true

经过几番查询最后才弄清楚时因为Mysql服务器通过拒绝访问进行响应,这就需要我们重置mysql的密码。

因此我们就可以使用此命令来进行密码修改:

update MySQL.user set authentication_string=password(123)(新密码) where user='python'(用户名) //刷新 flush privileges;

因为我使用的mysql版本是8.0以上的,会报以下语法错误。

所以我最终使用了这种方式进行密码修改(这种方式不用进入mysql,可直接使用终端进行):

MySQLadmin -upython -p password 新密码

密码修改完成后再进行连接会发现连接成功。数据也可以获取到