天天看点

拯救报错:Error: ER_HOST_NOT_PRIVILEGED: Host ‘x.x.x.x‘ is not allowed to connect to this MySQL server

问题描述:

使用 Nodejs 连接云服务器 MySQL 数据库,执行 js 文件报错如下:

Error: ER_HOST_NOT_PRIVILEGED: Host 'x.x.x.x' is not allowed to connect to this MySQL server
{
  code: 'ER_HOST_NOT_PRIVILEGED',
  errno: 1130,
  sqlMessage: "Host 'x.x.x.x' is not allowed to connect to this MySQL server",
  sqlState: undefined,
  fatal: true
}
           

解决方法:

这是由于MySQL配置了不支持远程连接引起的,需要连接服务器进行如下配置(需登陆 root 账号):

mysql -u root -p
use mysql;
select host from user where user='root';
update user set host = '%' where user ='root';
// 如果 Host = '%',表示所有 IP 都有连接权限,实际应根据生产环境的 IP 进行设置
flush privileges;
           

继续阅读