access mySql via multiple ssh in java -
i'm new ssh , have question. have connect via ssh gateway , within ssh session i've ssh server. can connect mysql server.
ssh <gateway>
ssh -l3306:db02:3306 username@db02
then can configure putty , heidysql access mysql database. far, good.
but want access mysql database via java code. googled , found code use jsch.
so, below result of code. when run code, session okay, portforwarding okay.
but part giving me error:
connection con = drivermanager.getconnection("jdbc:mysql://localhost:"+nlocalport, strdbuser, strdbpassword);
this error: java.sql.sqlexception: access denied user 'dbuser'@'gateway.appl.local' (using password: yes)
it seems port forwarding not done because of error shows connect gateway: 'dbuser'@'gateway.appl.local'
after succesfull port forwarding should show this: dbuser'@'**db02**.appl.local
import com.jcraft.jsch.jsch; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.util.properties; public class mysqlconnoverssh { private static void dosshtunnel( string strsshuser, string strsshpassword, string strsshhost, int nsshport, string strremotehost, int nlocalport, int nremoteport ) throws jschexception { final jsch jsch = new jsch(); session session = jsch.getsession( strsshuser, strsshhost, 22 ); session.setpassword( strsshpassword ); final properties config = new properties(); config.put( "stricthostkeychecking", "no" ); session.setconfig( config ); session.connect(); session.setportforwardingl(nlocalport, strremotehost, nremoteport); } /** * java program connect remote database through ssh using port forwarding * @throws sqlexception */ public static void main(string[] args) throws sqlexception { try { string strsshuser = "sshusername"; // ssh loging username string strsshpassword = "sshpassword"; // ssh login password string strsshhost = "10.20.0.234"; // hostname or ip or ssh server int nsshport = 22; // remote ssh host port number string strremotehost = "db02"; // hostname or ip of database server int nlocalport = 3307; // local port number use bind ssh tunnel int nremoteport = 3306; // remote port number of database string strdbuser = "dbuser"; // database loging username string strdbpassword = "dbuserpw"; // database login password mysqlconnoverssh.dosshtunnel(strsshuser, strsshpassword, strsshhost, nsshport, strremotehost, nlocalport, nremoteport); class.forname("com.mysql.jdbc.driver"); connection con = drivermanager.getconnection("jdbc:mysql://localhost:"+nlocalport, strdbuser, strdbpassword); con.close(); } catch( exception e ) { e.printstacktrace(); } { system.exit(0); } } }
this quite slow, if absolutely have to, set tunnel across both connections:
on local host: ssh -l3306:localhost:3306 user@host1 on host1: ssh -l3306:localhost:3306 user@db02
then should able connect 3306 on local system , tunneled through 3306 on db02. if host1
running mysql , don't want interfere it, use different intermediate port, in
on local host: ssh -l3306:localhost:3316 user@host1 on host1: ssh -l3316:localhost:3306 user@db02
Comments
Post a Comment