0

Attempt to connect to MySQL-database by module MySQLdb fails and will be responded by: name 'MySQLdb' is not defined... in the servers log. Environment is not virtual, framework is Flask

The connection attempt via MySQLdb.connect(<credentials>) is in a try/except-clause and an exception will be thrown. In order to verify the correct credentials I entered host/user/password/database in the bash-console and the connect-function returned True for connect-state (=connected). But when running this in the Flask-App, then the server returns the above error message.

from flask import Flask
try:
    import MySQLdb
except Exception as e:
    print("A problem with import MySQLdb......", e)

def checkDBConnection():
    try:
        cnx = MySQLdb.connect(
        host = '<myUserName>.mysql.eu.pythonanywhere-services.com',
        user = '<myUserName>',
        password = '<myPW>',
        database = '<userName>$sensorData'
        )
        return True
    except Exception as e:
        print("Database error: ", e)
        return False

app = Flask(__name__)
#application = Flask(__name__)   # see hint in *.wsgi file

@app.route("/sensorData/<val>")
def storeDB(val):
    #return f'val is: {val}'
    check = checkDBConnection()
    if check:
        return "<p>Database access successful...........</p>"
        # Closing Database Connection
        #cnx.close()
    else:
        return "<p>Connection failed ......</p>"
4
  • Pleas show all the error messages that you see. Are you sure that MySQLdb has been installed correctly? Commented Jun 13 at 9:41
  • The MySQL-python package (MySQLdb) is no longer compatible with Python 3.x. To better assist you, please share more logs or error messages so the community can help troubleshoot further. Commented Jun 13 at 9:43
  • The MySQLdb package has not been updated since 2014, so i suspect it is no longer maintained. I suggest you switch to mysql-connector-python 9.3.0 which is up to date. Commented Jun 13 at 9:45
  • This question is similar to: No module named MySQLdb. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. (the top-scored answer is Pyhton2, but a number of the other high-scoring answers cover Python 3) Commented Jun 13 at 11:24

1 Answer 1

2

The error you're seeing is likely because you're using MySQL-python (which provides MySQLdb), but that package does not support Python 3 — it's only compatible with Python 2.x and is no longer maintained.

✅ Solution:

You can install pymysql and cryptography (needed for secure auth plugins like sha256_password or caching_sha2_password) with:

python3 -m pip install pymysql cryptography

Then, in your Python code, do the following to make pymysql act like MySQLdb:

import pymysql 
pymysql.install_as_MySQLdb()

This allows frameworks or codebases expecting MySQLdb to work seamlessly with pymysql. pymysql.install_as_MySQLdb() monkey-patches sys.modules to alias MySQLdb to pymysql.

Sign up to request clarification or add additional context in comments.

Comments

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.