Forums

mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user '<username>@%'@'localhost' (using password: YES)

Hi All,

I'm trying to have my python script connect to my pythonanywhere mysql database but face the following connection error. I've got a hacker account for testing as I saw this was required.

Also tried to give <username>@localhost permissions to the database on mysql but got GRANT command denied error.

Looking forward to your assistance.

  File "<phythonfile>.py", line 21, in <module>
    database='<username>$default',
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/__init__.py", line 173, in connect
    return MySQLConnection(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 102, in __init__
    self.connect(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/abstracts.py", line 735, in connect
    self._open_connection()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 252, in _open_connection
    self._ssl)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 172, in _do_auth
    self._auth_switch_request(username, password)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/mysql/connector/connection.py", line 216, in _auth_switch_request
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user '<username>@%'@'localhost' (using password: YES)

The python code is below:

import mysql.connector
import sshtunnel

sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0

with sshtunnel.SSHTunnelForwarder(
    ('ssh.pythonanywhere.com'),
    ssh_username='<username>', ssh_password='<password>',
    remote_bind_address=('<username>.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
    db = mysql.connector.connect(
        user='<username>', password='<passsword>',
        host='127.0.0.1', port=3306,
        database='<username>$default',
    )

[edit by admin: formatting]

First make sure that you can SSH into PythonAnywhere and then make sure that you have the correct passwords in the correct places. You need your account password to open the SSH tunnel and you need your MySQL password to connect to the database.

Thanks glenn,

I actually tried all your suggestions as they are also feedback from other posts but with no success.

I am able to ssh in through command line without the script and have checked the usernames and passwords and tried possible variations multiple times.

Any other suggestions?

Hi glenn,

After further hours of digging around the extensive forums, I found this post that solved the problem. https://www.pythonanywhere.com/forums/topic/12777/

That as well as the port needing to stay at port=server.local_bind_port rather than changing to 3306 as I wasn't able to get it to work initially.

Lesson of the evening... the importance of observing indentation! Thanks for your help!

Ah, great! Glad you worked it out.