Forums

2005 (HY000): Unknown MySQL server host

forgive me, i am not very good at all this stuff, but i'm trying to make something for my boyfriend (programmer) for valentine's day to surprise him. i made a flask app with an app.py that really just displays a render template and connects to a Flask-SQLAlchemy database. i got it deployed on pythonanywhere and it worked fine except for the database since it was tied to my local one. when i tried to do make a new one on the Databases tab, i'm getting errors :(

this is the error when i look at the errors page after trying to reload my app:

sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 2005 (HY000): Unknown MySQL server host 'l@vesperial.mysql.pythonanywhere-services.com' (-2)

and this is my code in app.py for the database, copied directly from the help page:

SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="vesperial",
password="(password)",
hostname="vesperial.mysql.pythonanywhere-services.com",
databasename="vesperial$coupons", 
)

app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)

i know there are a lot of other forum posts with this issue, i read all i could find but nothing helped. i noticed that, in the error log, it puts the last 1-3 characters of my password at the front of the server host name. you can see it has an "l" in front of the "@vesperial.mysql..." and i don't know why. i changed my password to test this and it's always whatever the last letters of the password are, even though in the file it is exactly as listed on the Database tab. i've tried both pip install mysql-connector-python and pip install mysqlclient in the venv console on pythonanywhere, according to other posts i've found, but it doesn't change the error. if i put my original code with my sqlite3 database, my app works and shows up on the page, just the database doesn't update.

i appreciate any help and i hope you are all having a good day and staying safe.

Does your MySQL password have an "@" in it? In particular, does it end with "@l"? If so, that would explain it. Let's say that your password was "sdjflsfajsdlfjs@1" -- then, the string interpolation used to build that database URL would produce this:

mysql+mysqlconnector://vesperial:sdjflsfajsdlfjs@1@vesperial.mysql.pythonanywhere-services.com/vesperial$coupons

The MySQL connection library would consider everything after the colon and before the first "@" to be the password, so it would try to connect to the server using the password "sdjflsfajsdlfjs" and the hostname "@1@vesperial.mysql.pythonanywhere-services.com", which would explain what you're seeing.

If that is the case, you can change your password on the "Databases" tab to one without an @, and make the same change in your code, and it should work fine.

thank you so much for your response! yes, actually i figured it to be the cause like 10 minutes after posting last night (after spending over an hour trying to figure out the issue on my own), but i couldn't find my post to delete/comment as it was in the mod approval queue. i feel so embarrassed and silly over the issue. i had driven 400 miles on 3 hours of sleep yesterday- so not the best condition to code in!

thank you for the nice explanation. i changed the password 3 times to test it beforehand and somehow they all ended up with an @ in it. problem fixed : ) i think my app is working very well now!

Excellent, glad to hear that you worked it out yourself -- that's the best way to learn :-)

excellent question and excellent answer i had the same problem and as soon as i read this i solved my problem