在CentOS虚拟机上安装好了MySQL服务以后,在Windows上用Workbench客户端去连接时碰到很多问题,现在把解决过程记录一下。
1.在Windows上ping CentOS IP是可以ping通的,在Workbench上配置好后出现如下提示,

在Windows命令行里输入 telnet 192.168.199.129 3306 执行,也得到:could not open connection to the host, on port 3306 这样的提示。
在网上查了一下,觉得可能是3306端口没有打开,先检查一下3306端口的使用情况,

3306这个端口没有被别的进程占用。然后看看防火墙的情况,CentOS7中防火墙改成了firewall,查看firewall的服务状态,

firewall防火墙正在运行。接下来查询3306端口是否开放,输入 firewall-cmd --query-port=3306/tcp,如果显示的是no,则输入 firewall-cmd --permanent --add-port=3306/tcp 开放3306端口,
重启防火墙,输入 firewall-cmd --reload。
2.完成以上步骤后,再次在Windows命令行中输入 telnet 192.168.199.129 3306 执行,得到类似:host is not allowed to connect mysql 这样的提示。
在网上查了一下,似乎是权限的问题,一般有两个途径,
第一个方法,可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%",
在CentOS虚拟机中进去MySQL,用下面的语句,
mysql>use mysql;
mysql>update user set host = '%' where user = 'root';
mysql>select host, user from user;
但是改完了后,还是不行,还是提示 host is not allowed to connect mysql。
试第二个方法,授权,例如你想myuser使用mypassword从任何主机连接到MySQL服务器的话,用下面的语句,
mysql>grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;
mysql>flush privileges;
执行完后成功了,可以用workbench连接上MySQL服务了。

