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
- mysqlが聞いていると言っているにもかかわらず、私はmysqlポートにテルネットすることはできません
- 私は80-> 8080を転送するための1つのiptablesルールを持っています、そして私が知っているファイアウォールはありません。
私はこれにかなり慣れていないので、他に何をテストするのかわかりません。など/インターフェイスを見るべきかどうか、そして何を探すべきかわからない。以前は動作していたので奇妙ですが、再起動した後はダウンしているので、何かを変えたに違いありません.... :)。
タイムアウトはサーバーが応答していないことを示していることに気付き、リクエストが実際に通過していないためだと思います。 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 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メソッドには常に同じ署名を使用していましたか、それとも機能しなくなる前に変更したものの1つですか? (文字列の「///」の提案それは最近の追加でした。)
これが新しいコードの場合、次の接続文字列を試すことができます。
string 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