MySQL : Communication Link failure error and telnet localhost 3306 : Connection close
Im trying to connect MYSQL db using Java where my SQL db is on localhost. Im facing some issues regarding communication link failure.
public testfile() throws ClassNotFoundException, SQLException{
Connection connection = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/rts?user=root&password=password");
System.out.println("Connected");
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
if(connection != null)
connection.close();
}
}
Stack trace :
com.m开发者_C百科ysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException
MESSAGE: Connection refused
STACKTRACE:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at java.net.Socket.<init>(Socket.java:392)
at java.net.Socket.<init>(Socket.java:235)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:222)
at test.testfile.<init>(testfile.java:18)
at test.mainfile.main(mainfile.java:9)
** END NESTED EXCEPTION **
Last packet sent to the server was 1 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:222)
at test.testfile.<init>(testfile.java:18)
at test.mainfile.main(mainfile.java:9)
Telnet output :
telnet localhost 3306
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
Package/Software Info :
- Eclipse Java EE IDE for Web Developers.
- Version: Helios Service Release 1.
- JDK6
- Connector/J version 5.0.8
- Ubuntu : Ubuntu 10.04 LTS- the Lucid Lynx
I have tried few things but it doesn't work:
I changed port in my.cnf but no effect, I changed to 3312 same err repeats and telnet shows as follows :
shreya@shreya-laptop:~$ telnet localhost 3306 Trying 127.0.0.1... telnet: Unable to connect to remote host: Connection refused shreya@shreya-laptop:~$ telnet localhost 3312 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host.
nmap output :
shreya@shreya-laptop:~$ nmap localhost -p 3306 Starting Nmap 5.00 ( [url]http://nmap.org[/url] ) at 2011-03-31 09:45 IST Interesting ports on localhost (127.0.0.1): PORT STATE SERVICE 3306/tcp closed mysql Nmap done: 1 IP address (1 host up) scanned in 0.11 seconds shreya@shreya-laptop:~$ nmap localhost -p 3312 Starting Nmap 5.00 ( [url]http://nmap.org[/url] ) at 2011-03-31 09:45 IST Interesting ports on localhost (127.0.0.1): PORT STATE SERVICE 3312/tcp open unknown Nmap done: 1 IP address (1 host up) scanned in 0.06 seconds
I tried putting entry in hosts.allow but no effect
I have tried reinstalling mysql. Insertion of data through mysql prompt and c program works perfectly but not through java code.
I tried adding rules in ip tables still it doesnt help
I have tried different combinations of url string. I have also tried different versions of eclipse and Connector/J jar file(latest 5.1.15)
First try to connect to MySQL using MYSQL Command Line Client
using same credentials used in the program.
java.net.ConnectException: Connection refused
There is simply nothing which listens/accepts connections on the given host/port. Verify the following:
- Verify in
my.cnf
if port number is indeed 3306. - Verify if the DB is started (use MySQL DB admin tool).
- Verify if DB accepts TCP/IP connections,
mysqld
shouldn't have--skip-networking
option. - Verify if the DB hasn't run out of connections, if necessary restart it and fix all existing Java code accordingly that it properly closes the connection in
finally
block. - Verify if there isn't a firewall/proxy in between which blocks connections on the given port.
You need to grant permission to the user connecting over port 3306... This guy has a good explanation: link
精彩评论