Airflow:How do I use PyMySQL with MySQL Hook?

Viewed 2042

I am trying to use MySQL in Apache Airflow. I already use PymySQL to query MySQL databases and did not have MySQLDb installed. Thus I got the error:

 from airflow.hooks.mysql_hook import MySqlHook
  File "/anaconda3/anaconda/lib/python3.6/site-packages/airflow/hooks/mysql_hook.py", line 15, in <module>
    import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'

If I go into mysql_hook.py class I already see they are using MySQLDB

import MySQLdb
import MySQLdb.cursors

What alternative I have other than importing pymysql manually instead of in a hook?

I am using Python 3.6

Thanks

1 Answers

In order for the MySQL hook to work in Airflow, you need to install the MySQL extra package. If you have installed Airflow with pip, then the following command will do:

pip install apache-airflow[mysql]

This will install the mysqlclient package, which is the one required here.

Related