【问题标题】:Linux python3 - Can't open lib 'SQL Server'Linux python3 - 无法打开库'SQL Server'
【发布时间】:2019-12-01 19:07:05
【问题描述】:

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

这就是我尝试连接的方式:

 conn = pyodbc.connect('DRIVER={SQL Server};SERVER=%s' % (self.config.get("Sql", "DataSource")),
                        user= self.config.get("Sql", "UserId"),
                        password=self.config.get("Sql", "Password"),
                        database=self.config.get("Sql", "Catalog"))

执行此行时出现错误。错误:

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)")

不知道为什么会这样,有什么想法吗?

【问题讨论】:

标签: sql-server linux python-3.x azure azure-sql-database


【解决方案1】:

DRIVER={SQL Server} 替换为DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1}

【讨论】:

  • 你救了我的命
  • 成功了,在这之前我跟着docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/…安装了odbc驱动。
  • 只要使用“{ODBC Driver 17 for SQL Server}”就可以了
  • 我到了这里,在我的 SQLAlchemy 连接 URL 中更新了这个值:engine = create_engine("mssql+pyodbc://" + uname + ":" + pword + "@" + server + "/" + dbname + "?driver=ODBC+Driver+17+for+SQL+Server") 谢谢@ws_
  • 不要忘记允许 web 应用 ip 地址访问 Azure 中的 sql server
【解决方案2】:

我还建议您安装 ODBC 驱动程序,然后尝试使用 pyodbc。我假设您使用的是 Ubuntu 15.04+ 机器。

要安装 ODBC 驱动程序,请按照以下说明进行操作:

sudo su
wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh
sh installodbc.sh

完成后,使用 pip 安装 pyodbc 并尝试以下脚本:

import pyodbc
server = 'tcp:myserver.database.windows.net'
database = 'mydb'
username = 'myusername'
password = 'mypassword'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT @@version;")
row = cursor.fetchone()
while row:
    print row
    row = cursor.fetchone()

让我知道这是怎么回事。

干杯,
见面

【讨论】:

  • 你使用的是什么 Django 适配器?在settings.py中,需要指定Driver:ODBC Driver 13 for SQL Server
  • 谢谢,我终于解决了我的问题——需要将 pyodbc-azure 从 1.10 降级到 1.9
  • 请注意,微软的 installodbc.sh 从源代码安装 unixodbc。考虑到 unixodbc 2.3.1 可以从至少从 Xenial 开始的 Ubuntu 存储库中获得,这可能不是您想要的。此处给出了为许多操作系统安装 ODBC 驱动程序的过程:msdn.microsoft.com/en-us/library/hh568454(v=sql.110).aspx
  • 谢谢亚历山大!那是对的。这是一个临时解决方法,人们应该按照您提到的链接中的步骤操作。
  • 那个 sh 脚本坏了!
【解决方案3】:

下载依赖项取决于您的平台, (对于其他操作系统Download your Dependencies

这个例子适用于 Ubuntu:

# sudo su 
curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#Ubuntu 14.04
# curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

#Ubuntu 16.04
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

# #Ubuntu 18.04
# curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

# #Ubuntu 18.10
# curl https://packages.microsoft.com/config/ubuntu/18.10/prod.list > /etc/apt/sources.list.d/mssql-release.list

# #Ubuntu 19.04
# curl https://packages.microsoft.com/config/ubuntu/19.04/prod.list > /etc/apt/sources.list.d/mssql-release.list

exit
sudo apt-get update
sudo ACCEPT_EULA=Y apt-get install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y apt-get install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo apt-get install unixodbc-dev

然后改变,

DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1} 

DRIVER={ODBC Driver 17 for SQL Server}

【讨论】:

  • 这些说明对我有用(在 Windows 子系统 Linux (WSL2) 下运行 Ubuntu 20.04)但是我只需要设置 DRIVER={ODBC Driver 17 for SQL Server}。如果我尝试 DRIVER={/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.0.so.1.1},则会收到错误消息
  • 你救了我的命
  • ODBC 库驱动程序路径随更新而变化。您可以使用find /opt/microsoft/msodbcsql17/lib64 -name libmsodbcsql* 来获取它的路径。
【解决方案4】:

检查这些链接。它解决了我的类似问题。

Installing FreeTDS

Connecting to SQL-Azure using FreeTDS

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2016-11-22
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多