0

I am working on a dynamic web project for class. I am trying to pull data from a MYSQL database so that I can read it and use it on my web application. When I try and connect, I get this error:

        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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at edu.neumont.csc280.controller.DBConnection.openConnection(DBConnection.java:31)
    at edu.neumont.csc280.listeners.ServiceLoader.contextInitialized(ServiceLoader.java:31)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
    ... 25 more
May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getStandardFacesConfig
INFO: Reading standard config META-INF/standard-faces-config.xml
May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig
INFO: Reading config : jar:file:/C:/xampp/tomcat/lib/openwebbeans-jsf-1.2.1.jar!/META-INF/faces-config.xml
May 22, 2014 3:47:10 AM org.apache.myfaces.config.DefaultFacesConfigurationProvider getClassloaderFacesConfig
INFO: Reading config : jar:file:/C:/xampp/tomcat/lib/openwebbeans-el22-1.2.1.jar!/META-INF/faces-config.xml
May 22, 2014 3:47:10 AM org.apache.myfaces.config.LogMetaInfUtils logArtifact
INFO: Artifact 'myfaces-api' was found in version '2.1.13' from path 'file:/C:/xampp/tomcat/lib/myfaces-api-2.1.13.jar'
May 22, 2014 3:47:10 AM org.apache.myfaces.config.LogMetaInfUtils logArtifact
INFO: Artifact 'myfaces-impl' was found in version '2.1.13' from path 'file:/C:/xampp/tomcat/lib/myfaces-impl-2.1.13.jar'
May 22, 2014 3:47:10 AM org.apache.myfaces.application.ApplicationImpl getProjectStage
INFO: Couldn't discover the current project stage, using Production
May 22, 2014 3:47:10 AM org.apache.myfaces.config.FacesConfigurator handleSerialFactory
INFO: Serialization provider : class org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory
May 22, 2014 3:47:10 AM org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider
INFO: Using LifecycleProvider org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider
May 22, 2014 3:47:10 AM org.apache.myfaces.webapp.AbstractFacesInitializer initFaces
INFO: ServletContext initialized.
May 22, 2014 3:47:10 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
May 22, 2014 3:47:10 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
May 22, 2014 3:47:10 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 22686 ms

I would like to know what possible reasons are there for this error? I know my server is indeed listening on port 3066 and I have the credentials necessary to connect. I do not understand the way tomcat7.0 is trying to communicate from the application to the MYSQL database. I have the latest MYSQL jdbc driver installed, but when I try and DriverManager.getConnection(), it times out. So I think it has something to do with the configuration. This is my config:

String url = "jdbc:mysql://myserver.com:3306/trivitup"; String user = user"; String pw = "pass";

I typed in netstat -a on my server to see what ports it was listening to

I did netstat -a and this is what I got:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 *:mysql                     *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0    320 ip-172-31-2-171.us-west:ssh c-76-27-90-190.hsd1.u:31050 ESTABLISHED
tcp        0      0 *:http                      *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0      0 *:https                     *:*                         LISTEN
udp        0      0 *:bootpc                    *:*
udp        0      0 ip-172-31-2-171.us-west-:ntp *:*
udp        0      0 localhost:ntp               *:*
udp        0      0 *:ntp                       *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     8268   /var/lib/mysql/mysql.sock
unix  7      [ ]         DGRAM                    6704   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     5745   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     6761   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     5947   @/org/kernel/udev/udevd
unix  3      [ ]         STREAM     CONNECTED     6770   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     6765
unix  3      [ ]         DGRAM                    5955
unix  2      [ ]         DGRAM                    7262
unix  2      [ ]         DGRAM                    7166
unix  3      [ ]         STREAM     CONNECTED     8455
unix  2      [ ]         DGRAM                    7325
unix  2      [ ]         DGRAM                    7243
unix  3      [ ]         DGRAM                    5954
unix  2      [ ]         DGRAM                    8451
unix  3      [ ]         STREAM     CONNECTED     8456
unix  3      [ ]         STREAM     CONNECTED     6769
unix  3      [ ]         STREAM     CONNECTED     6764

Thanks for reading

courtyen
  • 492
  • 3
  • 8
  • 23
  • Is the port open on the firewall on the server? Seems like a network error. – user432 May 22 '14 at 07:36
  • can you try telnet the server from command line first? ive seen this error when the hosted tomcat couldnt connect to the database port. – Gabriel May 22 '14 at 07:39
  • Is mysql configured that it is accessible from remote? (bind-address not localhost)? – Jens May 22 '14 at 07:40
  • telnet myserver.com 3306 And tell us what you see? – Gabriel May 22 '14 at 07:41
  • This might be of some help to you http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – Sanjeev May 22 '14 at 07:49
  • @user432 I ran the command telnet myserver.com:3306 and I got this response: "Name or service not known. myserver.com:3306: Unknown host" – courtyen May 22 '14 at 08:11
  • @Jens I am hosting the MySQL database on amazon ec2 and I am listening on 0.0.0 – courtyen May 22 '14 at 08:27
  • Try it with the ip-adress. What is the result? – Jens May 22 '14 at 08:29
  • @Jens same as domain, unknown. My server is listening, and my application is requesting.. Does this mean my request is wrongly configured? Or is my host not detectable? – courtyen May 22 '14 at 08:58
  • @CYen Then it looks like a network problem. – Jens May 22 '14 at 09:00

5 Answers5

0

You can try somethings like this.

Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.

connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb","root","password");

// If above line will executed successfully then you are connected to database

After that you can fire query.

more info

http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
0

If your server is listening on port 3066 then your JDBC url is wrong. JDBC url will be with port 3066 not 3306.

String url = "jdbc:mysql://myserver.com:3066/trivitup"; 
String user = user"; 
String pw = "pass";
Mamun
  • 512
  • 3
  • 10
  • I am pretty sure default mysql port is 3306. http://dev.mysql.com/doc/refman/5.1/en/connecting.html – courtyen May 22 '14 at 08:20
0

To verify the port, perform the following steps:

  1. Execute the winmysqladmin.exe file; it's available in the mysql\bin folder. I'm assuming that you are working on Windows.
  2. From the pop-up window select the Variables tab.
  3. In this tab, under the Variable name column search for an entry called port. The value against this indicates the port number used by mySQL.

Either you are using wrong port no in url or mySQL is not listening on that port no or mySQL server is not started at all.

For full discussion look at How to tell which port mySQL is running on?

Have a look tutorial on Establishing a Connection


You can try in this way as well. Usually mysql runs on port as default 3306.

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wrox?user=xxx&password=xxxxxxxx");
Braj
  • 46,415
  • 5
  • 60
  • 76
0

The MySQL jdbc url is parsed like this:

jdbc:mysql://[hostname]:3066/[dbname]

Are you hosting your MySQL server from myserver.com? e.g., if you are hosting the server locally, then the hostname is localhost.

You can check out this JDBC tutorial for information on further configuring the url.

jzheaux
  • 7,042
  • 3
  • 22
  • 36
0

I found the answer!!! It was SUPER SIMPLE!!! I am using Amazon ec2 default linux distribution.. I had my security group open to :mysql BUT my source was set to myself, and not everyone. So despite being able to open and listening, my server wasn't listening to 0.0.0.0.

I went to the security group on ec2 and changed the mysql's source to 0.0.0.0 and not the default ec2.webserveruser. Thanks everyone :)

courtyen
  • 492
  • 3
  • 8
  • 23