【问题标题】:Can't connect to MYSQL server which hosted in google instance无法连接到托管在谷歌实例中的 MYSQL 服务器
【发布时间】:2017-04-14 08:42:34
【问题描述】:

我有一个谷歌云平台 - 计算引擎实例,我在上面安装了 MySQL 服务器。

现在我无法获得安装 sql 的 VM 的任何生命信号, 例如:

package com.company;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {

public static void connection(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("in conncection");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void connectToMySQL(){
    connection();
    String host = "jdbc:mysql://hotsIP:3306/DBname";
    String user = "user";
    String pass = "password";
    try {
        Connection connection = DriverManager.getConnection(host,user,pass);
        System.out.println("???");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    connectToMySQL();
}
}

这需要几秒钟,就像他尝试连接和 EXEPTION 一样

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

我做了什么让它工作:

  • 在my.conf:bind address = 0.0.0.0,skip-external-locking comment out
  • 重启服务器
  • 查看服务器是否处于活动状态
  • 查看服务器是否监听端口
  • 查看是否是 TCP

我不知道该怎么办了。

【问题讨论】:

  • Class.forName("com.mysql.jdbc.Driver");放在Connection connection = DriverManager.getConnection(host,user,pass);之前不要使用单独的方法来加载你的驱动程序connection();另一个选项,尝试不加载你的驱动程序,因为2007你不需要加载你的驱动程序,还有一件事是你的jdbc连接版本老了,试试升级到新版本看看区别
  • 感谢评论,但相同的 EXEPTION
  • 你得到什么异常解释更多?
  • intelliJ 说“原因:java.net.ConnectException:连接超时:连接”
  • 你是改了驱动还是改了什么?

标签: java mysql jdbc google-cloud-platform google-compute-engine


【解决方案1】:

您必须对您的 my.cnf 文件进行以下更改

my.cnf

bind-address = www.000webhost.com (OR)
bind-address = xx.xx.xx.xx (IP Address)

一旦更改此设置,您需要重新启动 MySQL 服务。

另外值得注意的是,MAMP/MAMP Pro 默认设置MAMP_skip-networking_MAMP。您必须在 my.cnf 中禁用此行

如果您没有任何用户登录问题,您应该能够从您的 Java 代码连接到 MySQL 数据库。

【讨论】:

    【解决方案2】:

    就我而言,根本原因是:防火墙。我试图在工作中运行应用程序。

    有趣的是,在本地运行的 App Engine 标准实际上在 Google Cloud Platform Logs 中生成了一个非错误日志,这让我放弃了防火墙假设。

    解决方案: 我发现将笔记本从家里拿来并连接到公司的网络,但无法正常工作。当我连接到手机中的共享连接时,效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2012-09-10
      相关资源
      最近更新 更多