mysql - R docker : Can't connect to local MySQL server through socket

标签 mysql r docker shiny golem

我使用了一个 golem 管道来打包和 dockerize 我的应用程序。

对于初学者,我正在尝试使用 docker 在 windows pc 上本地部署应用程序(也尝试在 linux 上运行它但遇到同样的问题)。该应用程序从本地 SQlite 数据库收集数据,该数据库也在我的电脑上运行(一旦部署到服务器上就会类似)。

当我将应用作为一个包运行时,应用运行正常。 但是一旦我创建了一个 docker 镜像并运行它,该应用程序启动但无法连接到我的本地 sql 数据库,返回此错误: 无法通过套接字“/var/run/mysqld/mysqld.sock”连接到本地 MySQL 服务器(2“没有这样的文件或目录”)

与应用程序内部数据库的连接如下所示:

    con =  dbConnect(RMariaDB::MariaDB(), dbname = "training_dash_db", user = "root", password = "", host = '127.0.0.1')

我的 docker 文件如下所示:

FROM rocker/tidyverse:3.5.3
RUN R -e 'install.packages("remotes")'
RUN R -e 'remotes::install_github("r-lib/remotes", ref = "97bbf81")'
RUN R -e 'remotes::install_cran("shiny")'
RUN R -e 'remotes::install_github("Thinkr-open/golem")'
RUN R -e 'remotes::install_cran("processx")'
RUN R -e 'remotes::install_cran("attempt")'
RUN R -e 'remotes::install_cran("DT")'
RUN R -e 'remotes::install_cran("glue")'
RUN R -e 'remotes::install_cran("htmltools")'
RUN R -e 'remotes::install_cran("shinydashboard")'
RUN R -e 'remotes::install_cran("shinydashboardPlus")'
RUN R -e 'remotes::install_cran("lubridate")'
RUN R -e 'remotes::install_cran("dplyr")'
RUN R -e 'remotes::install_cran("purrr")'
RUN R -e 'remotes::install_cran("plotly")'
RUN R -e 'remotes::install_cran("DBI")'
RUN R -e 'remotes::install_cran("tibbletime")'
RUN R -e 'remotes::install_cran("tsibble")'
RUN R -e 'remotes::install_cran("shinyWidgets")'
RUN R -e 'remotes::install_cran("leaflet")'
RUN R -e 'remotes::install_cran("pool")'
RUN R -e 'remotes::install_cran("RMariaDB")'
RUN R -e 'remotes::install_cran("roxygen2")'
COPY K2dashboard_*.tar.gz /app.tar.gz
RUN R -e 'remotes::install_local("/app.tar.gz")'
EXPOSE 80
EXPOSE 3306
CMD R -e "options('shiny.port'=80,shiny.host='0.0.0.0');K2dashboard::run_app()"

谢谢。

最佳答案

以下是我能看到的问题:

  • 您正在使用 127.0.0.1 作为数据库的主机。一旦进入容器,此地址指的是容器的内部 IP,而不是来自您的主机/另一个容器的地址。因此您的应用无法访问主机数据库。

  • 您还没有在您的容器中安装 MariaDB 的驱动程序

解决方法如下:

关于mysql - R docker : Can't connect to local MySQL server through socket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694187/

相关文章:

php - 对一个查询进行多项选择

mysql - 跳过mysql中的第一行导出到linux中的.txt文件

r - ggplot : Showing x-axis line for each facet plot

r - 使用 R 在一张图中绘制多个列表

windows - 是否可以在没有 Windows 10 专业版的情况下使用 Docker?

docker - 尝试从 Docker 容器访问 Kubernetes API 的问题

mysql:SELECT WHERE id IN() 等

php - 使用 where 运算符从表中获取所有值

r - 根据引用列中的值是大于还是小于 0 对数据框进行子集化

docker - Docker 是否能够运行 Azure Webjobs 之类的后台任务?