问题
在CentOS7安装mysql中已经完成在CentOS7中安装好了mysql,且创建好了相应的用户,并设置对应的用户密码,现在尝试通过命令行远程命令访问mysql。
防火墙
mysql --host=xxx.xxx.200.58 --port=3306 -u myuser -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.200.58' (61)
看到这个现象,可以判定为CentOS的防火墙挡住了。
firewall-cmd
# 查看firewall-cmd所知道是所有服务,这里主要关注mysql
firewall-cmd --get-services
# 把mysql服务暴露在外面,主要就是开放3306端口
firewall-cmd --zone=public --add-service=mysql --permanent
# 重启firewall
firewall-cmd --reload
注意:上面是比较粗放的权限控制,因为在局域网里面,更加细粒度的端口开放,应该参考下面的命令。
# 防火墙更加细粒度的权限开放
firewall-cmd --add-rich-rule 'rule family="ipv4" source address="$your_IP_or_IP_Range" service name="mysql" accept' --permanent
数据库
mysql --host=xxx.xxx.200.58 --port=3306 -u myuser -p
Enter password:
ERROR 1130 (HY000): Host 'xxx.xxx.200.58' is not allowed to connect to this MySQL server
Mysql8
临时设置用户永许任何ip都可以访问
mysql> GRANT ALL ON mydb.* TO 'myuser'@'%';
Query OK, 0 rows affected (0.07 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
这个问题是Mysql要设置指定远程ip能够访问。