c#远程访问MySQL报错Host 'ip地址' is not allowed to connect to this MySQL server的解决方法

报类似"Host ‘10.0.0.4’ is not allowed to connect to this MySQL server错误的主要的意思是,这个host主机不能访问本机的mysql服务,原因需要连接非本机的mysql的时候,默认host是localhost,我们需要将这个mysql连接权限设置成%,更改方法直接通过软件更改和命令行更改:

方法一:软件更改

比如在MySQL WorkBench中更改如下界面:

 

c#远程访问MySQL报错Host 'ip地址' is not allowed to connect to this MySQL server的解决方法

在标红的地方,改成%就可以了。

方法二:命令行更改

可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入MySQL后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"

mysql -u root -p

mysql>use mysql;

mysql>update user set host = '%' where user = 'root';

mysql>select host, user from user;

mysql> flush privileges;