2016-06-21 119 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

相關問題