今天在用Navicat连接一台虚拟机上的MySQL时报错:
Host is not allowed to connect to this MySQL server
![97b75bd489b7c6d8ecc4b8e79e29c9bb.png](https://img-blog.csdnimg.cn/img_convert/97b75bd489b7c6d8ecc4b8e79e29c9bb.png)
虚拟机中的MySQL是通过PHPStudy安装的,无法远程连接是因为MySQL默认配置了不支持远程连接引起的。
解决方法:
一、在虚拟机本地主机上登录root用户
找到mysql.exe所在路径,shift+右键,在当前窗口打开命令行。
执行命令:
mysql -uroot -proot
成功连接mysql以后,执行以下sql语句查看user库情况:
use mysql;select host from user where user='root';
![4611dbdcf0dafd4e322ae83278b76c44.png](https://img-blog.csdnimg.cn/img_convert/4611dbdcf0dafd4e322ae83278b76c44.png)
可以看到,mysql默认配置的host都是本地IP。
二、修改配置使之允许远程登录访问
执行以下sql语句:
update user set host='%' where user='root';
Host设置了“%”后便可以允许远程访问,但需要注意,这里表里有3条记录,这样更新会出错,仅成功更新一条记录。
接下来,把多余的删掉,仅保留host值为'%'的记录即可。
delete from user where user = 'root' and host <>'%';
![e024fe9c37700ddf152267d2b4422e1b.png](https://img-blog.csdnimg.cn/img_convert/e024fe9c37700ddf152267d2b4422e1b.png)
三.刷新权限使配置生效
执行以下语句:
flush privileges
结果看上图,执行成功了。
下面再次测试远程连接。
![c228103c7a6d82095ec7a77c3777b154.png](https://img-blog.csdnimg.cn/img_convert/c228103c7a6d82095ec7a77c3777b154.png)
连接成功,问题解决!