[英]Can't open lib 'ODBC Driver 13 for SQL Server'? Sym linking issue?
When I try to connect to a sql server database with pyodbc (on mac):当我尝试使用 pyodbc(在 mac 上)连接到 sql 服务器数据库时:
import pyodbc
server = '####'
database = '####'
username = '####@####'
password = '#####'
driver='{ODBC Driver 13 for SQL Server}'
pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1443;DATABASE='+database+';UID='+username+';PWD='+password)
I get the following error:我收到以下错误:
Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 13 for SQL Server': file not found (0) (SQLDriverConnect)")
错误:('01000',“[01000] [unixODBC][Driver Manager]无法打开 lib 'ODBC Driver 13 for SQL Server':找不到文件 (0) (SQLDriverConnect)”)
When I path in the actual driver location:当我在实际驱动程序位置路径时:
driver='/usr/local/lib/libmsodbcsql.13.dylib'
It starts working!它开始工作!
My odbcinst.ini
looks like this:我的
odbcinst.ini
看起来像这样:
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.13.dylib
UsageCount=1
How can I get my reference to driver='{ODBC Driver 13 for SQL Server}'
to start working again?如何让我对
driver='{ODBC Driver 13 for SQL Server}'
重新开始工作?
I initially used this guide to install the driver.我最初使用本指南来安装驱动程序。 And I'm using anaconda on Mac Sierra if that helps?
如果有帮助,我在 Mac Sierra 上使用 anaconda?
Running:跑步:
odbcinst -j
It yielded:它产生了:
unixODBC 2.3.4
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /Users/emehex/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
Instead of copying the files to the /etc/
directory (not sure why unixODBC thought they were there) I created a symbolic link to each file:我没有将文件复制到
/etc/
目录(不确定为什么 unixODBC 认为它们在那里),而是为每个文件创建了一个符号链接:
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
This solved the problem.这解决了问题。
In my case, I have a Mac OS and the following commands fixed the problem:就我而言,我有一个 Mac OS,以下命令解决了这个问题:
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql mssql-tools
Note 1 : It might be necessary that you need to install unixodbc
in advance to msodbcsql
and mssql-tools
as the following:注意 1 :您可能需要提前将
msodbcsql
unixodbc
mssql-tools
,如下所示:
brew install unixodbc
Note 2 : If you dont have already brew
, the Missing Package Manager for macOS, then you can install it from here: https://brew.sh/注意 2 :如果您还没有
brew
(macOS 的 Missing Package Manager),那么您可以从这里安装它: https ://brew.sh/
Note 3 : You can verifiy your installation as @emehex already mentioned above with the following commands:注意 3 :您可以使用以下命令将您的安装验证为上面已经提到的@emehex:
odbcinst -j
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
In my case, I had to change the pyodbc
database driver string to就我而言,我必须将
pyodbc
数据库驱动程序字符串更改为driver='{ODBC Driver 17 for SQL Server}'
actually, in my python code pyodbc was expecting ODBC Driver 13 but, as the ODBC Driver version was updated (because of ubuntu update) to current version ODBC Driver 17 , the problem had occurred.实际上,在我的 python 代码中pyodbc期待ODBC Driver 13但是,随着 ODBC Driver 版本更新(由于 ubuntu 更新)到当前版本ODBC Driver 17 ,问题就发生了。
In my case, I fixed the problem with three steps as follow:就我而言,我通过以下三个步骤解决了问题:
# Step1: install unixodbc
brew install unixodbc
# Step2: install Microsoft ODBC Driver for SQL Server on MacOS
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
brew update
brew install msodbcsql mssql-tools
# Step3:verify odbcinst configuration path is correct
odbcinst -j
sudo ln -s /usr/local/etc/odbcinst.ini /etc/odbcinst.ini
sudo ln -s /usr/local/etc/odbc.ini /etc/odbc.ini
I was building a custom image on top of Python and this is the configuration that made it work:我在 Python 之上构建了一个自定义图像,这是使它工作的配置:
FROM python:3.8 as pyodbc
COPY . /app
WORKDIR /app
# Required for msodbcsql17 and mssql-tools
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/10/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
# Key might change in the future, replace with new one on the logs
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA6932366A755776
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 mssql-tools
RUN apt-get install unixodbc-dev
RUN pip install pipenv
RUN pipenv install --system --deploy
CMD [ "python" , "__init__.py"]
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list >
/etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install msodbcsql17
apt-get -y install unixodbc-dev
sudo apt-get install python3-pip -y
pip3 install --upgrade pyodbc
Use above steps to istall the odbc driver correctly and everything will fall in place.使用上述步骤正确安装 odbc 驱动程序,一切都会到位。
I've tried to establish a connection to a remote MS SQL Server deployed on a (Windows) machine from an external (Linux) machine.我试图从外部 (Linux) 机器建立与部署在 (Windows) 机器上的远程 MS SQL Server 的连接。 It took me some time to realise you need to first install the drivers on the machine that tries establish connection (ie Linux in my case)!
我花了一些时间才意识到您需要首先在尝试建立连接的机器上安装驱动程序(即在我的情况下是 Linux)!
If you're using macOS/Linux what you need to do is to simply Install the Microsoft ODBC Driver for SQL Server on Linux and macOS and then follow instructions on Connecting to databases for your particular OS.如果您使用的是 macOS/Linux,您只需在 Linux 和 macOS 上安装 Microsoft ODBC Driver for SQL Server ,然后按照连接到特定操作系统的数据库的说明进行操作。
I have to add that if you are using a different driver (FreeTDS) and in your connection string you omit to mention it, it will default to driver='{ODBC Driver 17 for SQL Server}
or something like like that.我必须补充一点,如果您使用不同的驱动程序(FreeTDS)并且在您的连接字符串中省略提及它,它将默认为
driver='{ODBC Driver 17 for SQL Server}
或类似的东西。
So the solution is not to forget driver, you DB settings will look like this: 'default': { 'ENGINE': 'sql_server.pyodbc', 'HOST': '127.0.0.1', 'NAME': 'mydb', 'PORT': '1433', 'USER': 'sa', 'PASSWORD': '*****', 'OPTIONS':{ 'driver': 'FreeTDS', 'host_is_server': True, } }
所以解决方案是不要忘记驱动程序,您的数据库设置将如下所示:
'default': { 'ENGINE': 'sql_server.pyodbc', 'HOST': '127.0.0.1', 'NAME': 'mydb', 'PORT': '1433', 'USER': 'sa', 'PASSWORD': '*****', 'OPTIONS':{ 'driver': 'FreeTDS', 'host_is_server': True, } }
I was struggling with a similar issue.我正在努力解决类似的问题。
Firstly, I was following this instruction:首先,我遵循以下说明:
I thought the error was in that I used driver 17 and not driver 13 - as per the connection string visible in the Azure Portal.我认为错误在于我使用了驱动程序 17 而不是驱动程序 13 - 根据 Azure 门户中可见的连接字符串。
After playing around with it for a long time, getting this error for driver 13: " pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/usr/local/lib/libmsodbcsql.13.dylib' : file not found (0) (SQLDriverConnect)") "玩了很长时间后,驱动程序 13 出现此错误:“ pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib '/usr/local/lib /libmsodbcsql.13.dylib' : 找不到文件 (0) (SQLDriverConnect)") "
and getting a timeout error for driver 17.并获得驱动程序 17 的超时错误。
I finally found the solution in this hack .我终于在这个 hack中找到了解决方案。 The problem was OpenSSL where two versions confused up.
问题是两个版本混淆的 OpenSSL。
I'm running the app in a vertual environment in VS Code on my Mac and deploying it to Azure Web App.我在我的 Mac 上的 VS Code 中的虚拟环境中运行该应用程序并将其部署到 Azure Web 应用程序。
I simply built image top of python:3.7-alpine我只是构建了 python:3.7-alpine 的图像顶部
FROM python:3.7-alpine
COPY . /app/.
WORKDIR /app
# Install curl
RUN apk add --no-cache curl
RUN apk update && apk add curl
# Install the Microsoft ODBC driver Linux.Follow the mssql documentation: https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.8.1.1-1_amd64.apk
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.8.1.1-1_amd64.apk
# Install the package(s)
RUN apk add --allow-untrusted msodbcsql17_17.8.1.1-1_amd64.apk
RUN apk add --allow-untrusted mssql-tools_17.8.1.1-1_amd64.apk
# Install other libs
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN apk add python3 python3-dev g++ unixodbc-dev
RUN python3 -m ensurepip
RUN pip3 install --user pyodbc
# Run script
ENTRYPOINT [ "python", "-u", "run.py"]
When setting connection inside the code, you need to specify the driver version that you've installed.在代码中设置连接时,您需要指定您已安装的驱动程序版本。 For instance in my case:msdobcversion=17 so driver should be
DRIVER={ODBC Driver 17 for SQL Server}
例如在我的情况下:msdobcversion=17 所以驱动程序应该是
DRIVER={ODBC Driver 17 for SQL Server}
# Make connection
server = os.environ["server"]
database = os.environ["database"]
username = os.environ["UID"]
password = os.environ["PWD"]
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
Installation that worked on the Ubuntu 18.04.在 Ubuntu 18.04 上运行的安装。 I'm not sure if two of the
./bash_profile
and ./bashrc
exports are needed but I didn't have time to check.我不确定是否需要两个
./bash_profile
和./bashrc
导出,但我没有时间检查。
sudo apt-get update
ACCEPT_EULA=Y sudo apt-get -y install msodbcsql17 mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile \
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc \
sudo apt-get -y install unixodbc libc6 libstdc++6 libkrb5-3 libcurl3 openssl debconf unixodbc unixodbc-dev
Then as a driver in connection use ODBC Driver 17 for SQL Server
which is matching the current Azure version.然后作为连接的驱动程序,使用与当前 Azure 版本匹配的
ODBC Driver 17 for SQL Server
。
I know everyone had the same stupid error ones: but I remind, I spent 30 mins just reading this branch... error was odd space at the end of connection string我知道每个人都有同样愚蠢的错误错误:但我提醒一下,我花了 30 分钟阅读这个分支......错误是连接字符串末尾的奇数空间
SQLALCHEMY_DATABASE_URI: str = "mssql+pyodbc://sa:tt@localhost:1433/babylon_pacemaker?Trusted_connection = no&driver=ODBC+Driver+17+for+SQL+Server " - odd space :(
Funny that Alembic works well, but SQLAlchimy has an error.有趣的是,Alembic 运行良好,但 SQLAlchimy 有一个错误。
Thank you Uzzal your solution solved my problem:谢谢Uzzal,您的解决方案解决了我的问题:
Blockquote In my case, I had to change the pyodbc database driver string to driver='{ODBC Driver 17 for SQL Server}' actually, in my python code pyodbc was expecting ODBC Driver 13 but, as the ODBC Driver version was updated (because of ubuntu update) to current version ODBC Driver 17, the problem had occurred.
Blockquote 在我的情况下,我不得不将 pyodbc 数据库驱动程序字符串更改为 driver='{ODBC Driver 17 for SQL Server}' 实际上,在我的 python 代码中,pyodbc 期待 ODBC 驱动程序 13,但是随着 ODBC 驱动程序版本的更新(因为ubuntu 更新)到当前版本的 ODBC 驱动程序 17,问题已经发生。
https://stackoverflow.com/a/48849351/7694643 https://stackoverflow.com/a/48849351/7694643
It's work for me.这对我有用。 I've added it as env.
我已将其添加为环境。 variable:
多变的:
In my case after running below commands with sudo user, i used DRIVER={ODBC Driver 17 for SQL Server}, which resolved my problem.就我而言,在使用 sudo 用户运行以下命令后,我使用了 DRIVER={ODBC Driver 17 for SQL Server},这解决了我的问题。
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list >
/etc/apt/sources.list.d/mssql-release.list
apt-get update
ACCEPT_EULA=Y apt-get install msodbcsql17
apt-get -y install unixodbc-dev
sudo apt-get install python3-pip -y
pip3 install --upgrade pyodbc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.