部署到云后无法访问 MySQL 数据库 - CommunicationsException
Can't access MySQL Database After deploying to the Cloud - CommunicationsException
我通过我的 App Engine Java Rest API 项目连接到我的 Google 云 SQL 以在我的云上检索和存储数据。我可以通过 Localhost 成功地与服务器通信,但是在我发布之后,我得到了这个错误:
Trying to Run Query:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.
虽然有很多解决方案,但我很遗憾地说 none 有效。
这是我的连接字符串:
String url = null;
try {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://APP_ENGINE_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
} else {
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://MY_IPV4_ADDRESS:3306/DB_NAME?user=root";
}
} catch (Exception e) {
e.printStackTrace();
}
我仅在发布后通过 Rest API 收到此错误。基本上,通过本地主机工作,但不能在云端远程工作。
我调试并指出了它崩溃的地方。它发生在我尝试与服务器建立连接并执行查询后:
try {
//THIS is where it crashes
conn = DriverManager.getConnection(url);
try {
String query = "SELECT * FROM UserTable";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
//Code
}
st.close();
} finally {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
有没有人遇到过这个问题?
如您所见,我在 Android 中调用了 asyncTask(与 java 中的 Thread 相同)
public class Connect extends AsyncTask<Context, Integer, Long>
{
protected Long doInBackground(Context... contexts) {
Connection connection;
String query = "Some query";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://<your cloud IP address>/<database schema you want to connect to>", "<user>", "<password>");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
希望你现在明白了:)
正如 Herman、Igor 和 Vadim 所指出的,这只是我在 PROJECT_ID:INSTANCE_ID 这一行中的一个简单错误:
错误代码:
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://APP_ENGINE_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
}
我一直在使用我的 App 引擎 ID,而不是我应该使用的 项目 ID。
更正代码:
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://PROJECT_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
}
我通过我的 App Engine Java Rest API 项目连接到我的 Google 云 SQL 以在我的云上检索和存储数据。我可以通过 Localhost 成功地与服务器通信,但是在我发布之后,我得到了这个错误:
Trying to Run Query: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
虽然有很多解决方案,但我很遗憾地说 none 有效。
这是我的连接字符串:
String url = null;
try {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://APP_ENGINE_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
} else {
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://MY_IPV4_ADDRESS:3306/DB_NAME?user=root";
}
} catch (Exception e) {
e.printStackTrace();
}
我仅在发布后通过 Rest API 收到此错误。基本上,通过本地主机工作,但不能在云端远程工作。
我调试并指出了它崩溃的地方。它发生在我尝试与服务器建立连接并执行查询后:
try {
//THIS is where it crashes
conn = DriverManager.getConnection(url);
try {
String query = "SELECT * FROM UserTable";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next())
{
//Code
}
st.close();
} finally {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
有没有人遇到过这个问题?
如您所见,我在 Android 中调用了 asyncTask(与 java 中的 Thread 相同)
public class Connect extends AsyncTask<Context, Integer, Long>
{
protected Long doInBackground(Context... contexts) {
Connection connection;
String query = "Some query";
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://<your cloud IP address>/<database schema you want to connect to>", "<user>", "<password>");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
希望你现在明白了:)
正如 Herman、Igor 和 Vadim 所指出的,这只是我在 PROJECT_ID:INSTANCE_ID 这一行中的一个简单错误:
错误代码:
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://APP_ENGINE_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
}
我一直在使用我的 App 引擎 ID,而不是我应该使用的 项目 ID。
更正代码:
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://PROJECT_ID:SQL_INSTANCE_NAME/DB_NAME?user=root";
}