在 Python 中将 MySQL 与 AWS Lambda 结合使用的问题
我正在尝试使用 AWS Lambda Python(Python 初学者)启动并运行,但在包含 MySQL 依赖项时遇到了一些问题.我正在尝试按照此处 在我的 Mac 上.
I am trying to get up and running with AWS Lambda Python (beginner in Python btw) but having some problems with including MySQL dependency. I am trying to follow the instructions here on my Mac.
对于第 3 步,我在项目根目录执行命令时遇到一些问题
For step number 3, I am getting some problems with doing the command at the root of my project
sudo pip install MySQL-python -t /
错误:
异常:回溯(最近一次调用最后一次):文件/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py",第 122 行,在 mainstatus = self.run(options, args)运行中的文件/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py",第 311 行os.path.join(options.target_dir, item)文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py",第 292 行,移动中引发错误,目标路径 '%s' 已经存在"% real_dst错误:目标路径'/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info'已经存在
Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 311, in run os.path.join(options.target_dir, item) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 292, in move raise Error, "Destination path '%s' already exists" % real_dst Error: Destination path '/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info' already exists
我最终编写了以下 lambda 函数(在我的 Mac 上运行良好),即:
I end up writing my following lambda function (works fine on my Mac), which is:
import MySQLdb
def lambda_handler(event, context):
# Open database connection
db = MySQLdb.connect(...)
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM Users"
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print ("lname=%s" %(lname))
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
我接下来要做的是转到/Library/Python/2.7/site-packages 并复制我执行 sudo pip install MySQL-python(不带 -t/)时下载的 MySQLdb 文件夹/文件(我确定我在这里做错了),到我的 lambda 项目,然后将内容与 lambda_function.py 一起压缩并上传到 AWS Lambda.
What I went on to do is go to /Library/Python/2.7/site-packages and copying over the the MySQLdb folders/files that were downloaded when I did sudo pip install MySQL-python (without -t /) (I'm sure I'm doing something wrong here), to my lambda project, and then zipped the content along with the lambda_function.py and uploaded to AWS Lambda.
然后我得到:
无法导入模块lambda_function":没有名为 MySQLdb 的模块
Unable to import module 'lambda_function': No module named MySQLdb
感谢任何帮助和建议!
编辑
能够使 sudo pip install MySQL-python -t/pathToProject 工作(感谢评论中的帮助)但现在我在运行 lambda 函数时得到了这个:
Was able to do make sudo pip install MySQL-python -t /pathToProject work (thanks for the help in the comments) but now I get this when runing the lambda function:
无法导入模块lambda_function":/var/task/_mysql.so:ELF 标头无效
Unable to import module 'lambda_function': /var/task/_mysql.so: invalid ELF header
我知道如果我在 Linux 机器上工作,那么它应该可以正常工作(正如某些人所建议的那样),但我想知道我是否可以让它在 OS X 机器上工作.
I know that if I work on a Linux box, then it should work fine (as suggested by some people), but I am wondering if I can make it work from an OS X box.
推荐答案
对于像 Lambda 这样的用例,你会更高兴使用像 PyMySQL.
For a use case like Lambda you'll be a lot happier using a pure python implementation like PyMySQL.
它是 MySQLdb 的替代品,遵循 Python 数据库 API规格.对于诸如触发的 Lambda 事件之类的大多数事情,它也一样快.
It's a drop in replacement for MySQLdb that follows the Python Database API specification. For most things like triggered Lambda events it will be just as fast.
我在生产中经常使用它,而且效果很好.
I've used it in production a lot and it works great.
相关文章