【问题标题】:java SSH connection to mysql accessing localhost instead of remote hostjava SSH连接到mysql访问localhost而不是远程主机
【发布时间】:2023-03-09 00:28:01
【问题描述】:

我正在尝试通过 SSH 连接从我的运行 windows OS 的电脑连接远程服务器中的 mysql。我安装了所有必需的 ssh、Java jar 并且信任能够访问 SSH 但不是连接到远程主机而是连接本地数据库一台我的机器我告诉下面的代码如何去远程服务器 因为我的服务器提供商也没有提供 ip 只允许通过 ssh 访问 代码在下面

package connectsshserver;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 lport=3306;
        String rhost="host47.registrar-servers.com";
        String host="host47.registrar-servers.com";
        int rport=3306;
        String user="soft123a"; //ssh user name 
        String password="123487ab"; //ssh password
        String dbuserName = "root"; 
        String dbpassword = "";
        String url = "jdbc:mysql://127.0.0.1"+"/arbaccounting"; //host+db
        String driverName="com.mysql.jdbc.Driver";
        Connection conn = null;
        Session session= null;
        try{
            //Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            session=jsch.getSession(user, host, 21098);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");
            int assinged_port=session.setPortForwardingL(21098, rhost, 3306);
            System.out.println("127.0.0.1:"+assinged_port+" -> "+rhost+":"+rport);
            System.out.println("Port Forwarded");
            //mysql database connectivity
            Class.forName(driverName).newInstance();
            conn = DriverManager.getConnection (url, dbuserName, dbpassword);
            System.out.println ("Database connection established");
            System.out.println("DONE");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(conn != null && !conn.isClosed()){
                System.out.println("Closing Database Connection");
                conn.close();
            }
            if(session !=null && session.isConnected()){
                System.out.println("Closing SSH Connection");
                session.disconnect();
            }
        }
    }

}

完美的,但对于本地数据库 我改变什么来连接到远程数据库? 连接的 127.0.0.1:21098 -> host47.registrar-servers.com:3306 端口转发 建立数据库连接 完毕 关闭数据库连接 关闭 SSH 连接 构建成功(总时间:9 秒)

【问题讨论】:

    标签: java mysql ssh webserver


    【解决方案1】:

    在行中:DriverManager.getConnection (url, dbuserName, dbpassword),url 引用指向 localhost (127.0.0.1),它与您的定位机器建立 DB 连接

    java.net.InetAddress#getByName(String) 将允许您检索远程主机的 IP 以便使用它。您还应该能够将 127.0.0.1 替换为服务器的名称,而不是 IP。

    【讨论】:

    • 我输入了 IP 地址和主机名,但出现错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    • 你有关于底层异常的任何信息吗? IE。通讯被拒绝、缓冲区故障等?
    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2012-02-03
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多