Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 远程访问数据库嵌套异常_Java_Database_Ssh_Remote Access - Fatal编程技术网

Java 远程访问数据库嵌套异常

Java 远程访问数据库嵌套异常,java,database,ssh,remote-access,Java,Database,Ssh,Remote Access,我想在我的java程序中连接到远程数据库ssh,因此我为此编写了一个java代码,我可以通过代码ssh连接到数据库服务器,但我无法访问数据库,这是我的输出,其中包含错误: (此链接是我之前的问题,与此问题相关) 我在远程服务器中定义一个具有所有权限的用户 将**上的所有权限授予由“密码”标识的“用户名”@“IP” 并将我的Ip添加到my.cnf(在DBserver中)中的绑定地址 这是什么意思?我怎样才能修好它 通过portforwarding连接时,数据库将不会看到您的原始IP地址,而是本地I

我想在我的java程序中连接到远程数据库ssh,因此我为此编写了一个java代码,我可以通过代码ssh连接到数据库服务器,但我无法访问数据库,这是我的输出,其中包含错误: (此链接是我之前的问题,与此问题相关)

我在远程服务器中定义一个具有所有权限的用户

将**上的所有权限授予由“密码”标识的“用户名”@“IP”

并将我的Ip添加到my.cnf(在DBserver中)中的绑定地址

