2016-06-21 808 views
3

我试图连接到在Amazon EC2服务器上运行的postgres数据库。我知道服务器配置正确,因为我可以从pgadmin访问它,并在intellij中作为数据源。尝试使用jsch和jdbc连接到postgres数据库会产生“连接尝试失败。导致:java.io.EOFException”

我也可以连接到我的程序内的服务器外壳时,只使用jsch和直接使用jdbc连接到另一个数据库,但只要我尝试连接到我的postgres实例通过SSH与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 

DB日志试图瞬移产生这个时候:

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 

回答

2

什么结果secConnect.getHost()?这是公共地址吗?如果您直接连接到运行数据库的服务器,请尝试:

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

我正在使用ec2服务器的ip,但用建议的地址替换它似乎解决了问题。非常感谢你! –

+1

@YashSharma *服务器*上的进程将连接到远程IP,因此就好像您在远程服务器上键入例如'psql -h remote_ip'。你不会在服务器上执行'psql -h ec2-public-ip',所以不要在那里放置公共IP。 – lmz

相关问题