谷歌云SQL连接

时间:2017-06-16 09:05:20

标签: mysql google-app-engine google-cloud-sql

我正在尝试连接到Google云端SQL。我几天前成功地做到了这一点,但是现在我面临着一些问题。 正在发生的问题是

正在加载课程com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver'。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。

因为它自动加载com.mysql.cj.jdbc.Driver我收到以下错误:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:676)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:638)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:606)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1837)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1653)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:662)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at com.example.endpoints.insertUser.doPost(insertUser.java:160)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:821)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:583)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1158)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1090)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:109)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
    at org.eclipse.jetty.server.Server.handle(Server.java:517)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:306)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
    at java.lang.Thread.run(Thread.java:745)

**Caused by: java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to com.mysql.cj.api.io.SocketFactory**


    at com.mysql.cj.core.io.AbstractSocketConnection.createSocketFactory(AbstractSocketConnection.java:129)
    at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:56)
    at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:144)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1783)
    ... 33 more

检查粗体部分......

我用来连接的网址是这样的: -

"jdbc:mysql://google/*****?cloudSqlInstance=******&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=****&password=****&useSSL=true"

3 个答案:

答案 0 :(得分:0)

我认为问题在于您使用mysql-socket-factorycom.google.cloud.sql.mysql.SocketFactory来实现com.mysql.jdbc.SocketFactory
试试这个:mysql-socket-factory-connector-j-6

答案 1 :(得分:0)

我遇到了一个非常相似的问题,可能有相同的根本原因

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create socket factory 'com.google.cloud.sql.mysql.SocketFactory' due to underlying exception: 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
Caused by: java.lang.ClassCastException: com.google.cloud.sql.mysql.SocketFactory cannot be cast to com.mysql.jdbc.SocketFactory
    at com.mysql.jdbc.MysqlIO.createSocketFactory(MysqlIO.java:3324)
    ... 80 more

自从我之前完成所有工作后,这很奇怪。

事实证明我的wifi连接位于错误的网络上,并且没有连接。修复网络连接后,数据库连接又恢复了。确保尝试启用和禁用wifi。是的,对于实际问题,非常令人困惑的错误。

希望这可以帮助遇到相同问题的人。

答案 2 :(得分:0)

如果您使用以下依赖项

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>*.*.*</version>
</dependency>
<dependency>
  <groupId>com.google.cloud.sql</groupId>
  <artifactId>mysql-socket-factory</artifactId>
  <version>*.*.*</version>
</dependency>

改用此入门版

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