Forums

MySQL 1045 error (using-password:NO) from always-on task

I am creating an always-on task to process queued items and am not successful in connecting to MySQL. I get the following error:

1045 (28000): Access denied for user 'user'@'10.0.0.207' (using password: NO) (error thrown at `if db_connection.is_connected():`)

.

def process_queue():
 config = {
  'user': DB_USER,
  'password': DB_PWD,
  'host': DB_HOST,
  'database': DB,
 }
 try:
    db_connection = mysql.connector.connect(**config)
    db_cursor = db_connection.cursor()
    sql = """Select * FROM webhooks where queued = %s"""
    db_cursor.execute(sql, [1])
    webhooks = db_cursor.fetchall()
    db_cursor.close()
 except mysql.connector.Error as error:
    print("Failed to get records: {}".format(error))
 finally:
    if db_connection.is_connected():
        db_connection.close()
        print("MySQL connection is closed")
        ... do something 
    else:
        ... do something else
 time.sleep(60)

I run it by adding an endpoint in flask and simply posting to it from Postman and the function runs perfectly, however, as an always-on task it throws the aforementioned error and even states that I'm not using a password when I am in the config var. Was hoping to use the functions interchangeably (flask and console), but unable at this point. Any help is greatly appreciated!

How are you passing the various config parameters in to the always-on task? Are they loaded from a file somewhere, or something like that? A common cause of confusion when moving code from consoles to always-on tasks is the working directory. If, in a console, you're doing something like:

cd somedirectory
python somescript.py

...whereas in an always-on task you're doing something like:

python somedirectory/somescript.py

...then you will have a different working directory for the always-on task, so references to files using relative paths -- for example "config.txt" -- will reference different filesystem locations.

In general, it's best to do the same in the always-on task as you do in the console, and cd to the directory before running the script. You can separate multiple commands with semicolons, so the task config would look like this for the example above:

cd somedirectory; python somescript.py

The config variable is drawing data from the previously defined variables within the file being processed that draw from ENV vars.


Just to clarify, I'm calling main_app.py and in that file are the following variables as well as the function above.

 DB_HOST = 'b.mysql.pythonanywhere-services.com'
 DB_USER = 'b'
 DB = 'b$default'
 DB_PWD = os.getenv('DB_PWD')

Can you add logging to your script to check the values of those env variables when the script runs as always-on task?

So that worked when I hard coded the config within the process_queue() function. That being said, it works flawlessly when invoked via Flask as-is. I imagine it is not reading the environment vars.

How do you populate those environment vars?

Export them in an .env file in the same directory.

How do you load that env file up in your script?

Ok, I figured out where I went wrong. I added conditional logic to load the env file if the env vars were not loaded and that resolved my issue. Thank you all for your help steering me to the fix!

Excellent, glad we could help!