Connection Timeout - - kann nicht auf MySQL mit JDBC verbinden Ubuntu 9.04
-
05-10-2019 - |
Frage
Ich bin mit Ubuntu und ich versuche letztlich Tomcat meiner MySQL-Datenbank zu verbinden JDBC verwenden.
Es ist zuvor , aber nach einem Neustart der Instanz nicht jetzt verbinden.
- Sowohl Tomcat 6 und MySQL 5.0.75 sind auf der gleichen Maschine
- Verbindungs ??string: jdbc: mysql: /// localhost: 3306
- Ich kann auf der Kommandozeile zu MySQL verbindet den
mysql
Befehl mit - Die Datei my.cnf ist ziemlich Standard (auf Anfrage) hat bind-Adresse: 127.0.0.1
- Ich kann nicht Telnet mit dem MySQL-Port trotz netstat sagen MySQL hört
- Ich habe eine iptables 80 weiterleiten regieren. -> 8080 und keine Firewall Ich bin mir dessen bewusst
Ich bin ziemlich neu in diesem und ich bin sicher nicht, was anderes zu testen. Ich weiß nicht, ob ich in etc / Schnittstellen suchen werden soll, und wenn ich es tue, was zu suchen. Es ist komisch, weil es an der Arbeit verwendet, aber nach einem Neustart ist es nach unten, so ich etwas geändert haben muß ....:.)
Ich weiß, ein Timeout zeigt der Server nicht reagiert, und ich nehme an, es ist, weil die Anfrage tatsächlich nicht durch zu bekommen. Ich installierte MySQL über apt-get und Tomcat manuell.
mysqld-Prozesse
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
Toy Klasse Connection
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();
}
}
}
Toy Connection-Klasse Ausgabe
. Hinweis: Dies ist der gleiche Fehler, den ich von Tomcat bekommen
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 Ausgabe
root@88:~/TestConnect# telnet localhost 3306
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection timed out
Lösung
Netstat zeigt, dass MySQL in der Tat auf Port 3306 auf allen lokalen Adressen abhört. Da telnet 127.0.0.1 keine Verbindung herstellen kann es is eine Firewall, die Ihren Sie stoppt -. Versuchen sudo ufw default allow
zu effectivly deaktivieren es läuft oder sudo ufw allow 3306
und sehen, ob es Änderungen vornimmt
Andere Tipps
Wie sieht Ihre / etc / hosts-Datei aussehen? Haben Sie einen Eintrag wie folgt aus?
127.0.0.1 localhost
Können Sie versuchen, Telnet:
telnet 127.0.0.1 3306 or telnet 127.0.0.1:3306
oder JDBC-Verbindung zu:
jdbc:mysql://127.0.0.1:3306
Die Verbindungszeichenfolge sollte sein
jdbc:mysql://localhost:3306
statt
jdbc:mysql:///localhost:3306
Auch auf Unix / Linux, die standardmäßig die Client / Server-Kommunikation wird über die Unix-Socket-Datei erfolgen und nicht über TCP / IP. Daher sind Sie Telnet auf Port 3306 auf dem lokalen Computer nicht in der Lage.
Haben Sie immer mit der gleichen Signatur für die getConnection Methode oder sind, dass eines der Dinge, die Sie geändert, bevor es aufgehört zu arbeiten? (Die '///' in den String Vorschläge war es ein neuer Zusatz.)
Wenn diese neue Code ist, wird möglicherweise die folgende Verbindungszeichenfolge versuchen:
String dbUrl = "jdbc: mysql: // localhost: 3306 / [db] user = [user] & password = [Kennwort]"; Connection conn = DriverManager.getConnection (dbUrl);
Dies ist das Beispiel-Format auf der mysql.com Dokumentationsseite:
http: //dev.mysql .com / doc / refman / 5.0 / es / connector-j-usagenotes-basic.html