Connexion java à une BDD MySQL distante via SSH
Bonjour,
J'ai un problème pour me connecter à partir d'un client JAVA à ma BDD MySQL "freez" qui est située sur un serveur distant "emotion" (situé dans un réseau
universitaire).
J'utilise donc un client SSH (PuTTY) pour faire un tunnel SSH avec les ports 3306 (MySQL) vers mon serveur (OS = Debian).
Voici l'équivalent pour Mac et Linux :
Code:
ssh -N -T -L 3306:emotion:3306 monlogin@res.iut-velizy.uvsq.fr
MySQL est configuré par défaut pour ne pas accepter les connexions externes, j'ai donc changé cela dans le fichier de configuration de MySQL sur le serveur : /etc/mysql/my.cnf
Code:
bind-address = 127.0.0.1
est devenu
Code:
bind-address = 10.0.0.46
NB : 10.0.0.46 = l'adresse de ma machine sur le réseau universitaire.
J'ai ensuite changé ,grâce à phpmyadmin, les privilèges de l'utilisateur MySQL "hote" utilisé pour la connexion avec mon client java :
Citation:
General :
Utilisateur = hote
Serveur = % (tous)
Mot de passe = Non
Privilèges = usage
Grant = Non
Privilèges spécifiques à une base de données:
Base = freez
Droits = SELECT, INSERT, UPDATE, DELETE
Grant = Non
Privilèges spécifiques à une table = Non
Puis j'ai redémarré MySQL grâce à la commande suivante :
Code:
/etc/init.d/mysql restart
Mes identifiants sont valides et j'ai bien compilé en précisant le driver (situé dans le même répertoire que ma classe) dans le classpath :
Code:
1 2
|
>javac -classpath "D:\Documents\Boulot\E-motion\Developpement\Accès aux bdd\mysql-connector-java-5.1.5-bin.jar;." *.java |
Voici le code de mon client JAVA:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import java.sql.*;
class connexion
{
StringBuffer resultat= new StringBuffer();
int nCol;
public connexion ()
{
envoiRequete();
}
void envoiRequete()
{
//Connexion
try
{
String nomDriver = "com.mysql.jdbc.Driver";
Connection conn=null;
try
{
Class.forName(nomDriver);
conn = DriverManager.getConnection("jdbc:mysql://10.0.0.46:3306/freez?","hote","");
}
catch(ClassNotFoundException cnfe)
{
System.out.println("La classe "+nomDriver+" n'a pas été trouvée");
cnfe.printStackTrace();
}
//Requete
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from event;");
ResultSetMetaData rsmd = rs.getMetaData ();
while (rs.next ())
{
String tmp= "";
for (int i = 1; i <= nCol ; ++i)
{
switch (rsmd.getColumnType (i))
{
case Types.INTEGER:
tmp = ""+rs.getInt (i);
break;
case Types.CHAR:
tmp = ""+rs.getString (i);
break;
default:
System.out.println("Type non implémenté");
break;
}
resultat.append (tmp+"\t");
}
resultat.append ("\n");
}
//Affichage du resultat
System.out.println(resultat.toString());
//Fin de connexion
conn.close();
}
catch (SQLException ex)
{
System.out.println("Exception: " + ex.toString());
ex.printStackTrace() ;
}
}
public static void main(String argv[])
{
new connexion ();
}
} |
J'ouvre alors mon tunnel SSH.
Quand je lance le programme, l'accès à la base de donnée m'est refusée :bug: :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1
074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:283)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at connexion.envoiRequete(connexion.java:22)
at connexion.<init>(connexion.java:9)
at connexion.main(connexion.java:68)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.ja
va:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
... 14 more |
Je pense que cela viens du fait que j'utilise un tunnel SSH. Ou alors d'une mauvaise configuration de MySQL.
J'ai également pensé au firewall qui bloquerais ma connexion, j'ai donc essayé avec un client Linux, mais sans succès.
Je vous en prie, aidez moi, cela fait 4 jour que je me casse la tête sur ce problème. Je commence sérieusement à perdre espoir... :(