无法通过SSH连接到MySQL服务器(由strato托管)

Lev*_*erk 8 mysql ssh putty navicat

我正在尝试连接到由strato托管的MySQL服务器.

我按照本页面的说明使用PuTTy连接数据库.现在连接到我的数据库是没有问题,如果我使用终端:你可以在这个截图上看到屁股

但是一旦我尝试使用NaviCatMySQL Workbench连接到MySQL服务器,它就会给我这个错误:错误的屏幕截图.

我究竟做错了什么?如何使用NaviCat连接到数据库?我还想通过Java连接到这个数据库,但是我应该用什么线连接,我应该在主机上填写什么?只是localhost?所以我应该使用这样的函数来连接:

public static void connectToSQL() {
    try {
        @SuppressWarnings("unused")
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/DB3262523", "U3262523", "....");
        System.out.println("Connection success");
    } catch (Exception e) {
        //TODO: handle exception
        System.err.println(e);

}
}
Run Code Online (Sandbox Code Playgroud)

Red*_*Boy 0

最后,我明白了,你只想使用 SSH 隧道连接 mysql 数据库服务,你不想打开防火墙,只想通过 JumpBox 访问数据库。我使用 JSch Libary 进行 SSH 调整。

这是你的代码。

import java.sql.DriverManager;
import java.sql.SQLException;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.sql.Connection;


public class MySqlConnOverSSH {


    public static void main(String[] args) throws SQLException {

        int local_proxy_port=4567;

        String database_host="rdbms.strato.de";
        String ssh_host="ssh.strato.de";
        int database_port=3306;
        String user="ssh_username";
        String privateKey = "In case you want to use private key to do SSH";

        String password="sshpassword";
        String dbuserName = "yourDBName";
        String dbpassword = "YourDBPass";
        String url = "jdbc:mysql://localhost:"+local_proxy_port+"/your-database-name";
        String driverName="com.mysql.jdbc.Driver";
        Connection conn = null;
        Session session= null;
        try{
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            //Only in case you need to access SSH using key.
            //jsch.addIdentity(privateKey);
            session=jsch.getSession(user, ssh_host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");
            int assinged_port=session.setPortForwardingL(local_proxy_port, database_host, database_port);
            System.out.println("localhost:"+assinged_port+" -> "+database_host+":"+database_port);
            System.out.println("Port Forwarded");

            Class.forName(driverName).newInstance();
            conn = DriverManager.getConnection (url, dbuserName, dbpassword);
            System.out.println ("Connected!!!");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(conn != null && !conn.isClosed()){
                System.out.println("Hurry Done!!");
                conn.close();
            }
            if(session !=null && session.isConnected()){
                System.out.println("Hurry Done!! Closing SSH");
                session.disconnect();
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

确保包含mysql-connector-java-xxx-bin.jar;到您的classpath. mysql jdbc jar其中 xxx 是eg的版本5.0.8

如果运行此代码不起作用,请发布您在运行此代码后看到的进一步错误。我可以重新尝试回答。