I preapre very simple file for connecting to external MySQL database server, like below:
from sqlalchemy import *
def run(event, context):
sql = create_engine('mysql://root:[email protected]/scraper?charset=utf8');
metadata = MetaData(sql)
print(sql.execute('SHOW TABLES').fetchall())
Doesn't work on AWS, but localy on Windows works perfectly.
Next, I install by pip install sqlalchemy --target my/dir
and prepare ZIP file to upload packages to AWS Lambda.
Run, but with failed message No module named 'MySQLdb': ModuleNotFoundError
.
Then, I use pip install mysqlclient --target my/dir
, create ZIP and again upload to AWS Lambda.
Run, but with new failed message cannot import name '_mysql': ImportError
.
So, what I should doing now?
SQLAlchemy is compatible with all these engines. For this post, we provision an Aurora PostgreSQL database. To deploy the database for our sample application, follow the instructions in Deploy an Amazon Aurora PostgreSQL DB cluster with recommended best practices using AWS CloudFormation.
SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license.
If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
SQLAlchemy includes many Dialect implementations for various backends. Dialects for the most common databases are included with SQLAlchemy; a handful of others require an additional install of a separate dialect.
The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL
Instead of mysql
you may use mysql+mysqlconnector
sql = create_engine('mysql+mysqlconnector://root:[email protected]/scraper?charset=utf8')
Then use:
pip install mysql-connector --target my/dir
Create Zip and again upload to AWS Lambda.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With