flask db upgrade - (1045, "Access denied for user '<user>'@'<ip>' (using password: YES)")

I'm trying to follow the instructions on Miguelgrinberg's tutorial to set up my MySQL database up and running. I am getting this error when I run flask db upgrade

sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1045, "Access denied for user 'brastile'@'' (using password: YES)")

I've checked and rechecked, my connection URL (best I can tell) is correct:

SQLALCHEMY_DATABASE_URI = 'mysql://brastile:<my password>$<my database name>'

Thank you guys!

Hmm, that IP seems incorrect. Can you also double check you have reloaded your webapp, and double check your password / databases etc are correctly named / correctly escaped etc?

Done, still same issue. Don't believe I need to escape anything since neither my pwd of db name have any special characters. Unless I'm missing something?

Here's another thing to try


Does that work for you?

ERROR 2005 (HY000): Unknown MySQL server host 'HOSTNAME' (2)

I was working on this while pythonanywhere updates were going on, could some wires have been crossed?

no- u need to put in your own username / hostname / db name

I've corrected the issue, but:

ERROR 1045 (28000): Access denied for user 'brastile'@'' (using password: YES)

CORRECTION: Changed the password and now the above command works. Will try on the app now.

Hi PythonAnywhere wizards, I have exactly the same error:

Error running WSGI application 
2022-01-14 01:41:28,147: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError)   
(1045, "Access denied for user 'USER'@'' (using password: YES)")

I use environment variables, here is a code snippet:

USER = os.getenv("USER")
DP = os.getenv("DP")
DB = os.getenv("DB")
# SQL engine
engine = db.create_engine('mysql+pymysql://')
connection = engine.connect()
metadata = db.MetaData()

Is there something wrong with this code? I tried to find answers elsewhere on the Internet but I am still stuck with this issue. I have a doubt about special characters. I tried putting credentials in plain text (just for testing, I don't want this option for security purposes), without escaping special characters and it worked, escaping them, it didn't. I am also wondering if I should put USER, DP and DB between brackets or something similar in here : engine = db.create_engine('mysql+pymysql://')

Other than that, I followed the instructions here to set up environment variables:

I tried mysql -u USERNAME -h HOSTNAME -p 'USERNAME$DATABASENAME' and it works after changing the password to one without special characters.

Can you help please?

Yes, you'll definitely need to interpolate the variables USER, DP and DB into that connection string. Assuming you're using Python 3.6 or later, an f-string is a good way to do that:

engine = db.create_engine(f'mysql+pymysql://{USER}:{DP}{DB}')