I'm developing a web application using rapache and brew. Within the R code, I want to use the RMySQL package to query a MySQL database, but I am questioning the best way to access the login details for the database from within the R script.
Following some suggestions for a similar problem with PHP, one thought was to do the following in an interactive session to save the connection details to a file outside of /var/www
:
con <- dbConnect(MySQL(), dbname = "mydb", user = "myuser", pass = "mypass")
save(con, file = "/home/myuser/sqlconnect.rda")
And then in the script run by rapache/brew, load the .rda
file:
<%
load("/home/myuser/sqlconnect.rda")
query <- "MY QUERY"
result <- dbGetQuery(con, query)
%>
I haven't tried this approach yet. I'm not even sure that my sqlconnect.rda
file will contain all of the information that it needs to connect.
Is there a more secure way to set up the dbConnect()
statement?
Update
Saving the dbConnect()
output to a file does not work, because the connection has timed out. However, source
ing a .R
file from my user directory containing
library(RMySQL)
con <- dbConnect(MySQL(), dbname = "mydb", user = "myuser", pass = "mypass")
does work.
However, I don't know how secure this approach is.