Forums

pymysql.err.OperationalError (1044 / "Access denied for user" ) when attempting to connect to database created on PythonAnywhere

Side notes: This is part of a Flask app that runs successfully locally.

Is there something else I could post or be directed toward to help solve this?

Update: The connection error has now shifted to (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused). It occurs when the database is first attempted to be accessed in the Flask app.

db.session.query(table.col_name).all() # line of current error,  in a function decorated with @app.before_first_request

Note: The following was the place of initial connection error, where attempt has the format outlined below [1]. It was apparently solved with a slight formatting change (but current error now exists).

    engine = create_engine(attempt, convert_unicode=True) # , echo=False)
    Base = declarative_base()
    Base.metadata.reflect(engine) # line of initial error

Previously:

Using the following format [1]:

'mysql+pymysql://username:password@username.mysql.pythonanywhere-services.com/username$dbname'

I am unable to connect:

2018-01-17 03:00:48,499: Error running WSGI application 2018-01-17 03:00:48,503: OperationalError: (pymysql.err.OperationalError) (1044, u"Access denied for user 'Epere063'@'%' to database 'epere063$hubzu'") (Background on this error at: http://sqlalche.me/e/e3q8)

Your username is Epere063, not epere063, so your database is Epere063$hubzu, not epere063$hubzu. You may have made the same mistake in other places.

In your update section - you have not specfied a database host, so it's trying to connect to localhost.

Right. It appears that caused the initial error. To clarify, the 2003, "Can't connect to MySQL server error is the current error, found after all combinations of capitalization was iterated (to be sure).

The same error [1] is received if attempting to connect remotely with PyMySQL:

[1] -- `OperationalError: (2003, "Can't connect to MySQL server on 'Epere063.mysql.pythonanywhere-services.com' ([Errno 61] Connection refused)")

connection = pymysql.connect(host='Epere063.mysql.pythonanywhere-services.com',
                                     user= 'Epere063',
                                     #port=3306,
                                     passwd= password,
                                     db='Epere063$hubzu',
                                     #charset= 'utf8mb4',
                                     cursorclass=pymysql.cursors.DictCursor)

I'll continue to look elsewhere in case the issue isn't related to configuration on PythonAnywhere.

Thank you

You can't connect remotely to a database on PythonAnywhere - we don't expose user databases to the internet. See http://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/ for options.

It was thought that the original attempt resulting in the same 2003 error was ostensibly not a remote connection, as the connection was attempted on the code ran from PythonAnywhere. Unless directed otherwise, I'll assume that any attempt to connect to the PythonAnywhere database, even in code ran from PA (i.e. not directly via the console) is considered remote. Thank you.

**Update: ** I am now attempting a connection method found at https://blog.pythonanywhere.com/121/ and will add a post if the condition persists (a longer post was replaced with this until I test it)

thanks, let us know how you get on!

app.config['SQLALCHEMY_DATABASE_URI']='mysql+pymysql://username:password@username.mysql.pythonanywhere-services.com:3306/username$pythondb?charset=utf8'

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'mayankbabariya.mysql.pythonanywhere-services.com' ([Errno -2] Name or service not known)")

From the logs it looks like you may have resolved that problem...?