0

My mySQL server is hosted on a ubuntu server vm @ 10.0.0.200, with an ssh port at 3333.

Attempting to start the spring boot app gives me this error:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

My application.properties looks like this:

spring.datasource.url = jdbc:mysql://10.0.0.200:3306/test?useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true
spring.datasource.username = boss
spring.datasource.password = StrongPass123!
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto= update
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect

I was thinking maybe I need to specify the ssh port for my vm ans tried:

spring.datasource.url = jdbc:mysql://{10.0.0.200:3333}:3306/test?useSSL=false&allowPublicKeyRetrieval=true&autoReconnect=true

And got the same error. I tried setting the bind address of mysql in my.cnf to my vm's IP(localhost). to no avail.

I am sure the credentials are correct, But have yet to get spring to start up right.

Error trace:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:898) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.29.jar:8.0.29]  
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.29.jar:8.0.29]
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar:na]   
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar:na]
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar:na]        
        at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) 
~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:175) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) 
~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:173) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:127) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
beatmaister
  • 375
  • 1
  • 14
  • Is this the full exception stacktrace? I usually would expect a cause ("Caused by") with - for example - an `IOException` or another exception. – Mark Rotteveel Jul 19 '22 at 07:47
  • @MarkRotteveel It isn't, the full trace had: Caused by: **java.net.ConnectException: Connection refused: no further information** – beatmaister Jul 19 '22 at 19:22
  • That error suggest that the server is not running, not running on port 3306, or not on that IP address, or not accessible from your machine due to network routing, firewalls, etc. – Mark Rotteveel Jul 20 '22 at 05:54
  • @MarkRotteveel I solved it, turns out I had an unecessary keyword in my sercives file. Solved it completely by accident. Thanks for the suggestion. – beatmaister Jul 20 '22 at 22:03

1 Answers1

0

In order to solve SQLNonTransientConnectionException error, I would first suggest using a strategy from an older stackoverflow post found here: SQLNonTransientConnectionException: No current connection, in my application while interacting with Derby database

If that does not work, then I would attempt to model your code in application.properties code after this:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

Which I found the in the application.properties properties section of https://spring.io/guides/gs/accessing-data-mysql/. So that information is straight from the source. If you can get it working with the structure from Springs standard example, then you can add the other properties one by one, build and re-run the code again prior to testing the connection.

Sometimes completely shutting down your IDE and then re-opening it helps as well.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cstax
  • 47
  • 1
  • I tried changing spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example using different combinations of my machine and vm's IPs, but I get the same error each time. I'm following the spring guide, but its not very helpful for this issue – beatmaister Jul 17 '22 at 21:15