1

When trying to connect to my local server (making a database entry - registration of a new user within a flask app) I'm getting the following error:

**_mysql_exceptions.OperationalError: (2059, <NULL>)**

Screenshot of the error Traceback

Here's MySQL import to Python:

from flask_mysqldb import MySQL

MySQL config:

# Config MySQL
app.config["MYSQL_DATABASE_HOST"] = "localhost"
app.config["MYSQL_DATABASE_USER"] = "root"
app.config["MYSQL_DATABASE_PASSWORD"] = "<password_ph>"
app.config["MYSQL_DATABASE_DB"] = "myfirstdb"
app.config["MYSQL_DATABASE_CURSORCLASS"] = "DictCursor"
# init MYSQL
mysql = MySQL(app)

MySQL workbench

Here's usage of MySQL (The following code is within a registration function in Python)

# Create cursor
cur = mysql.connection.cursor()
# Execute query
cur.execute("INSERT INTO myfirstdb.firstflaskapp(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()

The error seems to pop up at the line with:

cur = mysql.connection.cursor()

Any idea what else might cause this error, please? Thanks a lot in advance

2 Answers 2

2

Thank you @dominik-remetei, it's right if you don't want to downgrade MySQL, you can use mysql.connector in addition : pip install mysql-connector-python

Here's MySQL import to Python:

from flask import Flask
from flask_mysqldb import MySQL
import mysql.connector

MySQL config:

#config my db
db = mysql.connector.connect(
   host="localhost",
   user="root",
   passwd="<password_ph>",
   database='myfirstdb'
)
mysql = MySQL(app)

Here's usage of MySQL in your route:

cur = db.cursor()
cur.execute('insert into ...')
db.commit()
db.close()
Sign up to request clarification or add additional context in comments.

Comments

0

Same Error here. In my Case, the problem was the version of MySql.

Mysql Version 8 is not compatible with flask-sqlalchemy.

I downgraded it to 5.7.22 and everthing worked fine.

1 Comment

Thank you @public_void. I found in the MySQL documentation and ended up using the connect() method from the mysql.connector module to configure the app and it worked fine after. conn = mysql.connector.Connect(host='localhost', user='root',password='<password_ph>',database='myfirstdb')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.