在Python中使用AWS Lambda使用MySQL时出现的问题
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.
我正在尝试使用AWS Lambda Python (Python的初学者,顺便说一句)启动和运行,但是在包括MySQL依赖方面存在一些问题。我试着按照Mac上的说明操作。
For step number 3, I am getting some problems with doing the command at the root of my project
对于步骤3,我在项目的根上执行命令时会遇到一些问题
sudo pip install MySQL-python -t /
Error:
错误:
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
例外:Traceback(最近一次调用):File“/Library/Python/2.7/site-packages -packages/pip-1.5.6-py2.7.egg/pip/basecommand。py",第122行,主状态= self.run(options, args)文件"/Library/Python/2.7/site-packages/pip- 1.6 -py2.7.egg/pip/commands/install。在run os.path.join(选项)中,第311行。目标路径'%s'已经存在" % real_dst错误:目标路径'/MySQL_python-1.2.5-py2.7 -py2.7. egginfo / mysql2.7 - 2.2.5 -py2.7。egg-info”已经存在
I end up writing my following lambda function (works fine on my Mac), which is:
最后我编写了下面的lambda函数(在我的Mac上运行良好),即:
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()
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.
我接下来要做的是访问/Library/Python/2.7/site-packages并复制MySQLdb文件夹/文件,这些文件是我在sudo pip安装MySQL-python(没有-t /)时下载的(我肯定我在这里做错了),然后将内容与lambda_function一起压缩。然后上传到AWS Lambda。
Then I get:
然后我得到:
Unable to import module 'lambda_function': No module named MySQLdb
无法导入模块“lambda_function”:没有名为MySQLdb的模块
Grateful for any help and suggestions!
感谢您的帮助和建议!
EDIT
编辑
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:
能够做sudo pip安装sql -python -t /pathToProject工作(感谢在评论中的帮助),但是现在我在运行lambda函数时得到了这个:
Unable to import module 'lambda_function': /var/task/_mysql.so: invalid ELF header
无法导入模块“lambda_function”:/var/task/_mysql。所以:无效的ELF头
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.
我知道,如果我使用Linux box,那么它应该可以正常工作(正如一些人建议的那样),但是我想知道我是否可以使用OS X box。
5 个解决方案
#1
6
For a use case like Lambda you'll be a lot happier using a pure python implementation like PyMySQL.
对于像Lambda这样的用例,使用纯粹的python实现(如PyMySQL)会让您高兴得多。
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.
这是对MySQLdb的替换,它遵循Python数据库API规范。对于大多数事情,如触发Lambda事件,它将同样快速。
I've used it in production a lot and it works great.
我在生产中经常用到它,而且效果很好。
更多相关文章
- mysql python pymysql模块 增删改查 查询 fetchone
- linux下mysql配置文件my.cnf详解
- 可视化工具Navicat的使用/pymysql模块的使用
- SQLSTATE [HY000] [2002]没有这样的文件或目录
- mysql启动脚本——指定数据目录并指定配置文件
- 'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件.
- 什么是最好的Node.js mysql模块通过ssl连接到mysql?
- 多个mysql数据库怎么指定到多个不同文件目录
- ubuntu_mysql怎么判断自己的库和头文件的位置