我正在为一个动态的Web项目工作。我试图从MYSQL数据库中提取数据,以便我可以读取它并在我的Web应用程序中使用它。当我尝试连接时,我收到此错误:
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 -a以查看它正在侦听的端口
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 :(得分: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/
答案 1 :(得分:0)
如果您的服务器正在侦听端口3066,那么您的JDBC URL是错误的。 JDBC url将使用端口3066而不是3306。
String url = "jdbc:mysql://myserver.com:3066/trivitup";
String user = user";
String pw = "pass";
答案 2 :(得分:0)
要验证端口,请执行以下步骤:
您在url中使用了错误的端口号,或者mySQL没有在该端口上侦听no或者根本没有启动mySQL服务器。
有关完整讨论,请查看How to tell which port mySQL is running on?
上的教程您也可以尝试这种方式。通常mysql在端口上运行,默认为3306。
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wrox?user=xxx&password=xxxxxxxx");
答案 3 :(得分:0)
MySQL jdbc url的解析如下:
jdbc:mysql://[hostname]:3066/[dbname]
您是否从myserver.com
托管MySQL服务器?例如,如果您在本地托管服务器,则主机名为localhost
。
您可以查看this JDBC tutorial以获取有关进一步配置网址的信息。
答案 4 :(得分:0)
我找到了答案!!!这是超级简单!!!我正在使用亚马逊ec2默认的linux发行版。我把我的安全组打开到:mysql但是我的源代码设置为我自己,而不是每个人。因此,尽管能够打开和收听,我的服务器仍然没有收听0.0.0.0。
我去了ec2上的安全组,并将mysql的源代码更改为0.0.0.0而不是默认的ec2.webserveruser。谢谢大家:)