nodejs 中用localhost或者127.0.0.1连接数据库可以,但是使用本机 ip4地址连接时,失败,报错如下
ER_HOST_NOT_PRIVILEGED: Host 'LAPTOP-9VT1D63G' is not allowed to connect to this MySQL server
意思是没有权限,所以把权限开开即可:
//先打开命令窗口,登录自己的mysql
mysql -u root -h 127.0.0.1 -P3306 -p
//查看它允许访问的用户
select host,user from mysql.user;//发现只有localhost
//设置一下允许访问的用户,这里%设置所有用户都可访问
update mysql.user set host = '%' where user = 'root';
//执行让修改生效
flush privileges;