Unauthorized mysql access in following basic flask tutorial

Hello world! I am following the basic flask app tutorial and have checked and rechecked my code. It runs fine, without error but the resulting web page just shows 'Unauthorized' and the error log contains a line:

sqlalchemy.exc.ProgrammingError:(mysql.connector.errors.ProgrammingError) 1045 (28000): Access denied for user 'username'@'' (using password: YES)

I am using the correct pw for the database. My PA username AND my database username is the same. Could that be the problem? If so, how do I delete my database and/or rename it? Any Help would be appreciated. I hate that I'm stuck on the basics... :(

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://$comments"
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299

db = SQLAlchemy(app)

if db:

class Comment(db.Model):

    __tablename__ = "comments"

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(4096))

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("main_page.html", comments=Comment.query.all())

    comment = Comment(content=request.form["contents"])
    return redirect(url_for('index'))

Just to check -- is the error that you are getting:

sqlalchemy.exc.ProgrammingError:(mysql.connector.errors.ProgrammingError) 1045 (28000): Access denied for user 'jonahhawk'@'' (using password: YES)

...and not

sqlalchemy.exc.ProgrammingError:(mysql.connector.errors.ProgrammingError) 1045 (28000): Access denied for user 'username'@'' (using password: YES)


And also to make sure, the code has this:

SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://$comments"

...with the "SOMETHING" replaced by the real password, right?

If so, that sounds like you're using the wrong password, perhaps with some kind of typo -- you can see the password in the file .my.cnf in the "Files" page, so perhaps you could try copying it from there and pasting it to your code.

Thank you for the reply. Yes to each of your questions. I double checked the pw and it is correct. I am worried that it is due to the fact that I made my sql username the same as my PA username... that was probably a mistake.

[] returns, 'Unauthorized'

There are many errors in the error log after the Access denied error. Here is the exact error.

2022-01-13 03:34:49,356: sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1045 (28000): Access denied for user 'jonahhawk'@'' (using password: YES)

The MySQL username has to be the same as your PythonAnywhere username (well, unless the PA one is more than 16 characters, in which case it's truncated), so you don't need to worry about that.

The "unauthorized" you're getting there is a different problem, though -- when you go to your page, the browser should prompt you for a username and a password -- that's a different one to the MySQL one (and, indeed your PA login password). It's the one that you configured on the "Web" page inside PythonAnywhere, right down at the bottom of the page.

The error that you quoted is from 03:34am yesterday, so perhaps all of the code changes you have made to get all of the passwords in line have actually worked, and the problem is just that you can't access the site because of that extra security layer?

That makes sense. It probably is a browser issue, blocking the login popup or something. Thank you for the help and clarification on usernames.