Mysql Tomcat 6 Connection refused - mysql

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

Related

Unable to connect to mysql in AWS

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

Setting up a JDBC data source and connection pool

I have followed every step in the tutorial: https://netbeans.org/kb/docs/web/mysql-webapp.html but the data source and connection pool are not displayed on the Server Resources node.
after completing all the other steps and deploying the project, i get the following error...
exception
javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/IFPWAFCAD"
root cause
javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver found for jdbc/IFPWAFCAD"

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

Can't access mysql database (running on AWS instance) from my java application

I have a xampp server running on a AWS-EC2 Ubuntu instance, and I have a database there that is called "androiddb". I can access the database by writing in a navigator http://public_ip/phpmyadmin and entering with "root" user and no password.
In another side I have a Java application that needs to connect with that database, and I have the following line in my code to do it:
con = DriverManager.getConnection("jdbc:mysql://public_ip/androiddb",
"root", "");
When I run my application, I get the following error:
Error: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused
STACKTRACE:
java.net.SocketException: java.net.ConnectException: Connection refused
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2569)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at Presentacion.Presentacion$2.mouseClicked(Presentacion.java:125)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
** END NESTED EXCEPTION **
Last packet sent to the server was 19 ms ago.
Also I looked for the port where mysql was running and I used the code:
SHOW VARIABLES
WHERE Variable_name IN (
'hostname',
'port')
I write this piece of code in phpmyadmin in the SQL section, and I get the following output:
Variable_name Value
hostname ip-172-31-23-224
port 0
I don't know what does port 0 mean, it should be 3306 (default mysql port)...
Anyways, I don't know if I'm doing something wrong with the xampp configuration or if it's problem of AWS or anything else.
Could someone help me with this? Thank you very much.
You should ensure your security groups for AWS are open on the 3306 port (or whatever you configure) so they can access the database.
You could trouble shoot by trying to SSH into the machine, can you connect locally? If yes, then look at your security groups and open the port. If you can't connect locally than the database is down and you need to diagnose and configure the DB correctly.
But a connection refused is more than likely a port not being open in the security group.
Just to add what solved his issue:
He checked his security groups and opened the correct ports
Checked locally to see that mySQL wasn't listening on any port
netstat -tlnp to check to see SQL running locally
Checked the my.ini file to see that mysql was configured to listen on a port
Found it wasn't and reconfigured it to listen
It may help some working with phpMyAdmin and AWS and Amazon's relational database service (RDS) to know that phpMyAdmin will not work unless the RDS endpoint is edited into the "localhost" or "127.0.0.1" location in the config.inc.php file. (It is a necessary but not sufficient fix -- you may need to do other things...)
Generalizing a bit, if you are using foo and it has a config file, and you are looking to connect to a database instantiated under RDS, you'll need that endpoint as host entry.

Grails and MySQL Connection Exception

I have a grails application that is in production now. This morning I was alerted to the fact that the server was not resolving. Tomcat was spinning and spinning. I researched and it looks like it has to do with MySQL causing the connection to timeout after 8 hours of inactivity. I have found examples on stackoverflow of people having similar problems. However, all of these people mention that if they hit the server again and the connection is refreshed. For me, the site was down entirely and Tomcat wouldn't respond. Does it sound like something else could be at play here?
Last Exception in Tomcat log
2011-Aug-30 23:58:43,283 [TP-Processor19] org.hibernate.util.JDBCExceptionReporter
ERROR The last packet successfully received from the server was 37,118,147 milliseconds ago. The last packet sent successfully to the server was 37,122,138 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.
2011-Aug-30 23:58:43,290 [TP-Processor19] org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver
ERROR Exception occurred when processing request: [GET] /picks/ncaafb
Stacktrace follows:
java.net.SocketException: Connection timed out
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3302)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1940)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2275)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at sportsdb.Season.getCurrentNCAAFootballSeason(Season.groovy:93)
at PicksController$_closure2.doCall(PicksController.groovy:60)
at PicksController$_closure2.doCall(PicksController.groovy)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:774)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:896)
at java.lang.Thread.run(Thread.java:662)
2011-Aug-30 23:58:43,315 [TP-Processor19] org.hibernate.util.JDBCExceptionReporter
ERROR Already closed.
2011-Aug-30 23:58:43,315 [TP-Processor19] org.hibernate.util.JDBCExceptionReporter
ERROR Already closed.
2011-Aug-30 23:58:43,316 [TP-Processor19] org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet
ERROR HandlerInterceptor.afterCompletion threw exception
org.hibernate.exception.GenericJDBCException: Cannot release connection
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:774)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:896)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:114)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
at $Proxy7.close(Unknown Source)
... 6 more
My plan is to implement the solution mentioned in the link above, but I wanted to make sure nothing else visibly fishy was going on since we have a somewhat different result (their connections are refreshing and mine are not).
If you're using a Tomcat JNDI DataSource look at some of the parameters you can set on the datasource such as testOnBorrow. If validation fails the connection will be dropped from the pool. There is some performance overhead that you will incur by testing connections, but it should fix problems like this. If you have minIdle/maxIdle set high that would explain why you continue to experience the problem while reconnecting fixes it for other people.