I have created this very simple script that connects with SQLAlchemy to a MySQL database and runs a SELECT statement. It works great on my dev laptop. Now that I moved it to a new server, it breaks.
from sqlalchemy import create_engine
import pandas as pd
def return_connection_string(database_name, database_user, database_password, database_host, database_port):
try:
connection_string = 'mysql+mysqlconnector://' + database_user + ':' + \
database_password + '@' + database_host + ':' + database_port + '/' + database_name
print(connection_string)
return connection_string
except Exception as e:
print('Encountered error while generating connection string for MySQL!')
print(e)
def return_connection_object(database_name, database_user, database_password, database_host, database_port):
try:
connection_string = return_connection_string(
database_name, database_user, database_password, database_host, database_port)
engine = create_engine(connection_string).connect()
return engine
except Exception as e:
print('Encountered error while connecting to MySQL database!')
print(e)
mysql_conn = return_connection_object(
'xyz', 'xyz', 'xyz', 'xyz', 'xyz')
SQL_Query = pd.read_sql_query(
'select current_date as today, current_date-1 as yesterday', mysql_conn)
df = pd.DataFrame(SQL_Query, columns=['today', 'yesterday'])
df1 = df[['yesterday']]
print(df1)
mysql_conn.close()
At runtime, the script fails with this error
Encountered error while connecting to MySQL database!
No module named 'mysql'
AttributeError: 'NoneType' object has no attribute 'cursor'
I compared my server against my dev laptop and PIP shows me the same version of SQLAlchemy module: 1.3.11.
Do I need to install another module? What am I missing?
SQLAlchemydoesn't access database directly but uses other modules to work with different databases. And you need some of module which works withmysql- ie.mysql-connector-pythonor check what module shows yourpipmysql-connector-python module. I'll check and report back.