简体   繁体   English

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

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

I am trying to connect to a Microsoft SQL Server database from R studio.我正在尝试从 R Studio 连接到 Microsoft SQL Server 数据库。

I installed the unixODBC driver manager using homebrew:我使用自制软件安装了 unixODBC 驱动程序管理器:

brew install unixodbc

I then tried to install the freetds driver as recommended on https://db.rstudio.com/best-practices/drivers/然后我尝试按照https://db.rstudio.com/best-practices/drivers/上的建议安装 freetds 驱动程序

# SQL Server ODBC Drivers (Free TDS)
  brew install freetds --with-unixodbc

I received the error:我收到错误:

Error: invalid option: --with-unixodbc

I found a source saying the option is no longer available because it is default now.我发现一个消息来源说该选项不再可用,因为它现在是默认的。 Thus, I ran:因此,我跑了:

brew install freetds

In R Studio (and R) I then attempted to list the installed driver, but it was not found:然后在 R Studio(和 R)中,我尝试列出已安装的驱动程序,但未找到:

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

Next I tried to install the Microsoft ODBC driver for SQL Server (macOS) according to 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/接下来我尝试根据https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/install-microsoft-odbc-driver-sql为 SQL Server (macOS) 安装 Microsoft ODBC 驱动程序-server-macos?view=sql-server-ver15https://db.rstudio.com/databases/microsoft-sql-server/上的建议

I ran:我跑了:

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

To check that this driver was registered in an odbcinst.ini file I first ran:要检查此驱动程序是否已在 odbcinst.ini 文件中注册,我首先运行:

odbcinst -j

with result:结果:

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

The driver was already registered in /opt/homebrew/etc/odbcinst.ini驱动程序已在 /opt/homebrew/etc/odbcinst.ini 中注册

view /opt/homebrew/etc/odbcinst.ini

file contents (note the freetds driver does not appear to be present):文件内容(注意 freetds 驱动程序似乎不存在):

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

In Rstudio, I tried again to list the drivers and none were found:在 Rstudio 中,我再次尝试列出驱动程序,但没有找到:

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

I have tried many more things, including:我尝试了更多的东西,包括:

  1. copying the contents of /opt/homebrew/etc/odbcinst.ini to the user level file /Users/Gina/.odbcinst.ini将 /opt/homebrew/etc/odbcinst.ini 的内容复制到用户级文件 /Users/Gina/.odbcinst.ini

  2. editing the /opt/homebrew/etc/odbc.ini file (which was previously empty) to include connection details (though I was planning to just specify these in the DBI::dbConnect command).编辑 /opt/homebrew/etc/odbc.ini 文件(以前是空的)以包含连接详细信息(尽管我打算只在 DBI::dbConnect 命令中指定这些)。 New redacted file contents:新编辑的文件内容:

     [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. copying the contents of the /opt/homebrew/etc/odbc.ini to the user level file /Gina/.odbc.ini将 /opt/homebrew/etc/odbc.ini 的内容复制到用户级文件 /Gina/.odbc.ini

  4. setting an environmental variable:设置环境变量:

     export ODBCSYSINI=/opt/homebrew/etc

Note that when I run:请注意,当我运行时:

odbcinst -q -s

The driver is found:找到驱动程序:

[ODBC Driver 17 for SQL Server]

Thus, it appears that odbc::odbcListDrivers() should be able to find the driver.因此,似乎 odbc::odbcListDrivers() 应该能够找到驱动程序。 Any thoughts or suggestions would be very much appreciated!任何想法或建议将不胜感激! Thank you!谢谢!

EDIT: I also tried using the New Connection button and interestingly, my driver is listed there!编辑:我也尝试使用 New Connection 按钮,有趣的是,我的驱动程序列在那里! New Connection Window新的连接窗口

However, when I click on it and click Test, I get an error:但是,当我单击它并单击测试时,出现错误:

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

Driver Connection Window with Error出现错误的驱动程序连接窗口

I get the same error when I try:我尝试时遇到同样的错误:

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

EDIT 2: I changed the Data Source Name (top line in odbc.ini file) to "SQLSever" and changed the Servername argument to Server (I did this in both the system level file and the user level file).编辑 2:我将数据源名称(odbc.ini 文件中的第一行)更改为“SQLSever”,并将 Servername 参数更改为 Server(我在系统级文件和用户级文件中都这样做了)。 Now when I run the below command it tries to connect (though hits a new error).现在,当我运行以下命令时,它会尝试连接(尽管遇到新错误)。

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

However, unfortunately, I still can not connect in R. I get the same errors as before.但是,不幸的是,我仍然无法在 R 中连接。我得到与以前相同的错误。

Note that it appears that the isql command uses the system level files and RStudio uses the user level files.请注意,似乎 isql 命令使用系统级文件,而 RStudio 使用用户级文件。 The files are identical in both places.两个地方的文件是相同的。

I read in a post on a similar issue that the M1 Macs have this problem but non-M1 Macs do not.我在一篇关于类似问题的帖子中读到 M1 Mac 有这个问题,但非 M1 Mac 没有。 Thus, I tried repeating the whole process on an older Macbook Pro and did not encounter the problem!因此,我尝试在较旧的 Macbook Pro 上重复整个过程并没有遇到问题! odbc::odbcListDrivers() immediately listed the driver after installing the Microsoft ODBC driver for SQL Server (macOS). odbc::odbcListDrivers() 在为 SQL Server (macOS) 安装 Microsoft ODBC 驱动程序后立即列出驱动程序。 I did not need to edit or create any files.我不需要编辑或创建任何文件。

With M1 Mac I was able to connect to SQL Server through terminal by changing the openssl folder to an older version.使用 M1 Mac,我能够通过将 openssl 文件夹更改为旧版本来通过终端连接到 SQL Server。 The driver installs openssl@3 (opt/homebrew/opt/openssl@3) but you actually need to use openssl@1.1 instead.驱动程序安装 openssl@3 (opt/homebrew/opt/openssl@3) 但您实际上需要使用 openssl@1.1 代替。

Here are the steps i followed in terminal:以下是我在终端中遵循的步骤:

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

This will create a symlink "openssl" and point it to the correct file (1.1.1l_1) inside the opt/homebrew/Cellar/openssl@1.1 folder.这将创建一个符号链接“openssl”并将其指向 opt/homebrew/Cellar/openssl@1.1 文件夹内的正确文件 (1.1.1l_1)。 Before creating the symlink verify the file name 1.1.1l_1 has not changed with a newer version in 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 worked for me:这个解决方案对我有用:

Create two symlinks:创建两个符号链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM