【发布时间】:2019-10-16 08:31:59
【问题描述】:
我想使用 Docker for Windows 在我的公司网络上托管 Shiny 应用程序。
如何设置 Docker、odbc.ini、odbcinst.ini、freetds.conf 或其他文件,以便我的 Shiny 应用程序可以从内部 Microsoft SQL Server (2016) 数据库中查询数据?数据库服务器不在运行 Docker 容器的同一台机器上运行。
我不知道我是否需要更新版本的 FreeTDS,或者我是否错误地配置了其中一个文件。我尝试在所有文件中使用服务器的 IP 地址而不是 sql-server.host.com,但在下面得到相同的错误消息。
$tsql -C 输出:
Compile-time settings (established with the "configure" script)
Version: freetds v1.00.104
freetds.conf directory: /etc/freetds
MS db-lib source compatibility: no
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
OpenSSL: no
GnuTLS: yes
MARS: no
$odbcinst -j 输出:
unixODBC 2.3.6
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /root/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
$cat etc/odbcinst.ini 输出:
[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
$cat etc/odbc.ini 输出:
[sql-server]
driver = FreeTDS
server = sql-server.host.com
port = 1433
TDS_Version = 4.2
$cat etc/freetds/freetds.conf 输出:
[sql-server]
host = sql-server.host.com
port = 1433
tds version = 4.2
R 中的命令给出错误:
con <- dbConnect(odbc::odbc(),
driver = "FreeTDS",
server = "sql-server.host.com",
port = 1433,
database = "database name",
TDS_Version = 4.2)
错误:
Error: nanodbc/nanodbc.cpp:950: 08001: [FreeTDS][SQL Server]Unable to connect to data source
Execution halted
Docker 文件:
# Install R version 3.5.3
FROM r-base:3.5.3
# Install Ubuntu packages
RUN apt-get update && apt-get install -y \
sudo \
gdebi-core \
pandoc \
pandoc-citeproc \
libcurl4-gnutls-dev \
libcairo2-dev/unstable \
libxt-dev \
libssl-dev \
unixodbc unixodbc-dev \
freetds-bin freetds-dev tdsodbc
# Edit odbc.ini, odbcinst.ini, and freetds.conf files
RUN echo "[sql-server]\n\
host = sql-server.host.com\n\
port = 1433\n\
tds version = 4.2" >> /etc/freetds.conf
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so" >> /etc/odbcinst.ini
RUN echo "[sql-server]\n\
driver = FreeTDS\n\
server = sql-server.host.com\n\
port = 1433\n\
TDS_Version = 4.2" >> /etc/odbc.ini
# Install R packages that are required
RUN R -e "install.packages(c('shiny', 'DBI', 'odbc'), repos='http://cran.rstudio.com/')"
# copy the app to the image
RUN mkdir /root/shiny_example
COPY app /root/shiny_example
COPY Rprofile.site /usr/lib/R/etc/
# Make the ShinyApp available at port 801
EXPOSE 801
CMD ["R", "-e", "shiny::runApp('/root/shiny_example')"]
Docker 构建和运行命令:
docker build . -t shiny_example
docker run -it --network=host -p 801:801 shiny_example
请注意,以下 R 代码可在我的运行 Docker 容器的 Windows 机器上运行,并且我可以成功查询数据库:
library(DBI)
con <- dbConnect(odbc::odbc(),
driver = "SQL server",
server = "sql-server.host.com")
$isql -v sql-server 输出:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect
$tsql -S sql-server 输出:
locale is "en_US.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20013 (severity 2):
Unknown host machine name.
There was a problem connecting to the server
【问题讨论】:
-
看起来你需要指定用户名和密码,如果他们还没有在客户端上指定的话。
-
我需要在R代码中指定用户名和密码?在我的 Windows 机器上,我不需要指定用户名和密码,所以我猜它使用我的 Windows 登录用户名和密码,对吗?
-
是的,SQL Server 上默认使用通过 NTLM 的 Windows 登录用户,而 Linux 上没有 NTLM。我不熟悉从 Linux 连接到 SQL Server,但在我看来,用户/密码需要在
odbc.ini或freetds.conf中 -
在我的 Windows 机器上,我使用 Windows 身份验证。我是否需要要求 DBA 团队在 SQL Server 中设置不同类型的身份验证,以便我从 Docker 容器内访问数据库服务器?我什至不知道我的用户名或密码应该是什么。
-
也许我需要使用 SQL Server 身份验证登录,如下所述:Microsoft Docs
标签: r sql-server docker shiny freetds