无法使用JDBC连接到MySQL-连接超时-Ubuntu 9.04
-
05-10-2019 - |
题
我正在运行Ubuntu,并最终试图使用JDBC将Tomcat连接到我的MySQL数据库。
它以前有效 但是重新启动后,实例现在无法连接。
- Tomcat 6和MySQL 5.0.75都在同一台机器上
- 连接字符串:JDBC:mysql:/// localhost:3306
- 我可以使用命令行上的mySQL连接到
mysql
命令 - my.cnf文件是非常标准的(可根据要求可用)具有绑定地址:127.0.0.1
- 尽管Netstat说MySQL正在听
- 我有一个iPtables规则可以转发80-> 8080,我不知道防火墙。
我对此非常陌生,我不确定还要测试什么。我不知道我是否应该在ETC/界面中查找以及是否要做的事情。这很奇怪,因为它曾经起作用,但是重新启动后,它已经下降了,所以我必须更改一些东西。...:)。
我意识到超时表示服务器没有响应,我认为这是因为请求实际上没有通过。我通过APT-GET和TOMCAT手动安装MySQL。
mysqld处理
root@88:/var/log/mysql# ps -ef | grep mysqld
root 21753 1 0 May27 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql 21792 21753 0 May27 ? 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock
root 21793 21753 0 May27 ? 00:00:00 logger -p daemon.err -t mysqld_safe -i -t mysqld
root 21888 13676 0 11:23 pts/1 00:00:00 grep mysqld
Netstat
root@88:/var/log/mysql# netstat -lnp | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21792/mysqld
unix 2 [ ACC ] STREAM LISTENING 1926205077 21792/mysqld /var/run/mysqld/mysqld.sock
玩具连接课
root@88:~# cat TestConnect/TestConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestConnection {
public static void main(String args[]) throws Exception {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Got driver");
con = DriverManager.getConnection(
"jdbc:mysql:///localhost:3306",
"uname", "pass");
System.out.println("Got connection");
if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");
} finally {
if(con != null)
con.close();
}
}
}
玩具连接类输出
注意:这是我从tomcat遇到的错误。
root@88:~/TestConnect# java -cp mysql-connector-java-5.1.12-bin.jar:. TestConnection
Got driver
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 1 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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at TestConnection.main(TestConnection.java:14)
Caused by: 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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
... 12 more
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
... 13 more
telnet输出
root@88:~/TestConnect# telnet localhost 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection timed out
解决方案
NetStat显示,MySQL确实在所有本地地址上都在端口3306上聆听。由于Telnet无法连接到127.0.0.1 是 阻止您的防火墙 - 尝试跑步 sudo ufw default allow
有效禁用它,或 sudo ufw allow 3306
看看它是否做出任何更改。
其他提示
您的 /etc /主机文件是什么样的?您有以下条目吗?
127.0.0.1 localhost
您可以尝试telnet:
telnet 127.0.0.1 3306 or telnet 127.0.0.1:3306
或JDBC连接到:
jdbc:mysql://127.0.0.1:3306
连接字符串应该是
jdbc:mysql://localhost:3306
代替
jdbc:mysql:///localhost:3306
同样在UNIX/Linux上,默认情况下,客户端/服务器通信是通过UNIX套接字文件而不是通过TCP/IP完成的。因此,您无法在本地计算机上进行端口3306。
您是总是为GetConnection方法使用相同的签名,还是您在停止工作之前更改的一件事? (字符串建议中的'///'这是最近的添加。)
如果这是新代码,则可以尝试以下连接字符串:
字符串dburl =“ jdbc:mysql:// localhost:3306/[db]?user = [user]&password = [password]”;连接conn = drivermanager.getConnection(dburl);
这是mysql.com文档页面上的示例格式:
http://dev.mysql.com/doc/refman/5.0/es/connector-j-usagenotes-basic.html