Forums

Issue in accessing MySQL database from local machine - python script

Hello, I'm trying to follow the instruction from this page: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/#from-python-code

However, I have the issue in this place:

connection = MySQLdb.connect(
        user='your PythonAnywhere database username',
        passwd='your PythonAnywhere database password',
        host='127.0.0.1', port=tunnel.local_bind_port,
        db='your database name, eg yourusername$mydatabase',
    )

The error mesage is following:

(2059, "Authentication plugin 'mysql_native_password' cannot be loaded: dlopen(/opt/homebrew/Cellar/mysql/9.0.1/lib/plugin/mysql_native_password.so, 0x0002): tried: '/opt/homebrew/Cellar/mysql/9.0.1/lib/plugin/mysql_native_password.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/mysql/9.0.1/lib/plugin/mysql_native_password.so' (no such file), '/opt/homebrew/Cellar/mysql/9.0.1/lib/plugin/mysql_native_password.so' (no such file)")


It looks like, the connection is trying to be established to my local server instead of pythonanywhere's database.

I think it's the versions mismatch between your local mysql and PA -- your database is still on older version (5.7). We can migrate your database to newer v. 8, or you could try downgrading your mysql client locally.

Thank you @pafk. But how can I request the database migration? And if the database will be migrated, I hope that the data will not be erased :)

If you send us an email at support@pythonanywhere.com, we can do the database migration -- and don't worry, your data will be safe! We will essentially take a dump of all of the data, get you set up on a MySQL 8 machine, and then import your data again. So there will be a short downtime (an overhead of 10 minutes, then however long it takes to dump/restore the data, which of course depends on how much data you have), but everything should be fine afterwards.

Dear @giles, Thank you for your support. Indeed, the reason of the issue is related to my local mysql 9.

As you might now, in order to install the mysqlclient python package on mac, you need first install mysql-client with homebrew (installation steps are here: https://pypi.org/project/mysqlclient/)

It turns out that mysql9 doesn't have 'mysql_native_password' (the issue described here: https://github.com/Homebrew/homebrew-core/issues/180498). Therefore, once I have downgraded the mysql-client to 8.4 version, the issue was resolved (don't forget to uninstall and then reinstall python mysqlclient package via --no-cache-dir)

Thanks for sharing, I'm glad you were able to solve it