'Mysql error 1045 (Access denied for user) while using flask_mysqldb

Operating System: MacOS

MySQL version: 8.0.16

Code to connect MySQL:

app.config['MYSQL_HOST']='localhost'
app.config['MYSQL_USERNAME']='root'
app.config['MYSQL_PASSWORD']='123456'
app.config['MYSQL_DB']='Teaching_Sys'
app.config['MYSQL_CURSORCLASS']='DictCursor'

The password is correct without any special characters inside and I already selected "use legacy password encryption" to initialize the database (Somebody solved the problem by using this trick)

and I already set up a table called Teaching_Sys

Code to insert new data into table by using flask:

username=form_reg.username.data
email=form_reg.username.data
password = sha256_crypt.hash(form_reg.password.data)
#create cursor
cur=mysql.connection.cursor()
cur.execute("INSERT INTO users(username,email,password) VALUES(%s,%s,%s)",(username,email,password))
#commit to db
mysql.connection.commit()
cur.close()

and this is what the database looks like: Database schema

The complete error message is:

MySQLdb._exceptions.OperationalError: (1045, "Access denied for user 'yiling'@'localhost' (using password: YES)")

happended in line

cur=mysql.connection.cursor()

While yiling is the name of my system account.

I also checked the user permissions of root@localhost

Root user permissions



Solution 1:[1]

OperationalError: (1045, "Access denied for user 'rajendra'@'localhost' (using password: NO)")

The answer is here

app.config['MYSQL_USERNAME']='root'

should be

app.config['MYSQL_USER']='root'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yiling Liu