尝试使用 jsch 和 jdbc 连接到 postgres 数据库会产生 "The connection attempt failed. Caused by: java.io.EOFException"

Trying to connect to postgres database with jsch and jdbc yields "The connection attempt failed. Caused by: java.io.EOFException"

我正在尝试连接到 Amazon EC2 服务器上的 postgres 数据库 运行。我知道服务器配置正确,因为我可以从 pgadmin 访问它并作为 intellij 中的数据源。

我也可以在仅使用 jsch 时从我的程序中连接到服务器 shell,并使用 jdbc 直接连接到另一个数据库,但是一旦我尝试通过 ssh 连接到我的 postgres 实例使用 jsch 隧道我遇到了一个我无法诊断的奇怪错误。:

org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:262)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:67)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:216)
at org.postgresql.Driver.makeConnection(Driver.java:406)
at org.postgresql.Driver.connect(Driver.java:274)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at EnumStreamer.main(EnumStreamer.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.io.EOFException
at org.postgresql.core.PGStream.ReceiveChar(PGStream.java:285)
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:411)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:208)
... 12 more

我用来连接的代码如下:

 System.out.println("Connecting...");
        session = secConnect.getSesssion();
        UserInfo ui = new uInfo();
        session.setUserInfo(ui);
        session.connect();
        session.setPortForwardingL(5432,secConnect.getHost(),5432);
        System.out.println("SSH Tunneled");

        System.out.println("Opening Database Connection");
        Class.forName("org.postgresql.Driver");
        Properties props = new Properties();
        props.setProperty("user","username");
        props.setProperty("password","password");
        props.setProperty("loglevel", "2");
        connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dbname",props);
        System.out.println("Connected");

secConnect对象的相关代码在这里:

jsch.addIdentity(chooser.getSelectedFile().getAbsolutePath());

 public Session getSesssion() throws JSchException {
    return jsch.getSession(user, host, 22);
}

我的 ec2 postgres 服务器配置为 listenaddress = '*' 并且 pg_hba.conf 有行

host all all 0.0.0.0/0 md5

尝试连接时生成的数据库日志:

DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  InitPostgres
DEBUG:  my backend ID is 2
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  autovacuum: processing database "postgres"
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  pg_statistic: vac: 0 (threshold 127), anl: 0 (threshold 88)
DEBUG:  pg_type: vac: 0 (threshold 116), anl: 0 (threshold 83)
DEBUG:  pg_authid: vac: 7 (threshold 50), anl: 8 (threshold 50)
DEBUG:  pg_proc: vac: 0 (threshold 548), anl: 8 (threshold 299)
DEBUG:  pg_class: vac: 0 (threshold 108), anl: 0 (threshold 79)
DEBUG:  pg_attribute: vac: 0 (threshold 499), anl: 0 (threshold 275)
DEBUG:  pg_constraint: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_operator: vac: 0 (threshold 196), anl: 0 (threshold 123)
DEBUG:  pg_opclass: vac: 0 (threshold 74), anl: 0 (threshold 62)
DEBUG:  pg_am: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_amop: vac: 0 (threshold 129), anl: 0 (threshold 89)
DEBUG:  pg_amproc: vac: 0 (threshold 107), anl: 0 (threshold 79)
DEBUG:  pg_language: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_aggregate: vac: 0 (threshold 74), anl: 0 (threshold 62)
DEBUG:  pg_rewrite: vac: 0 (threshold 71), anl: 0 (threshold 61)
DEBUG:  pg_trigger: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_description: vac: 0 (threshold 740), anl: 1 (threshold 395)
DEBUG:  pg_database: vac: 0 (threshold 50), anl: 1 (threshold 50)
DEBUG:  pg_namespace: vac: 0 (threshold 51), anl: 0 (threshold 51)
DEBUG:  pg_depend: vac: 0 (threshold 1291), anl: 8 (threshold 670)
DEBUG:  pg_db_role_setting: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_tablespace: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_shdepend: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_shdescription: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_ts_config: vac: 0 (threshold 53), anl: 0 (threshold 52)
DEBUG:  pg_ts_dict: vac: 0 (threshold 53), anl: 0 (threshold 52)
DEBUG:  pg_ts_parser: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_ts_template: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_extension: vac: 0 (threshold 50), anl: 1 (threshold 50)
DEBUG:  pg_index: vac: 0 (threshold 72), anl: 0 (threshold 61)
DEBUG:  pg_cast: vac: 0 (threshold 89), anl: 0 (threshold 70)
DEBUG:  pg_toast_2619: vac: 0 (threshold 52), anl: 0 (threshold 51)
DEBUG:  pg_toast_2618: vac: 0 (threshold 84), anl: 0 (threshold 67)
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  shmem_exit(0): 8 callbacks to make
DEBUG:  proc_exit(0): 2 callbacks to make
DEBUG:  exit(0)
DEBUG:  shmem_exit(-1): 0 callbacks to make
DEBUG:  proc_exit(-1): 0 callbacks to make
DEBUG:  reaping dead processes
DEBUG:  server process (PID 21628) exited with exit code 0
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  InitPostgres
DEBUG:  my backend ID is 2
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  autovacuum: processing database "patientSim"
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  pg_statistic: vac: 0 (threshold 127), anl: 0 (threshold 88)
DEBUG:  pg_type: vac: 5 (threshold 116), anl: 14 (threshold 83)
DEBUG:  pg_authid: vac: 7 (threshold 50), anl: 8 (threshold 50)
DEBUG:  pg_proc: vac: 0 (threshold 548), anl: 0 (threshold 299)
DEBUG:  pg_class: vac: 7 (threshold 108), anl: 20 (threshold 79)
DEBUG:  pg_attribute: vac: 36 (threshold 499), anl: 111 (threshold 275)
DEBUG:  pg_constraint: vac: 2 (threshold 50), anl: 5 (threshold 50)
DEBUG:  pg_operator: vac: 0 (threshold 196), anl: 0 (threshold 123)
DEBUG:  pg_opclass: vac: 0 (threshold 74), anl: 0 (threshold 62)
DEBUG:  pg_am: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_amop: vac: 0 (threshold 129), anl: 0 (threshold 89)
DEBUG:  pg_amproc: vac: 0 (threshold 107), anl: 0 (threshold 79)
DEBUG:  pg_language: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_aggregate: vac: 0 (threshold 74), anl: 0 (threshold 62)
DEBUG:  pg_rewrite: vac: 0 (threshold 71), anl: 0 (threshold 61)
DEBUG:  pg_trigger: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_description: vac: 0 (threshold 740), anl: 0 (threshold 395)
DEBUG:  pg_database: vac: 0 (threshold 50), anl: 1 (threshold 50)
DEBUG:  config: vac: 10 (threshold 50), anl: 19 (threshold 50)
DEBUG:  pg_namespace: vac: 0 (threshold 51), anl: 0 (threshold 51)
DEBUG:  pg_depend: vac: 16 (threshold 1291), anl: 46 (threshold 670)
DEBUG:  pg_db_role_setting: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_tablespace: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_shdepend: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_shdescription: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_ts_config: vac: 0 (threshold 53), anl: 0 (threshold 52)
DEBUG:  pg_ts_dict: vac: 0 (threshold 53), anl: 0 (threshold 52)
DEBUG:  pg_ts_parser: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_ts_template: vac: 0 (threshold 51), anl: 0 (threshold 50)
DEBUG:  pg_extension: vac: 0 (threshold 50), anl: 0 (threshold 50)
DEBUG:  pg_collation: vac: 0 (threshold 234), anl: 0 (threshold 142)
DEBUG:  pg_attrdef: vac: 1 (threshold 50), anl: 3 (threshold 50)
DEBUG:  pg_index: vac: 2 (threshold 72), anl: 6 (threshold 61)
DEBUG:  pg_cast: vac: 0 (threshold 89), anl: 0 (threshold 70)
DEBUG:  pg_toast_2619: vac: 0 (threshold 52), anl: 0 (threshold 51)
DEBUG:  pg_toast_2618: vac: 0 (threshold 84), anl: 0 (threshold 67)
DEBUG:  pg_toast_16409: vac: 0 (threshold 50), anl: 148 (threshold 50)
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  shmem_exit(0): 8 callbacks to make
DEBUG:  proc_exit(0): 2 callbacks to make
DEBUG:  exit(0)
DEBUG:  shmem_exit(-1): 0 callbacks to make
DEBUG:  proc_exit(-1): 0 callbacks to make
DEBUG:  reaping dead processes
DEBUG:  server process (PID 21659) exited with exit code 0

secConnect.getHost()的结果是什么?是 public 地址吗?如果您直接连接到服务器 运行 数据库可能会尝试:

session.setPortForwardingL(5432, "127.0.0.1", 5432);