【问题标题】:"Could not create connection to database server" netbeans google cloud sql error“无法创建与数据库服务器的连接”netbeans google cloud sql 错误
【发布时间】:2018-01-13 08:00:58
【问题描述】:

我正在尝试使用 java httpServlet 和 netbeans 创建我的第一个 API,它们将连接到基于其 examplesdocumentations 的谷歌云上的数据库。我还没有创建数据库,但我获得了必要的凭据和变量来创建连接。所以我从github下载了项目,当我尝试从netbeans打开它时,出现了一些关于依赖项的问题......所以我解决了它们,我用它们的值替换了凭据并运行项目;不幸的是,抛出了一个错误:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. 我进行了一些搜索,但没有得到任何结果......这可能是这段代码的错误吗?如果在云上不可见,数据库安全性会出错吗?任何帮助都将不胜感激。

public class ListTables {
  public static void main(String[] args) throws IOException, SQLException {

    String instanceConnectionName = "<foo:foo:bar>";

    String databaseName = "myDatabaseName ";

    String username = "myUsername ";

    String password = "<myPass>";

    if (instanceConnectionName.equals("<insert_connection_name>")) {
      System.err.println("Please update the sample to specify the instance connection name.");
      System.exit(1);
    }

    if (password.equals("<insert_password>")) {
      System.err.println("Please update the sample to specify the mysql password.");
      System.exit(1);
    }

    String jdbcUrl = String.format(
        "jdbc:mysql://google/%s?cloudSqlInstance=%s&"
            + "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
        databaseName,
        instanceConnectionName);
 try{
     Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

    /* try (Statement statement = connection.createStatement()) {
      ResultSet resultSet = statement.executeQuery("select * from wf_accounts;");
      while (resultSet.next()) {
        System.out.println(resultSet.getString(1));
      }
    }*/


 }catch(Exception ex){
    System.out.println("Error: " + ex.toString());
 }

更新

当我这样做时抛出了同样的错误:

String jdbcUrl = String.format( 
   "jdbc:mysql://google/%s?cloudSqlInstance=%s&" 
   + "socketFactory=com.google.cloud.sql.mysql.SocketFactory", 
"", 
"");

有什么提示吗?

【问题讨论】:

  • 您是否尝试关闭防火墙并尝试连接?
  • 也许this 的问题可能对您有所帮助。

标签: java mysql sql-server google-app-engine netbeans


【解决方案1】:

首先你必须检查mysql服务器是否正在运行。如果你是Windows用户,你可以简单

Go to Task Manager
Go to Services
then search for your Mysql server(eg:for my case its MYSQL56)
then you will see under the status column it says its not running
by right clicking and select start

如果它已经在运行,那么你必须按照 mysql 文档中的操作,

在您的应用程序中使用之前,您应该考虑过期和/或测试连接有效性,增加客户端超时的服务器配置值,或使用 Connector/J 连接属性

autoReconnect=true

为了避免这个问题

【讨论】:

  • 应该在哪里添加?在哪个文件中?以及为什么您建议在连接从一开始就被拒绝的情况下重新连接?
  • 我已经更新了我的答案,并在你的 Mysql 连接 URI mysql://db_user:db_user@localhost/mydb?autoReconnect=true?useUnicode=yes 中试试这个
  • 还是同样的错误,另外我告诉过你我正在尝试通过google cloud调用别人创建的数据库...
  • 这很简单,就像您添加一个字符串一样,只需像这样将它附加到 Db 连接 URI "?autoReconnect=true" 的末尾。希望这有效
  • 请参考这里。我无法用你提供的信息直接回答serverfault.com/questions/89955/…
猜你喜欢
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2014-03-06
  • 2021-02-12
相关资源
最近更新 更多