У меня создан экземпляр Cloud Sql, который работает. Сейчас я немного пытался подключиться к нему через Android Studio и Eclipse, и мне не удалось в обоих местах.
package com.example.testapplication3;
import java.sql.*;
public class ConnectToSql {
public String run()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String instanceConnectionName = "TheActualInstanceName";
String databaseName = "BudgetApp";
String IP_of_instance = "35.******";
String username = "root";
String password = "********";
String jdbcUrl = String.format("jdbc:mysql://%s/%s?cloudSqlInstance=%s" +
"&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
IP_of_instance, databaseName, instanceConnectionName);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
Statement stat = connection.createStatement();
ResultSet res = stat.executeQuery("Select * from users;");
String result = "";
while (res.next())
{
result += (res.getString(1) + " " + res.getString(2) + " " + res.getString(3));
}
connection.close();
return result;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
}
Я постоянно получаю эту ошибку:
java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on 35.*******:3,306.
Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.
Что я сделал:
- Убедитесь, что мой IP-адрес внесен в белый список на вкладке Соединения с экземпляром
- Различное форматирование для URL-соединения
- Убедитесь, что серверuping by pinging
- ТОЛЬКО подключен к Cloud Shell
- Подтвержденный порт открыт на конце Cloud