在R的shinyapps.io上托管闪亮的应用程序时连接到数据库

时间:2021-01-06 14:26:31

I am creating a shiny application for my company that I would like to host on shinyapps.io. The application requires data grabbed from my company's database, which I am able to query on my own computer. However, instead of putting the username and password directly into my code, I saved the username and password into a .my.cnf file which is stored in my home directory. In my app, I connect in the following way:

我正在为我的公司创建一个闪亮的应用程序,我想在shinyapps.io上主持。该应用程序需要从我公司的数据库中获取数据,我可以在自己的计算机上查询。但是,我没有将用户名和密码直接放入我的代码中,而是将用户名和密码保存到存储在我的主目录中的.my.cnf文件中。在我的应用程序中,我以下列方式连接:

rmysql.settingsfile <- "~/.my.cnf"
rmysql.db1 <- "dbname"
drv <- dbDriver("MySQL")

mydb_connected <- dbConnect(drv, default.file = rmysql.settingsfile, group = rmysql.db1, user = NULL, password = NULL)

And this connects me to the database.

这将我连接到数据库。

All of this code is at the top of my app.R shiny app file. Unfortunately, after I deploy the app with rsconnect::deployApp(), although the upload is successful, the app doesn't actually run, and I receive the following error in my shinyapps.io account logs:

所有这些代码都在我的app.R闪亮的应用程序文件的顶部。不幸的是,在我使用rsconnect :: deployApp()部署应用程序后,虽然上传成功,但应用程序实际上并未运行,并且我在shinyapps.io帐户日志中收到以下错误:

Warning: Error in .local: mysql default file ~/.my.cnf does not exist

I could probably get around this error by bringing the username and password into my script, but my Company does not want me to do this. Is there any other way I can get around this?

我可以通过将用户名和密码放入我的脚本来解决此错误,但我的公司不希望我这样做。还有其他方法可以解决这个问题吗?

Thanks!

EDIT - the way I am supposedly not allowed to connect, even though it works but because the password is in the code, is like this:

编辑 - 我被认为不允许连接的方式,即使它工作,但因为密码在代码中,是这样的:

mydb_connected <- dbConnect(

 MySQL(),
  user="admin-read",
  dbname="dbname",
  host="myhost",
  password="mypass"
)

and ofcourse this works for connecting.

当然,这适用于连接。

1 个解决方案

#1


0  

Except if you use the RunAs option on RSConnect the user that runs a deployed Application is rstudio-connect.

除非您在RSConnect上使用RunAs选项,否则运行已部署应用程序的用户是rstudio-connect。

Then it doesn't find the ~/.my.cnf file.

然后它找不到〜/ .my.cnf文件。

If you want to keep your code as it is, you should log on the server that host RSConnect and create the file /home/rstudio-connect/.my.cnf and make it readable for the user rstudio-connect

如果要保持代码不变,则应该登录托管RSConnect的服务器并创建文件/home/rstudio-connect/.my.cnf并使其对用户rstudio-connect可读

#1


0  

Except if you use the RunAs option on RSConnect the user that runs a deployed Application is rstudio-connect.

除非您在RSConnect上使用RunAs选项,否则运行已部署应用程序的用户是rstudio-connect。

Then it doesn't find the ~/.my.cnf file.

然后它找不到〜/ .my.cnf文件。

If you want to keep your code as it is, you should log on the server that host RSConnect and create the file /home/rstudio-connect/.my.cnf and make it readable for the user rstudio-connect

如果要保持代码不变,则应该登录托管RSConnect的服务器并创建文件/home/rstudio-connect/.my.cnf并使其对用户rstudio-connect可读