Java Database Connectivity (JDBC) - com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying except
前陣子在處理某人丟給我要我修改的一支 Java Server Page 程式,花了一些時間安裝配置好JSP的執行環境和開發環境後,開啟網頁要測試關鍵字搜尋時卻顯示一片空白,但 Tomcat 好像無法跟 Rails 的 Mongrel Server 一樣可以從 Terminal 即時瀏覽輸出訊息,所以只好去查看/var/log/tomcat6/catalina.out記錄檔,之後找到如下的錯誤訊息:
com.mysql.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:310)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
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.(Socket.java:392)
at java.net.Socket.(Socket.java:235)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:267)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2739)
at com.mysql.jdbc.Connection.(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:222)
at foo.retrieve.caluQueryM(retrieve.java:76)
at foo.retrieve.(retrieve.java:34)
at org.apache.jsp.showDocFeature_jsp._jspService(showDocFeature_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
** END NESTED EXCEPTION **
從log file的輸出看來,這個錯誤明顯是Java程序没有連接上MySQL資料庫,導致無法進行任何與資料庫相關的操作。因此這個錯誤本質上是一個網路的問題。大致瀏覽了一下程式碼,發現它是採用TCP/IP的方式去連接MySQL:
String sDriver = "com.mysql.jdbc.Driver";
String sCon = "jdbc:mysql://localhost:3306/" + myprojectuser=root&password=123321&useUnicode=true&characterEncoding=big5";
最後我從Oracle論壇上面找到了一個相似的討論串,終於得到了頭緒,問題就出在MySQL設置檔裡有個選項skip-networking,而我以往習慣會將它設置為ON(因為在開發Ruby on Rails應用程式時根本不會遇到這種問題),因為這個設置,因此使得這支JSP程式無法透過TCP/IP去連接MySQL。解決方法只要將/etc/mysql/my.cnf設置檔中的skip-networking這個選項註解掉或移除就行了,同時我也將skip-name-resolve的選項給註解掉,最後重新啟動MySQL和Tomcat就搞定了!
Development Environment:Linux Ubuntu 10.04 + MySQL 5.1.46 Community Server + JDK 6.0 + MySQL Connector 5.1.6 with "com.mysql.jdbc.Driver" class + Apache2 + Tomcat6
com.mysql.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:310)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
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.
at java.net.Socket.
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.jdbc.MysqlIO.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2739)
at com.mysql.jdbc.Connection.
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:222)
at foo.retrieve.caluQueryM(retrieve.java:76)
at foo.retrieve.
at org.apache.jsp.showDocFeature_jsp._jspService(showDocFeature_jsp.java:100)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)
** END NESTED EXCEPTION **
從log file的輸出看來,這個錯誤明顯是Java程序没有連接上MySQL資料庫,導致無法進行任何與資料庫相關的操作。因此這個錯誤本質上是一個網路的問題。大致瀏覽了一下程式碼,發現它是採用TCP/IP的方式去連接MySQL:
String sDriver = "com.mysql.jdbc.Driver";
String sCon = "jdbc:mysql://localhost:3306/" + myprojectuser=root&password=123321&useUnicode=true&characterEncoding=big5";
最後我從Oracle論壇上面找到了一個相似的討論串,終於得到了頭緒,問題就出在MySQL設置檔裡有個選項skip-networking,而我以往習慣會將它設置為ON(因為在開發Ruby on Rails應用程式時根本不會遇到這種問題),因為這個設置,因此使得這支JSP程式無法透過TCP/IP去連接MySQL。解決方法只要將/etc/mysql/my.cnf設置檔中的skip-networking這個選項註解掉或移除就行了,同時我也將skip-name-resolve的選項給註解掉,最後重新啟動MySQL和Tomcat就搞定了!
Development Environment:Linux Ubuntu 10.04 + MySQL 5.1.46 Community Server + JDK 6.0 + MySQL Connector 5.1.6 with "com.mysql.jdbc.Driver" class + Apache2 + Tomcat6