【问题标题】:What firewall settings should be updated in order to connect mysql to google compute engine为了将 mysql 连接到谷歌计算引擎,应该更新哪些防火墙设置
【发布时间】:2015-04-17 15:22:32
【问题描述】:

我已经创建了一个计算引擎实例并安装了 MySQL 也授权云 sql 实例。需要更改一些防火墙设置以打开端口 3306 使用此命令:

$ gcloud compute firewall-rules create FIREWALL_RULE --allow tcp:3306

但仍然无法连接到数据库。我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

【问题讨论】:

  • 通过发出此命令,您将打开默认网络上的 3306 端口。您的实例是在默认网络上运行还是在其他网络上运行?要检查的另一件事是实例上的 iptables。尝试禁用 iptables 进行测试。在授权网络中,您还需要授权实例的公共 IP 而不是私有 IP。
  • 检查 Mysql 是否监听所有地址,而不仅仅是本地。在 /etc/mysql/my.cnf(对于 Debian 操作系统)中检查:bind-address = 0.0.0.0
  • 我尝试使用 bind-address = 0.0.0.0 和 bind-address = localhost,但它对我也不起作用,收到此错误 ERROR 2002 (HY000): Can't connect to local MySQL server通过套接字 '/var/run/mysqld/mysqld.sock' (2) 在 ssl 上编写此命令时:mysql --host=localhost --user= --password

标签: java mysql google-compute-engine google-cloud-sql


【解决方案1】:

通过运行以下命令关闭 iptables 进行测试:

    sudo service iptables stop

如果这样的话,重启 iptables 并允许 3306 端口(mysql 端口)通过:

    sudo service iptables restart
    sudo iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
    sudo service iptables save

** 以上命令适用于 linux 操作系统

【讨论】:

  • 仍然收到 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  • 我也需要更改绑定地址吗?提到127.0.0.1
  • 是的,把它改成:bind-address = 0.0.0.0,这样它就可以监听所有IP,你也可以通过运行:telnet localhost 3306来检查mysql是否接受连接
  • 是否需要覆盖/etc/my.cnf,我用的是centos,没有运行telnet命令的权限
  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) while writing this command on ssl: mysql --host =localhost --user= --password
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-10
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多