如何让 rapache/brew 安全地连接到 MySQL 数据库?

发布于 2024-12-02 22:53:03 字数 1010 浏览 5 评论 0原文

我正在使用 rapache 和brew 开发一个Web 应用程序。在 R 代码中,我想使用 RMySQL 包来查询 MySQL 数据库,但我质疑从 R 脚本中访问数据库登录详细信息的最佳方法。

根据针对 PHP 的类似问题的一些建议,一种想法是在交互式会话中执行以下操作,将连接详细信息保存到 /var/www 之外的文件中:

con <- dbConnect(MySQL(), dbname = "mydb", user = "myuser", pass = "mypass")
save(con, file = "/home/myuser/sqlconnect.rda")

然后在 rapache/brew 运行的脚本中,加载 . rda 文件:

<%
load("/home/myuser/sqlconnect.rda")
query <- "MY QUERY"
result <- dbGetQuery(con, query)
%>

我没有尝试过这种方法。我什至不确定我的 sqlconnect.rda 文件是否包含连接所需的所有信息。

是否有更安全的方法来设置 dbConnect() 语句?

更新

将 dbConnect() 输出保存到文件不起作用,因为连接已超时。但是,从我的用户目录中 source 获取 .R 文件

library(RMySQL)
con <- dbConnect(MySQL(), dbname = "mydb", user = "myuser", pass = "mypass")

确实有效。

但是,我不知道这种方法的安全性如何。

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, sourceing 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2024-12-09 22:53:03

虽然我不明白为什么与在 R 脚本文件中包含用户名/密码相比,您的建议会提高安全性,但您可以存储 dbConnect 函数的参数。示例:

con.details <- list("MySQL", dbname = "mydb", user = "myuser", pass = "mypass")
save(con.details, file='/nonpub/con.details')

并在您的brew文件中使用这些参数:

load('/nonpub/con.details')
con <- do.call(dbConnect, con.details)

当您使用rApache时,在每次运行时加载con.details没有任何意义,我宁愿将该行放在REvalOnStartup<中/code>,这也可以为您节省这个 save/load 问题:)

所以我建议添加 library(RMySQL); con.details <- list("MySQL", dbname = "mydb", user = "myuser", pass = "mypass") 到 rApache 启动并在brew 脚本中使用该参数列表。

Although I just do not see why would your suggestion boost security compared to having username/password in the R script file, you could store the parameters of your dbConnect function. Example:

con.details <- list("MySQL", dbname = "mydb", user = "myuser", pass = "mypass")
save(con.details, file='/nonpub/con.details')

And using those parameters in your brew files:

load('/nonpub/con.details')
con <- do.call(dbConnect, con.details)

As you are using rApache, loading con.details on every run just does not make any sense, I would rather put that line in REvalOnStartup, which could also save you this save/load issue :)

So I suggest to add library(RMySQL); con.details <- list("MySQL", dbname = "mydb", user = "myuser", pass = "mypass") to rApache startup and use that parameter list in the brew scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文