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

问题描述 投票:8回答:2

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

我按照this page的指示使用PuTTy连接数据库。现在连接到我的数据库是没有问题,如果我使用终端:ass you can see on this screenshot

但是一旦我尝试使用NaviCatMySQL Workbench连接到MySQL服务器,它就会给我这个错误:screenshot of the errors

我究竟做错了什么?如何使用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);

}
}
mysql ssh putty navicat
2个回答
0
投票

由于您可以使用终端连接到数据库,因此您可以实际检查数据库本身,从服务器的角度来看,为什么连接失败。

试试这个查询:

SELECT * FROM performance_schema.host_cache

每个连接错误都在不同的列中进行说明,因此这有助于查明拒绝连接的根本原因。

参考手册:

https://dev.mysql.com/doc/refman/5.6/en/host-cache-table.html


0
投票

最后,我明白了,你想只使用SSH隧道连接mysql数据库服务,你不想打开防火墙,只想通过JumpBox访问数据库。我用JSch liberary做了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();
            }
        }
    }

}

一定要包括mysql-connector-java-xxx-bin.jar;到你的classpath。其中xxx是mysql jdbc jar的版本,例如5.0.8

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

© www.soinside.com 2019 - 2024. All rights reserved.