I have a Java application running Win 7 connecting to MySQL on the same host. Eclipse IDE shows the following error every time after 8-10mins.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1070)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:348)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at iProclassHandlers.IProclass_Info_DAO.conn2SQL(IProclass_Info_DAO.java:151)
at iProclassHandlers.IProclass_Info_DAO.toString(IProclass_Info_DAO.java:140)
at iProclassParsers.IProclass_Info_Parser.printData(IProclass_Info_Parser.java:67)
at iProclassParsers.IProclass_Info_Parser.runParser(IProclass_Info_Parser.java:38)
at iProclassParsers.IProclass_main.main(IProclass_main.java:19)
Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure
This reported MySQL bug might explain it.
Try these:
Tomcat6 can't connect to MySql (The driver has not received any packets from the server)
https://serverfault.com/questions/89955/unable-to-connect-to-mysql-through-jdbc-connector-through-tomcat-or-externally
Can't make JDBC connection to MySQL (using Java, IntelliJ, and Linux)
Related
Yesterday I spent several hours trying to solve this problem.
When I try to connect to MySQL server from my Java program I get the CommunicationsException: Communications link failure.
I have encountered several questions on Stackoverflow with similar problems but none of the solutions that were provided in the answers worked for me. For instance I have read an answer which advises to replace "localhost" with "127.0.0.1" or with my private IP address in the Driver.getConnection method, but that didn't solve the problem.I have read an answer which suggested that it should be checked whether the server is running. So, I typed sudo service mysql status and I saw that the server was active and operational. I have read an answer telling me to add the line Class.forName("com.mysql.jdbc.Driver"); so I did that but that didn't help either.
Here is my Java code
package com.mainpackage;
import java.sql.*;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1?user=userName&passwword=userPassword");
}
}
Here is the entire message from the terminal that I have received:
java -classpath .:/home/javaUtil/MySQL/mysql-connector.jar com/mainpackage/Main
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Exception in thread "main" com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
at com.mainpackage.Main.main(Main.java:9)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
at com.mysql.cj.NativeSession.connect(NativeSession.java:120)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
... 6 more
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:579)
at java.base/sun.nio.ch.Net.connect(Net.java:568)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:633)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
... 9 more
Edit: I don't have big enough reputation to respond in the comment, so I will add additional information here. I am able to connect to the server from terminal by writting mysql -u username -p. Everything works fine then.
I have found the solution to my problem and I want to share the solution in case someone else faces the same problem. As Jon Skeet suggested in his comment, the server, for whatever reason, wasn't listening on port 3306.
I confirmed this by logging into the mysql server via terminal (mysql -u root -p) and then I wrote
SHOW GLOBAL VARIABLES LIKE 'PORT';
It said that port was 0 instead of 3306. Here is how I changed the port.
I went to /etc/mysql/mysql.conf.d/
then I edited the file which is named mysqld.cnf.
On my PC, that file was a read-only file, so I needed to first change the permissions.
I wrote
sudo chmod +rw mysqld.cnf in the terminal while I was in the /etc/mysql/mysql.conf.d/ directory.
After that, I edited the file by typing sudo gedit mysqld.cnf
in the terminal.
In the file, I added
port = 3306 in a new line, below [mysqd], so that it looks like this
[mysqld]
port = 3306
I restarted the mysql server by writting sudo service mysql restart in terminal.
Just as a quick note, if you want to run the server with skip-grant-tables option, this is where you add skip-grant-tables, you add it on the same place that I added port = 3306.
This question already has answers here:
Connect aws ec2 mysql with my local java application [duplicate]
(1 answer)
Solving a "communications link failure" with JDBC and MySQL [duplicate]
(25 answers)
Closed 1 year ago.
I have created a Ubuntu ec2 instance in AWS and installed the MySQL server. I've associated the instance with an elastic IP. I'm able to access the MySQL server with this address http://x.x.xxx.xx/phpmyadmin x.x.xxx.xx is my public elastic IP address. I can access the MySQL database with my MySQL workbench from my local machine. But I cant access it with my local java application.
This is my java connection code
conn = DriverManager.getConnection("jdbc:mysql://ec2-x-x-xxx-xx.ap-south-1.compute.amazonaws.com:3306/userDB",
"root", "password");
I'm getting the following error
Connecting to database...
com.mysql.cj.jdbc.exceptions.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 mysql.connector.java#8.0.23/com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at firstJDBCProgram.main(firstJDBCProgram.java:20)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at mysql.connector.java#8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at mysql.connector.java#8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at mysql.connector.java#8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at mysql.connector.java#8.0.23/com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at mysql.connector.java#8.0.23/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
at mysql.connector.java#8.0.23/com.mysql.cj.NativeSession.connect(NativeSession.java:144)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:953)
at mysql.connector.java#8.0.23/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:823)
... 6 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.base/sun.nio.ch.Net.connect0(Native Method)
at java.base/sun.nio.ch.Net.connect(Net.java:574)
at java.base/sun.nio.ch.Net.connect(Net.java:563)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
at java.base/java.net.Socket.connect(Socket.java:648)
at mysql.connector.java#8.0.23/com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
at mysql.connector.java#8.0.23/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
... 9 more
I’ve been receiving the following error:
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:be8d9e1d-fff7-4310-ae77-03394c83f86b".
(More of the error at bottom of post)
When trying to connect to a MSSQL database.
I’m using the following connection string:
jdbc:sqlserver://192.168.100.190:1433;databaseName=myDatabase;user=validUser;password=validPassword;encrypt=false;trustServerCertificate=false;sslProtocol=TLSv1;
I’ve tried variations of the connection string, essentially changing and omitting:
encrypt=false;trustServerCertificate=false;sslProtocol=TLSv1;
I’ve tried different versions of the driver from:
https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017
all with the same results.
It is important to note that on some PC’s the application connects
and works as expected but I have so far found no real differences
between the PC’s or user accounts, no pattern really.
I’ve tried different versions of the JDK and different JRE’s, all
give the same results.
UPDATE: I have since found JRE 1.8.0_152 works in all my test cases
I’ve tried looking at logs on the servers and there are no entries in
the SQL logs.
If I try a connection string with the wrong credentials, the error is
exactly the same.
I’ve setup a test server and database, this works fine, as does one I
setup in a Virtual Machine.
I can connect to said database using Navicat for example and have
another application, writing in Visual Basic.Net that connects fine,
so it would see that it is just something with this driver or java,
but as I say, it does work on some PC’s and not others and of course
works as expected in my test environment.
Any thoughts or suggestions would be greatly appreciated.
I’ve not provided any code, as I can replicate the exact same results using the example code provided here:
https://learn.microsoft.com/en-us/sql/connect/jdbc/step-3-proof-of-concept-connecting-to-sql-using-java?view=sql-server-2017
More detail on error:
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Server did not return a response. The connection has been closed. ClientConnectionId:be8d9e1d-fff7-4310-ae77-03394c83f86b".
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2670)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1837)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2257)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1921)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1762)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1077)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:623)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.DataBase.openConnection(DataBase.java:122)
at jambuddylite.jblCoord.readSimex(jblCoord.java:387)
at jambuddylite.jblCoord$2.run(jblCoord.java:314)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: SQL Server did not return a response. The connection has been closed. ClientConnectionId:be8d9e1d-fff7-4310-ae77-03394c83f86b
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.ensureSSLPayload(IOBuffer.java:780)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.readInternal(IOBuffer.java:836)
at com.microsoft.sqlserver.jdbc.TDSChannel$SSLHandshakeInputStream.read(IOBuffer.java:827)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.readInternal(IOBuffer.java:1009)
at com.microsoft.sqlserver.jdbc.TDSChannel$ProxyInputStream.read(IOBuffer.java:997)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.microsoft.sqlserver.jdbc.TDSChannel.enableSSL(IOBuffer.java:1767)
... 11 more
check the port is open or not using on cmd or terminal
telnet 192.168.100.190 1433
and check the database name and credentials
and firewall there is some thing in firewall i think
and take a look here may help you
I'm trying to connect a Java application to a Redshift database. When I run DriverManager.getConnection(), it sits for a long time (minutes) and finally throws an exception:
Exception in thread "main" java.sql.SQLException:
[Amazon](600001) The server closed the connection.
at com.amazon.support.channels.TLSSocketChannel.readBytes(Unknown Source)
at com.amazon.support.channels.TLSSocketChannel.doHandshake(Unknown Source)
at com.amazon.support.channels.TLSSocketChannel.<init>(Unknown Source)
at com.amazon.redshift.client.PGClient.checkSSL(Unknown Source)
at com.amazon.redshift.client.PGClient.<init>(Unknown Source)
at com.amazon.redshift.core.PGJDBCConnection.connect(Unknown Source)
at com.amazon.jdbc.common.BaseConnectionFactory.doConnect(Unknown Source)
at com.amazon.jdbc.common.AbstractDriver.connect(Unknown Source)
at com.amazon.redshift.jdbc.Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
I can connect using the same connection string and credentials from SQL Workbench on the same machine. Also, if I supply bad credentials to the connection, it gives an authentication error. So I don't believe this is an Amazon security issue, which seems to be the most common reason for an inability to connect.
Other ideas?
Update: The mystery deepens. Other members of the team are able to check out the code and run it successfully. We have the driver in our team's shared maven repository.
Well... not sure what the problem was. But I did an Eclipse update and it went away.
I am trying to do this tutorial and when I got to the hibernate part, I get this errors:
java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataS
ource.java:146)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(Wrap
perConnectionPoolDataSource.java:195)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(Wrap
perConnectionPoolDataSource.java:184)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourceP
oolManager.acquireResource(C3P0PooledConnectionPool.java:200)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.ja
va:1086)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAc
quiresWithinLockOnSuccess(BasicResourcePool.java:1073)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.j
ava:44)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(Basic
ResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolA
synchronousRunner.java:648)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.jav
a:288)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
... 10 more
Here is the hibernate.cfg.xml part of accessing the database:
<!--Database connection settings-->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databaseName=MyTut1db</property>
<property name="connection.username">sop1</property>
<property name="connection.password">myPW1</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
The user sop1 was created in MS SQL Server 2012 Express edition as:
CREATE LOGIN sop1
WITH PASSWORD = 'myPW1';
USE MyTut1db;
GO
CREATE USER sop1 FOR LOGIN sop1;
GO
The database properties are in the figure below:
I have inabled the TCP-IP from MS SQL Server Configuration Manager (TCP Port is 1433), but the problem still persists.
I have also closed the antivirus for a while.
I am asking for some help: What else can it be? How do I verify the user? How do I know that localhost:1433 is the right server?
I would check things following these steps:
Ensure that SQL Server is up and running. Check your running services to see if SQL Server service is up.
I would check the SQL Server Configuration Manager to see if the TCP/IP communication is enabled.
I would check my firewall settings. Perhaps something is in the way between SQL Server and the client.
I would check if another application from the same client can connect to the same server. Then I would check the connection string differences.
If all of the above were playing correctly, then I would check the privileges on the specific SQL Server, on the specific database, for the account I am trying to connect with. Allow everyone to use this database as a first step to check if it is an account-permissions problem.
Hope I helped!
localhost will always be the server your Java is running on - it's saying "Talk back to the same machine".
If your database is hosted on a different server, you need to put that server name in the configuration.
Please the check the following
Click Start >Programs -> Microsoft Sql Server 2008 R2 -> Configuration Tools -> Sql Server Configuration Manager
Click Sql Server Network Configuration
Click Protocols for SQLExpress
Double click TCP/IP and click IP adrress tab
Go to the end till you see IP ALL
Ensure that you see port number 1433 is configured under TCP Port under IPALL header.
If not configured, enter 1433 port and click Apply.
You need to restart the MS sql server and try now and it should work.
Hope this will solve it.