Search code examples
mysqlflaskpythonanywhere

PythonAnywhere error trying to access MySQL database (Access denied)


I have setup an environment in PythonAnywhere with a simple database (new_db), following all the guides and tutorials.

I have a simple python Flask app which is trying to connect to the database using the following code:

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["DEBUG"] = True

SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
    username="WazzalJohn",
    password="my_password_here",
    hostname="WazzalJohn.mysql.pythonanywhere-services.com",
    databasename="WazzalJohn$new_db>",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

db = SQLAlchemy(app)

But i keep getting the error:

ProgrammingError: (mysql.connector.errors.ProgrammingError) 1044 (42000): Access denied for user 'WazzalJohn'@'%' to database 'WazzalJohn$new_db>'

I have tried creating a new database but get the same problem. I thought PythonAnywhere handled all these types of issues so don't know what to do... has anyone had the same problem specifically on PythonAnywhere?

I have now tried to log in through a BASH console and got similar message - screen shot is here


Solution

  • In your original code, you have an error in the database name:

    databasename="WazzalJohn$new_db>",
    

    ...should be:

    databasename="WazzalJohn$new_db",
    

    ...without the extra ">".

    The problem in the screenshot is that you're not specifying a database on the command line; you need to do something like:

    mysql -u TWarren -h TWarren.mysql.pythonanywhere-services.com -p  'TWarren$default'
    

    ...where 'TWarren$default' should be replaced with the actual database name. The single quotes around the database name are important, BTW -- if you don't use them, Bash will interpret "$default" as an environment variable and mysql will try to connect to the wrong database.