简体   繁体   English

在AWS Lambda中无法使用pydobc在Python 3.6中使用pyodbc连接到SQL Server

[英]Unable to use pydobc to connect to SQL Server using pyodbc in Python 3.6 in AWS Lambda

I have been struggling, and am unable to figure out how to connect to SQL Server using python 3.6 via pydobc and AWS Lambda. 我一直在努力,无法弄清楚如何通过pydobc和AWS Lambda使用python 3.6连接到SQL Server。

I followed the instructions provided by AWS to create an Amazon Linux AMI on which I was able to install the Microsoft ODBC drivers (v13 and v17), upgrade the unixODBC to a supported version, and get my python code to connect to the AWS RDS SQL Server instance. 我按照AWS提供的说明创建了一个Amazon Linux AMI,可以在其上安装Microsoft ODBC驱动程序(v13和v17),将unixODBC升级到受支持的版本,并获取我的python代码以连接到AWS RDS SQL服务器实例。

However, I have not been able to figure out how to package those changes successfully to deploy this code to AWS Lambda and have it work. 但是,我无法弄清楚如何成功打包这些更改以将该代码部署到AWS Lambda并使其正常工作。

I get one of two errors, depending on how I try to reference the ODBC driver. 我收到两个错误之一,具体取决于我如何尝试引用ODBC驱动程序。 Using this syntax 使用此语法

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=servername.account.region.rds.amazonaws.com,port;DATABASE=database;UID=user;PWD=password'

I get the error: 我得到错误:

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

I have tried using other driver aliases (ODBC Driver 17 for SQL Server, ODBC Driver 13 for SQL Server), with the same results. 我尝试使用其他驱动程序别名(用于SQL Server的ODBC驱动程序17,用于SQL Server的ODBC驱动程序13),结果相同。

Using the syntax: 使用语法:

    cnxn = pyodbc.connect('DRIVER=lib/libmsodbcsql-13.so;SERVER=servername.account.region.rds.amazonaws.com,port;DATABASE=database;UID=user;PWD=password')

I get the error: 我得到错误:

'IM004', "[IM004] [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed (0) (SQLDriverConnect)")

I have the following code in my .ZIP deployment file: 我的.ZIP部署文件中包含以下代码:

  1. simple_db.py - My code to create the connection simple_db.py-创建连接的我的代码
  2. pyodbc.so - Lambda version of pyodbc from https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc ) pyodbc.so-来自https://github.com/Miserlou/lambda-packages/tree/master/lambda_packages/pyodbc的pyodbc的Lambda版本)
  3. odbcinst.ini - Attempt to use a Linux-version of odbcinst to list the driver(s). odbcinst.ini-尝试使用Linux版本的odbcinst列出驱动程序。
  4. lib/libmsodbcsql-13.so - Copied from the Amazon Linux install lib / libmsodbcsql-13.so-从Amazon Linux安装复制
  5. libodbc.so.2 - Copied from Amazon Linux install as well, attempt to deploy unixODBC version. libodbc.so.2-也可以从Amazon Linux安装中复制,尝试部署unixODBC版本。

I've toyed around with directories, and adding more libodbc*.* files from the /usr/lib64 directory, but nothing has worked so far. 我弄弄了目录,并从/ usr / lib64目录中添加了更多libodbc *。*文件,但到目前为止没有任何效果。 As well as bringing over the entire msodbcsql directory (with /etc, /include, /lib64, and /share). 以及带来整个msodbcsql目录(带有/ etc,/ include,/ lib64和/ share)。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Based in this answer: AWS Lambda function to connect to SQL Server with Python 基于此答案: AWS Lambda函数可使用Python连接到SQL Server

You will need to compile unixODBC, take its shared libraries. 您将需要编译unixODBC,并使用其共享库。 Then install the MS driver, takes its shared libs and gather together 然后安装MS驱动程序,获取其共享库并收集在一起

# Start a container that mimic the lambda environment
docker run -it --rm --entrypoint bash  -e ODBCINI=/var/task -e ODBCSYSINI=/var/task -v "$PWD":/var/task  lambci/lambda:build-python2.7

# Then, download ODBC source code, compile and take the output
curl ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.5.tar.gz -O
tar xvzf unixODBC-2.3.5.tar.gz
cd unixODBC-2.3.5
./configure  --sysconfdir=/var/task  --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-iconv-ucode-enc=UTF16LE --prefix=/home
make install
cd ..
mv /home/* .
mv unixODBC-2.3.5 unixODBC-2.3.5.tar.gz /tmp/

# Install MSsql odbc driver
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo
ACCEPT_EULA=Y yum -y install msodbcsql
export CFLAGS="-I/var/task/include"
export LDFLAGS="-L/var/task/lib"

# Then you can install pyodbc (or pip install -t . -r requirements.txt)
pip install pyodbc -t .

cp -r /opt/microsoft/msodbcsql .

cat <<EOF > odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/var/task/msodbcsql/lib64/libmsodbcsql-13.1.so.9.2
UsageCount=1
EOF

cat <<EOF > odbc.ini
[ODBC Driver 13 for SQL Server]
Driver      = ODBC Driver 13 for SQL Server
Description = My ODBC Driver 13 for SQL Server
Trace       = No
EOF

# Test if it works
python -c "import pyodbc; print(pyodbc.drivers());"
python -c 'import pyodbc;conn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server}; SERVER=YOUr_SERVER:ADD;PORT=1443;DATABASE=TestDB;UID=SA;PWD=<YourStrong!Passw0rd>");crsr = conn.cursor();rows = crsr.execute("select @@VERSION").fetchall();print(rows);crsr.close();conn.close()'

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

相关问题 无法使用 PyODBC 和 AWS Lambda 连接 RDS SQL 服务器 - Unable to connect with RDS SQL Server using PyODBC & AWS Lambda 无法在AWS Lambda for MS Sql Server中导入PyODBC - Unable to import PyODBC in AWS Lambda for MS Sql Server 使用pyodbc将Python连接到MS SQL Server - Connect Python to MS SQL Server using pyodbc 无法将 pyodbc 与 aws lambda 和 API 网关一起使用 - Unable to use pyodbc with aws lambda and API Gateway 使用 python pyodbc 库创建 AWS Lambda function 抛出错误“模块‘pyodbc’没有属性‘connect’ - Creating AWS Lambda function using python pyodbc library throw an error "module 'pyodbc' has no attribute ' connect' AWS Lambda function 连接到 SQL 服务器与 ZA7F5F35426B9274173FC9231B563 - AWS Lambda function to connect to SQL Server with Python Python pyodbc 使用 SQL Server 身份验证连接到 Sql Server - Python pyodbc connect to Sql Server using SQL Server Authentication Python PYDOBC 插入带参数的SQL Server DB - Python PYDOBC Insert Into SQL Server DB with Parameters 尝试使用 pyodbc 使用 python 连接到 sql server 时出错 - Error while trying to connect to sql server with python using pyodbc 无法在Windows 7上的Python中使用PYODBC连接到远程MS SQL服务器 - Cant Connect to remote MS SQL server using PYODBC on Windows 7 in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM