Unable to connect to mysql in AWS - mysql

My spring boot application is throwing error when I run the jar in AWS EC2 instance. It works fine when I run in my local system.
My application.properties file is
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/gbi? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=fa lse&serverTimezone=UTC&autoReconnect=true&failOverReadOnly=false&maxReconne cts=10
spring.jpa.database-platform =org.hibernate.dialect.MySQLDialect
spring.datasource.username=******
spring.datasource.password=******#
spring.datasource.pool.size=20
spring.jpa.hibernate.ddl-auto=update
server.port=8090
When I run the jar in AWS server, I get the following error
Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server. Attempted reconnect 10
times. Giving up.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
followed by,
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)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
followed by,
Caused by: java.net.ConnectException: Connection refused (Connection
refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
atjava.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
followed by,
2017-Oct-09 15:24:49.735 WARN [main] o.s.b.a.o.j.DatabaseLookup -
Unable to determine jdbc url from datasource
org.springframework.jdbc.support.MetaDataAccessException: Could not get
Connection for extracting meta data; nested exception is
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get
JDBC Connection; nested exception is
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server. Attempted reconnect 10
times. Giving up.
How to resolve this?

Got to Ec2 and click on Security Groups then in the bottom add a new security group with type as MYSQL/Aurora protocal as TCP and select the port number and then in source select your computer's Ip Address.
You should be able to resolve your issue.
Thanks,
Madhu

Related

Percona connection fails after timeout

We have multimodule project with database, and sometimes we recieve this exception:
org.springframework.dao.DataAccessResourceFailureException: PreparedStatementCallback; SQL [SELECT data FROM table]; No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:79)
...
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at sun.reflect.GeneratedConstructorAccessor108.newInstance(Unknown Source)
... 87 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 51,072,384 milliseconds ago. The last packet sent successfully to the server was 51,072,384 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
at sun.reflect.GeneratedConstructorAccessor89.newInstance(Unknown Source)
... 78 common frames omitted
Caused by: java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
... 83 common frames omitted
Google suggested that it could be because of database closes connections when no queries in time specified in wait_timeout variable.
So I reprodused this situation in that way:
Use Percona server 5.7.16, SpringBoot 1.3.3
Set timeouts in mysql to demonstrate problem (in production sets default value 8 hours):
SET GLOBAL interactive_timeout = 15;
SET GLOBAL wait_timeout = 15;
Set heartbeat for mysql in springboot properties:
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle = true
Run test:
#Test
#DatabaseSetup("/....xml")
public void test() throws Exception {
mockMvc
.perform(get("/request_that_fetches_data_from_db"))
.andExpect(status().isOk());
sleep(1000 * 60);
mockMvc
.perform(get("/request_that_fetches_data_from_db"))
.andExpect(status().isOk());
}
Result is:
java.sql.SQLException: Could not retrieve transation read-only status server
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 40 414 milliseconds ago. The last packet sent successfully to the server was 1 milliseconds ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2957)
... 46 more
I looked at mysql general log. There are query SELECT 1 passes every 10 seconds, but when after that recieve business query, then straight recieve command quit. Error log was empty for testing period.
What is wrong and how to fix this problem without increasing wait_timeout?
P.S.jdbc:mysql://url?autoReconnect=true don't works too.

Spring Boot Service fail to run after startup because MySQL Database is not ready yet

I have installed a Spring Boot application at my Ubuntu 16 server as an init.d service to run at system startup automatically
as described here.
I have also MySQL Database which I need for Spring Boot app. The problem is, application crashes after Spring boot tries to connect to db:
2016-09-21 12:54:32.382 ERROR 1384 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
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) ~[na:1.8.0_101]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_101]
...
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_101]
...
2016-09-21 12:54:32.384 WARN 1384 --- [ main] o.h.e.jdbc.internal.JdbcServicesImpl : HHH000342: Could not obtain connection to query metadata : Communications link failure
I think, this because MySQL database is not ready yet. But this causes complete fail of Spring Boot App.
When I try to start the same service manually again, everything works perfectly.
So, my question is, can I define some kind of init.d dependencies? So my Spring boot app waits until database is ready?
I'm using systemd.
In your link, in the part:
[Unit]
Description=myapp
After=mysql.service
[Service]
User=myapp
ExecStart=/var/myapp/myapp.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
Change the After line like I show, replacing syslog by mysql.

Cannot connect to Second Generation Cloud SQL database

At the moment I am trying to follow this documentation in order to connect to my Second Generation Cloud SQL database. However, my application fails on startup since it cannot connect to it ..
As been told by the documentation, I have added both service accounts
my-project#appspot.gserviceaccount.com
1234-compute#developer.gserviceaccount.com
as Editor under IAM.
I am using the IPv4 of the Cloud SQL instance to connect. Here the connection string:
jdbc:mysql://w.x.y.z:3306/app_db?useSSL=false
but in the Stackdriver Logging view I see:
[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)
...
Both instances are in the same region: us-central1-b for the compute instances and us-central1 for the SQL instances.
My app.yaml file:
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
I am not sure what's wrong here. Am I actually allowed to use the local IPv4 address of the SQL instance?
The documentation actually says
"In your application code, connect to the instance by opening a socket with the following name: /cloudsql/[INSTANCE_CONNECTION_NAME}"
But I am not sure what that would mean for the connection string.
I am also missing the information about what username/password is required or do I not need one here?
Why am I not able to connect?
For Java applications running on App Engine Flexible Environment applications use the following library: https://github.com/GoogleCloudPlatform/cloud-sql-mysql-socket-factory

How do I access MySQL from CloudBees Jenkins?

I cannot figure out how to access a MySQL database while running in Jenkins CloudBees. I have tried following the Dev#Cloud MySQL, but there are a lot of warnings in the console output and I have to deal with localhost access vs. sockets, which is pretty tricky to debug without a command line.
Is there a way to use the CloudBees MySQL hosting? When I try and use the cloudbees JDBC driver, I get ClassNotFound error messages.
Update: The answer below clarified that I cannot use the CloudBees driver, so I am now using the regular MySQL connector through the c3p0 connection pool. Based on comments below, I have limited the number of connections in the pool to 10.
The exact same configuration and Maven command line works when I run it from my development computer, connecting to the Cloudbees Mysql instance.
Unfortunately, I get this error when trying to connect:
Caused by: com.googlecode.flyway.core.exception.FlywayException: Unable to obtain Jdbc connection from DataSource
at com.googlecode.flyway.core.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:56)
at com.googlecode.flyway.core.Flyway.execute(Flyway.java:864)
at com.googlecode.flyway.core.Flyway.migrate(Flyway.java:591)
at com.assuredlabor.db.DatabaseMigrator.migrate(DatabaseMigrator.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
... 62 more
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at com.googlecode.flyway.core.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50)
... 72 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 74 more
Any ideas would be appreciated.
You can't use CloudBees JDBC driver from outside the RUN#Cloud container, but you can use a standard MySQL JDBC driver with your RUN database using the host name and port that you can find on RUN#Cloud web console for your DB resource.

Mysql Tomcat 6 Connection refused

I have a tomcat app running on a ubuntu cloud server. Everything was working well until a few hours ago. I tried to add a second tomcat app with a similar data source resource in the context.xml. The idea was that the first app would connect to the first DB and the second to the second Database. I called the datasources the same name. It didnt work.
Now I have removed the second app and I cant connect to the database from the original app. All with the same context.xml as it was before I started today. This is driving me crazy.
I get this exception.
Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Server connection failure during transaction. Due to underlying exception: 'java.net.ConnectException: Connection refused'.
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused