6

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?

2
  • It might be missing the MySQL client libraries. The python libraries afaik are wrappers around the C implementation. Commented Feb 8, 2019 at 1:45
  • It is possible to install? Commented Feb 8, 2019 at 7:31

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I normaly instatiate ‘db = SQLAlchemy()’ and then use db.session, any idea on how would that work in this case?
Should this be included as a layer? or the function code itself?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.