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

R odbc::odbcListDrivers() does not list dirver in /opt/homebrew/etc/odbcinst.ini

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

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

brew install unixodbc

然后我尝试按照 https://db.rstudio.com/best-practices/drivers/

上的建议安装 freetds 驱动程序
# SQL Server ODBC Drivers (Free TDS)
  brew install freetds --with-unixodbc

我收到错误:

Error: invalid option: --with-unixodbc

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

brew install freetds

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

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

接下来我尝试根据https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql-server-macos?view=sql-server-ver15 and as recommended on https://db.rstudio.com/databases/microsoft-sql-server/

为SQL服务器(macOS)安装Microsoft ODBC驱动程序

我运行:

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

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

odbcinst -j

结果:

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

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

中注册
view /opt/homebrew/etc/odbcinst.ini

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

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/opt/homebrew/lib/libmsodbcsql.17.dylib
UsageCount=1

在 Rstudio 中,我再次尝试列出驱动程序并找到 none:

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

我尝试了很多东西,包括:

  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
    
  3. 复制/opt/homebrew/etc/odbc.ini的内容到用户级文件/Gina/.odbc.ini

  4. 设置环境变量:

     export ODBCSYSINI=/opt/homebrew/etc
    

请注意,当我 运行:

odbcinst -q -s

找到驱动程序:

[ODBC Driver 17 for SQL Server]

因此,看来 odbc::odbcListDrivers() 应该能够找到驱动程序。任何想法或建议将不胜感激!谢谢!

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

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

Error: nanodbc/nanodbc.cpp:1021: 00000: [unixODBC][Driver Manager]Data source name not found and no default driver specified 

Driver Connection Window with Error

我尝试时遇到同样的错误:

isql -v ODBC Driver 17 for SQL Server myusername mypassword
[IM002][unixODBC][Driver Manager]Data source name not found and no     
default driver specified

编辑 2: 我将数据源名称(odbc.ini 文件中的第一行)更改为“SQLSever”并将服务器名称参数更改为服务器(我在系统级文件和用户级文件中都这样做了)。现在,当我 运行 下面的命令尝试连接时(尽管遇到了一个新错误)。

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

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

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

我在 post 中读到一个类似的问题,M1 Mac 有这个问题,但非 M1 Mac 没有。因此,我尝试在较旧的 Macbook Pro 上重复整个过程,但没有遇到问题! odbc::odbcListDrivers() 在为 SQL Server (macOS) 安装 Microsoft ODBC 驱动程序后立即列出驱动程序。我不需要编辑或创建任何文件。

使用 M1 Mac 通过将 openssl 文件夹更改为旧版本,我能够通过终端连接到 SQL 服务器。驱动程序安装 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

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

中的较新版本更改

This solution 为我工作:

创建两个符号链接:

sudo mkdir -p /usr/local/etc
sudo ln -s /opt/homebrew/etc/odbc.ini /usr/local/etc/odbc.ini
sudo ln -s /opt/homebrew/etc/odbcinst.ini /usr/local/etc/odbcinst.ini