2014-05-22 74 views
0

我正在開發一個類的動態web項目。我試圖從MYSQL數據庫中提取數據,以便我可以讀取它並在我的Web應用程序中使用它。當我嘗試連接時,出現此錯誤:如何使用jdbc和tomcat連接到MySQL數據庫

 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 

我想知道可能出現此錯誤的原因是什麼?我知道我的服務器確實在端口3066上偵聽,並且我有連接所需的憑據。我不明白tomcat7.0試圖從應用程序傳遞到MYSQL數據庫的方式。我安裝了最新的MYSQL jdbc驅動程序,但是當我嘗試和DriverManager.getConnection()時,它超時。所以我認爲這與配置有關。這是我的配置:

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

我在netstat的 - 輸入我的服務器上,看它是聽

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 

感謝哪些端口讀取

+0

端口是否在服務器上的防火牆上打開?看起來像一個網絡錯誤。 – user432

+0

你能嘗試從命令行首先telnet服務器嗎?如果託管的tomcat無法連接到數據庫端口,則看到此錯誤。 – Gabriel

+0

是否配置了mysql,它可以從遠程訪問? (綁定地址不是本地主機)? – Jens

回答

0

我找到了答案!這是超級簡單!我正在使用亞馬遜EC2默認的Linux發行版..我有我的安全組打開:mysql但我的源被設置爲我自己,而不是所有人。所以,儘管能夠打開和聆聽,但我的服務器並未聽取0.0.0.0。

我去了ec2上的安全組,並將mysql的源代碼更改爲0.0.0.0,而不是默認的ec2.webserveruser。謝謝大家:)

0

你可以嘗試像出頭這個。

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 

之後,您可以觸發查詢。

更多信息

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

0

如果您的服務器在端口3066監聽那麼你的JDBC URL是錯誤的。 JDBC網址將與端口3066不是3306

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

我非常確定默認的mysql端口是3306. http://dev.mysql.com/doc/refman/5.1/en/connecting.html – courtyen

0

要驗證端口,請執行以下步驟:

  1. 執行winmysqladmin.exe文件;它在mysql \ bin文件夾中可用。我假設你正在使用Windows。
  2. 從彈出窗口中選擇Variables選項卡。
  3. 在此選項卡中,在變量名稱列下搜索名爲port的條目。這個值表示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.

全面討論一下How to tell which port mySQL is running on?

Establishing a Connection


看看教程,您可以用這種方式嘗試爲好。通常的MySQL運行在端口默認3306

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wrox?user=xxx&password=xxxxxxxx"); 
+0

如果你看完整個問題,它的OP很清楚*尼克斯風味。無需做出假設。 – Gimby

+1

@Gimby發佈我的答案後編輯帖子。 – Braj

+0

你是對的。抱歉。 – Gimby

0

MySQL的JDBC URL解析如下:

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

你從myserver.com託管你的MySQL服務器?例如,如果您在本地託管服務器,則主機名爲localhost

有關進一步配置url的信息,您可以查看this JDBC tutorial

相關問題