无法将Spring Boot Mysql连接到GCP云

时间:2020-05-04 05:12:44

标签: mysql spring-boot google-cloud-platform spring-cloud-gcp

我在GCP云帐户中创建了SQL实例,如果我在本地运行,它可以正常工作。但是将Spring应用程序部署到GCP后会抛出错误。

错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1796)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:595)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton (DefaultSingletonBeanRegistry.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean (AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean (AbstractApplicationContext.java:1108)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization (AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh (ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh (SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext (SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run (SpringApplication.java:1215)
at inner.stories.StoriesApplication.main (StoriesApplication.java:10)

application.properties:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql: **private-IP-of-sql-instance**
spring.datasource.username=*****
spring.datasource.password=*****
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update

pom.xml:

         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>


       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql</artifactId>
            <version>1.0.0.BUILD-SNAPSHOT</version>
        </dependency>

更新

我在gcloud命令日志中看到了不同的错误。我的页面加载了一半,然后在gcloud命令日志中显示了SocketTimeoutException。

一段时间后我的主页

 Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.

Gcloud命令日志:

    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2189) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2222) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2017) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.3.jar!/:na]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:43) ~[hibernate-core-5.4.14.Final.jar!/:5.4.14.Final]
2020-05-04 21:34:35 default[20200504t172700]    ... 43 common frames omitted
2020-05-04 21:34:35 default[20200504t172700]  Caused by: java.net.SocketTimeoutException: connect timed out
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300) ~[mysql-connector-java-5.1.44.jar!/:5.1.44]
2020-05-04 21:34:35 default[20200504t172700]    ... 65 common frames omitted

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您的问题可能位于属性文件上:

def fetch_learner_data_from_combo(self):
    new = str("self.stream_combo.currentText()")
    connection= sqlite3.connect("mydb.db")
    query = ("SELECT * FROM learner_data WHERE STREAM =   new")
    result = connection.execute(query)
    self.marks_table.setRowCount(0)

    for row_number,row_data in enumerate(result):
        self.marks_table.insertRow(row_number)
        for column_number,data in enumerate(row_data):
            self.marks_table.setItem(row_number,column_number,QTableWidgetItem(str(data)))
    connection.commit()
    connection.close()

您可以查看Google's community教程,并在“创建JDBC URL”上的Google Cloud Platform's Github页面上:

spring.datasource.url=jdbc:mysql://google/myBlogcloudSqlInstance=reviewcontainerblog&socketFactory=com.google.cloud.sql.mysql.SocketFactory

因此,它是实例连接名称,而不是实例名称。您还可以查看以下documentation from Google Community教程,这些教程可以帮助您快速完成。

您还可以按照this external article进行说明,其中说明了如何以不同方式从Spring应用程序连接到CloudSQL。

请告诉我它是否适合您。

答案 1 :(得分:0)

据我从您的应用程序属性中了解到,您正在尝试通过其Pivate IP访问实例。根据{{​​3}}:

如果要使用私有IP地址,请参阅配置无服务器 VPC使用专用IP访问并直接连接。

请按照文档中所述的步骤来设置documentation,然后按照Serverless VPC Access中所述的步骤来通过其私有IP连接到您的实例。

希望有帮助。