目前我正在尝试关注此documentation以连接到我的第二代云SQL 数据库。但是,我的应用程序在启动时失败,因为它无法连接到它..
正如文档所述,我已经添加了两个服务帐户
my-project@appspot.gserviceaccount.com
1234-compute@developer.gserviceaccount.com
作为IAM下的编辑。
我正在使用Cloud SQL实例的IPv4进行连接。这里是连接字符串:
jdbc:mysql://w.x.y.z:3306/app_db?useSSL=false
但在Stackdriver Logging视图中,我看到了:
[main] ERROR com.mz.server.BootstrappingServerConfig - Error trying to migrate SQL scripts ..
org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
at org.flywaydb.core.Flyway.execute(Flyway.java:1385)
at org.flywaydb.core.Flyway.migrate(Flyway.java:1006)
...
Caused by: 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
...
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
...
两个实例都在同一个区域:计算实例为us-central1-b
,SQL实例为us-central1
。
我的 app.yaml 文件:
runtime: java
vm: true
runtime_config:
jdk: openjdk8
server: jetty9
env_variables:
'ALPN_ENABLE': 'true' # OPTIONAL
beta_settings:
cloud_sql_instances: mz-test:us-central1:mz-test-cloudsql
health_check:
enable_health_check: false
我不确定这里有什么问题。我实际上是否允许使用SQL实例的本地IPv4地址?
文档实际上是说
" 在您的应用程序代码中,通过使用以下名称打开一个套接字来连接到该实例:
/cloudsql/[INSTANCE_CONNECTION_NAME}
"
但我不确定这对连接字符串意味着什么。
我也缺少有关需要用户名/密码的信息,或者我在这里不需要用户名/密码?
为什么我无法连接?
答案 0 :(得分:2)
对于在App Engine Flexible Environment应用程序上运行的Java应用程序,请使用以下库:https://github.com/GoogleCloudPlatform/cloud-sql-mysql-socket-factory