問題描述:
使用 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;