R odbc::odbcListDrivers() 未在 /opt/homebrew/etc/odbcinst.ini 中列出驱动程序

gco*_*nte 6 sql-server odbc r unixodbc

我正在尝试从 R studio 连接到 Microsoft SQL Server 数据库。

我使用自制程序安装了 unixODBC 驱动程序管理器:

brew install unixodbc
Run Code Online (Sandbox Code Playgroud)

然后我尝试按照https://db.rstudio.com/best-practices/drivers/上的建议安装 freetds 驱动程序

# SQL Server ODBC Drivers (Free TDS)
  brew install freetds --with-unixodbc
Run Code Online (Sandbox Code Playgroud)

我收到错误:

Error: invalid option: --with-unixodbc
Run Code Online (Sandbox Code Playgroud)

我发现一个消息来源说该选项不再可用,因为它现在是默认选项。因此,我跑了:

brew install freetds
Run Code Online (Sandbox Code Playgroud)

然后,在 R Studio(和 R)中,我尝试列出已安装的驱动程序,但未找到:

> odbc::odbcListDrivers()
[1] name      attribute value    
<0 rows> (or 0-length row.names)
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试根据https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql安装适用于 SQL Server (macOS) 的 Microsoft ODBC 驱动程序-server-macos?view=sql-server-ver15并按照https://db.rstudio.com/databases/microsoft-sql-server/上的建议

我跑:

brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
HOMEBREW_NO_ENV_FILTERING=1 ACCEPT_EULA=Y brew install msodbcsql17 mssql-tools
Run Code Online (Sandbox Code Playgroud)

为了检查该驱动程序是否已在 odbcinst.ini 文件中注册,我首先运行:

odbcinst -j
Run Code Online (Sandbox Code Playgroud)

结果:

unixODBC 2.3.9
DRIVERS............: /opt/homebrew/etc/odbcinst.ini
SYSTEM DATA SOURCES: /opt/homebrew/etc/odbc.ini
FILE DATA SOURCES..: /opt/homebrew/etc/ODBCDataSources
USER DATA SOURCES..: /Users/Gina/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Run Code Online (Sandbox Code Playgroud)

驱动程序已在 /opt/homebrew/etc/odbcinst.ini 中注册

view /opt/homebrew/etc/odbcinst.ini
Run Code Online (Sandbox Code Playgroud)

文件内容(注意 freetds 驱动程序似乎不存在):

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=1
Run Code Online (Sandbox Code Playgroud)

在 Rstudio 中,我再次尝试列出驱动程序,但没有找到:

> odbc::odbcListDrivers()
[1] name      attribute value    
<0 rows> (or 0-length row.names)
Run Code Online (Sandbox Code Playgroud)

我还尝试了很多事情,包括:

  1. 将 /opt/homebrew/etc/odbcinst.ini 的内容复制到用户级文件 /Users/Gina/.odbcinst.ini

  2. 编辑 /opt/homebrew/etc/odbc.ini 文件(以前为空)以包含连接详细信息(尽管我计划仅在 DBI::dbConnect 命令中指定这些信息)。新编辑的文件内容:

     [ODBC Driver 17 for SQL Server]
     Driver   = ODBC Driver 17 for SQL Server
     Servername   = servername.net
     Database = databasename
     UserName     = rstudioapi::askForPassword("Database user")
     Password      = rstudioapi::askForPassword("Database password")
     Port     = 1433
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将 /opt/homebrew/etc/odbc.ini 的内容复制到用户级文件 /Gina/.odbc.ini

  4. 设置环境变量:

     export ODBCSYSINI=/opt/homebrew/etc
    
    Run Code Online (Sandbox Code Playgroud)

请注意,当我运行时:

odbcinst -q -s
Run Code Online (Sandbox Code Playgroud)

找到驱动程序:

[ODBC Driver 17 for SQL Server]
Run Code Online (Sandbox Code Playgroud)

因此,odbc::odbcListDrivers() 似乎应该能够找到驱动程序。任何想法或建议将非常感激!谢谢你!

编辑:我还尝试使用“新连接”按钮,有趣的是,我的驱动程序列在那里! 新连接窗口

但是,当我单击它并单击“测试”时,出现错误:

Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Data source name not found and no default driver specified 
Run Code Online (Sandbox Code Playgroud)

驱动程序连接窗口有错误

当我尝试时,我得到同样的错误:

isql -v ODBC Driver 17 for SQL Server myusername mypassword
[IM002][unixODBC][Driver Manager]Data source name not found and no     
default driver specified
Run Code Online (Sandbox Code Playgroud)

编辑2:我将数据源名称(odbc.ini 文件中的顶行)更改为“SQLSever”,并将 Servername 参数更改为 Server(我在系统级文件和用户级文件中都执行了此操作)。现在,当我运行以下命令时,它会尝试连接(尽管遇到新错误)。

isql -v SQLServer myusername mypassword

[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]SSL   
Provider: [OpenSSL library could not be loaded, make sure OpenSSL   
1.0 or 1.1 is installed]
[08001][unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Client    
unable to establish connection
[ISQL]ERROR: Could not SQLConnect
Run Code Online (Sandbox Code Playgroud)

然而,不幸的是,我仍然无法在 R 中连接。我得到了与以前相同的错误。

请注意,isql 命令似乎使用系统级文件,而 RStudio 使用用户级文件。两个地方的文件都是相同的。

小智 0

使用 M1 Mac,我可以通过将 openssl 文件夹更改为旧版本来通过终端连接到 SQL Server。驱动程序安装 openssl@3 (opt/homebrew/opt/openssl@3),但实际上您需要使用 openssl@1.1。

以下是我在终端中执行的步骤:

brew install openssl@1.1
rm /opt/homebrew/opt/openssl
ln -s /opt/homebrew/Cellar/openssl@1.1/1.1.1l_1 /opt/homebrew/opt/openssl
Run Code Online (Sandbox Code Playgroud)

这将创建一个符号链接“openssl”并将其指向 opt/homebrew/Cellar/openssl@1.1 文件夹内的正确文件 (1.1.1l_1)。创建符号链接之前,请验证 opt/homebrew/Cellar/openssl@1.1/1.1.1l_1 中的文件名 1.1.1l_1 未随较新版本更改