2016-07-05 9 views
6

उपयोग करने के लिए अपनी परियोजनाओं में से एक के लिए मेरे आर कार्यप्रवाह के भाग के रूप आर के माध्यम से किसी अन्य कंप्यूटर से एक SSH सुरंग बनाने, मैं एक PostgreSQL एक रिमोट सर्वर पर स्थित तालिका से डेटा में लोड।PostgreSQL तालिका

मेरा कोड इस तरह दिखता है (अनामित प्रमाण पत्र)।

मैं पहली बार टर्मिनल में दूरस्थ सर्वर के लिए एक ssh कनेक्शन खोलें।

ssh -p Port -L LocalPort:IP:RemotePort servername" 

मैं तो आर

# Load the RPostgreSQL package 
library("RPostgreSQL") 

# Create a connection 
Driver <- dbDriver("PostgreSQL") # Establish database driver 
Connection <- dbConnect(Driver, dbname = "DBName", host = "localhost", port = LocalPort, user = "User") 

# Download the data 
Data<-dbGetQuery(Connection,"SELECT * FROM remote_postgres_table") 

यह दृष्टिकोण ठीक काम करता है में postgres डेटाबेस से कनेक्ट है, और मैं कोई समस्या नहीं के साथ डेटा डाउनलोड करने के लिए कर रहा हूँ।

हालांकि, मैं पहले कदम करना चाहता हूं - यानी, टर्मिनल की बजाय आर में एसएसएच कनेक्शन बनाना। त्रुटि के साथ, ऐसा करने का मेरा प्रयास यहां दिया गया है।

# Open the ssh connection in R 
system("ssh -T -p Port -L LocalPort:IP:RemotePort servername") 

# Load the RPostgreSQL package 
library("RPostgreSQL") 

# Create a connection 
Driver <- dbDriver("PostgreSQL") # Establish database driver 
Connection <- dbConnect(Driver, dbname = "DBName", host = "localhost", port = LocalPort, user = "User") 

# Download the data 
Data<-dbGetQuery(Connection,"SELECT * FROM remote_postgres_table") 

Error in postgresqlExecStatement(conn, statement, ...) : 
RS-DBI driver: (could not Retrieve the result : server closed the connection unexpectedly 
This probably means the server terminated abnormally 
before or while processing the request. 

मेरे सवाल का स्पष्ट करने के लिए, मैं इस पूरे कार्यप्रवाह आर में पूरी तरह से टर्मिनल में कोई कदम के बिना प्रदर्शन (एक संबंध स्थापित, PostgreSQL डेटा डाउनलोड) करना चाहते हैं।

+2

'system2 (" ssh "का उपयोग कर सकते हैं, ग (के रूप में" - L8080: स्थानीय होस्ट: 80 "," एन "," टी ", "अन्यहोस्ट"), प्रतीक्षा = गलत) लिनक्स पर मेरे लिए काम किया। विंडोज़ पर काम नहीं करता है, हालांकि, 'फोर्क' की कमी के कारण, इसलिए आपको पृष्ठभूमि में कुछ चाहिए (जैसे 'समांतर' या [भविष्य '] (https://github.com/HenrikBengtsson/future) एक और आर सत्र चलाने के लिए)। इसे रोकना 'उपकरण :: पस्किल' के साथ काम कर सकता है, परीक्षण नहीं किया है। – r2evans

+0

@ r2evans मेरे लिए काम किया, धन्यवाद। – Andy

उत्तर

2

@ r2evans सुझावों के अनुसार।

##### Starting the Connection ##### 
# Start the ssh connection to server "otherhost" 
system2("ssh", c("-L8080:localhost:80", "-N", "-T", "otherhost"), wait=FALSE) 

आप अपने सर्वर नाम से मेल खाने वाली सभी बोलियों को मैन्युअल रूप से ढूंढकर और टाइप करके प्रक्रिया को मार सकते हैं। चेतावनी दीजिये कि आप केवल इस बाद के संस्करण का उपयोग करना चाहते हैं यदि आप एक अपेक्षाकृत अद्वितीय सर्वर नाम का उपयोग कर रहे हैं जो अन्य प्रक्रियाओं में डुप्लीकेट होने की संभावना नहीं है।

##### Killing the Connection: Manually ##### 
# To end the connection, find the pid of the process 
system2("ps",c("ax | grep otherhost")) 
# Kill pid (x) identified by the previous grep. 
tools::pskill(x) 

##### Killing the Connection: Automatically ##### 
# To end the connection, find the pid of the process 
GrepResults<-system2("ps",c("ax | grep otherhost"),stdout=TRUE) 
# Parse the pids from your grep into a numeric vector 
Processes<-as.numeric(sub(" .*","",GrepResults)) 
# Kill all pids identified in the grep 
tools::pskill(Processes) 
0

एक विकल्प के आप plinkshell साथ

library(RPostgreSQL) 
drv <- dbDriver("PostgreSQL") 

cmd<- paste0(
    "plink ", 
    # use key and run in background process 
    " -i ../.ssh/id_rsa -N -batch -ssh", 
    # port forwarding 
    " -L 5432:127.0.0.1:5432", 
    # location of db 
    " [email protected]" 
) 

shell(cmd, wait=FALSE) 
# sleep a while before the the connection been established. 
Sys.sleep(5) 

conn <- dbConnect(
    drv, 
    host = "127.0.0.1", 
    port=5432, 
    dbname="mydb", 
    password = "pass" 
) 

dbListTables(conn)