这是什么意思?我怎样才能修好它

  • 通过portforwarding连接时,数据库将不会看到您的原始IP地址,而是本地IP地址(localhost)。因此,您的授权应该是
    将**上的所有权限授予由“密码”标识的“用户名”@“本地主机”
  • Mysql的bind address参数用于配置服务器IP,而不是客户端IP。您应该将该设置保留为默认设置(除非您另有需要),因为您将从localhost进行连接
  • 如果
    Channel=session.openChannel(“sftp”)
    按照它看起来的方式(即将您连接到SFTP子系统),sshd可能不允许您连接到其他地方。您应该使用
    Channel=session.openChannel(“shell”)
  • 检查服务器日志
  • 谢谢:),我使用此语法为我的用户授予新权限。也改变了openChannel,但我得到了一个非常例外的结果。关于第2部分:我不明白,我在数据库服务器中写了绑定地址,你的意思是我应该在代码服务器(希望远程访问数据库的服务器)中写吗,我在/var/log/中检查了安全日志和消息日志,但找不到任何相关内容。您写道“我……将我的Ip添加到my.cnf(在DBserver中)中的绑定地址”。DB服务器根本看不到客户端的IP地址。因此,您不能在DB服务器端的任何位置配置它,也不能在任何my.cnf中配置它。绑定地址告诉DB服务器要侦听哪个IP
    session.setPortForwardingL(3366,host,3306)
    将所有流量转发到地址“host”上的端口3306,因此您的DB服务器应该绑定到该地址。如果使用
    session.setPortForwardingL(3366,“localhost”,3306)
    ,则您的DB服务器必须在localhost(绑定地址:127.0.0.1)上侦听。我不明白:(,我将setportforwadringL第二个元素更改为127.0.0.1,但不起作用。
    hi
    identity added
    session created.
    session connected.....
    shell channel connected....
    connecting to database ...
    db Method...
    try-catch
    Connecting to database...
    DB_URL : jdbc:mysql://localhost:3366/DBNAME
    com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
    
    ** BEGIN NESTED EXCEPTION **
    
    com.mysql.jdbc.CommunicationsException
    MESSAGE: Communications link failure due to underlying exception:
    
    ** BEGIN NESTED EXCEPTION **
    
    java.io.EOFException
    MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    
    STACKTRACE:
    
    java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
            at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
            at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
            at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2748)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
            at java.sql.DriverManager.getConnection(DriverManager.java:664)
            at java.sql.DriverManager.getConnection(DriverManager.java:247)
            at Main.connectToDB(Main.java:78)
            at Main.main(Main.java:49)
    
    
    ** END NESTED EXCEPTION **
    
    
    
    Last packet sent to the server was 0 ms ago.
    
    STACKTRACE:
    
    com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
    
    ** BEGIN NESTED EXCEPTION **
    
    java.io.EOFException
    MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    
    STACKTRACE:
    
    java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
            at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)
            at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
            at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2748)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
            at java.sql.DriverManager.getConnection(DriverManager.java:664)
            at java.sql.DriverManager.getConnection(DriverManager.java:247)
            at Main.connectToDB(Main.java:78)
            at Main.main(Main.java:49)
    
    
    ** END NESTED EXCEPTION **
    
    
    
    Last packet sent to the server was 0 ms ago.
            at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:641)
            at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2748)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
            at java.sql.DriverManager.getConnection(DriverManager.java:664)
            at java.sql.DriverManager.getConnection(DriverManager.java:247)
            at Main.connectToDB(Main.java:78)
            at Main.main(Main.java:49)
    
    
    ** END NESTED EXCEPTION **
    
    
    
    Last packet sent to the server was 1 ms ago.
            at com.mysql.jdbc.Connection.createNewIO(Connection.java:2820)
            at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
            at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
            at java.sql.DriverManager.getConnection(DriverManager.java:664)
            at java.sql.DriverManager.getConnection(DriverManager.java:247)
            at Main.connectToDB(Main.java:78)
            at Main.main(Main.java:49)
    Goodbye!
    done
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    
    public class Main {
    
        public static void main(String[] arg) {
            try {
                    System.out.println("hi");
                JSch jsch = new JSch();
    
                String user = "***";
                String host = "**.**.***.***";
                int port = 22;
                String privateKey = "id_rs";
    
                jsch.addIdentity(privateKey);
                System.out.println("identity added ");
    
                Session session = jsch.getSession(user, host, port);
                System.out.println("session created.");
    
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                session.setConfig(config);
    
                session.connect();
               session.setPortForwardingL( 3366, host, 3306)  ;
    
                System.out.println("session connected.....");
    
                Channel channel = session.openChannel("sftp");
                channel.setInputStream(System.in);
                channel.setOutputStream(System.out);
                channel.connect();
                System.out.println("shell channel connected....");
                System.err.println("connecting to database ...");
    
               connectToDB();
    
              System.out.println("done");
    
            } catch (Exception e) {
                System.err.println(e);
            }
        }
    
    
            static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            static String databaseName ="DBNAME";
           static  int lport = 3366;
            static final String DB_URL = "jdbc:mysql://localhost:"+lport+"/"+databaseName;
    
            static final String USER = "***";
            static final String PASS = "***";
    
      public static void connectToDB() {
                    System.out.println("db Method...");
                    Connection conn = null;
                    Statement stmt = null;
                    try{
                            System.out.println("try-catch");
                            Class.forName("com.mysql.jdbc.Driver");
    
                            System.out.println("Connecting to database...");
                            System.err.println("DB_URL : "+DB_URL);
                            System.err.println(USER+":"+PASS+";");
                            conn = DriverManager.getConnection(DB_URL,USER,PASS);
    
                            System.out.println("hey!");
                            stmt = conn.createStatement();
                            String sql;
                            sql="SELECT * from Customer_Classes ; ";
                            ResultSet rs = stmt.executeQuery(sql);
    
                           try (BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"))) {
                                    while(rs.next()){
                                          String name = rs.getString("ID");
                                          bw.write(name);
                                          bw.newLine();
    
                                          System.out.println(" name: " + name);
                                    }
                                    bw.close();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
    
                            rs.close();
                            stmt.close();
                            conn.close();
                    }catch(SQLException se){
                            se.printStackTrace();
                    }catch(Exception e){
                            e.printStackTrace();
                    }finally{
                            try{
                                    if(stmt!=null)
                                            stmt.close();
                            }catch(SQLException se2){
                                System.err.println("unable to close statement");
                            }
       }
                            try{
                                    if(conn!=null)
                                            conn.close();
                            }catch(SQLException se){
                                   System.err.println("unable to close connection");
                                   se.printStackTrace();
                            }
                    }
                    System.out.println("Goodbye!");
            }
    }