Forums

Can't access MySQL database from Django or Bash

Initially I was facing this error in Django when I tried to upload some new data to a newly created MySQL (just have migrated from SQLite):

django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '*****.mysql.pythonanywhere-services.com' (60)")

Then I read this forum and tried to debug this. As I understand the command below executed in PA Bash should work:

(h2h_env) 17:21 ~/h2h_project (main)$ mysql -u skyscanner -h skyscanner.mysql.pythonanywhere-services.com -p
Enter password: 
ERROR 1045 (28000): Access denied for user '*****'@'10.0.0.144' (using password: YES)

I've created a new database password recently and I know for sure that this should be correct one, but it does not allow me. As if it was still remembering some older password (I did have other databases as well, but I'm not using them anymore). Probably my issue is simple and has something to do with the authentication, but I can't figure it out

Your username is not skyscanner, it's Skyscanner

Yes, you are right with this one. PA Bash is working fine with this. However, my local script had correct settings incl username and password.

While switching from SQLite to MySQL, I've changed database settings in Django and installed mysqlclient. Did I miss anything else? My use case is that I'm running a Django command locally and this should update MySQL database on PA.

If you want to connect from your local machine, you'll need to use an SSH tunnel -- the PythonAnywhere MySQL servers aren't directly accessible from the public Internet. This help page has some options; the best one for your use case is probably the "Manual SSH tunnelling" option near the bottom of the page.

Thanks, I tried it, but it did not work either... I feel like I'm either doing something completely wrong or my case was not understood. I've tried ssh tunnel and ssh itself works just fine. But then Python connection (as per page guidance) did not work. I was meticulous about setting, but the script does not have much to debug it - it just hangs without outputting any error. I think it's trying to connect to my local sql database which I simply don't have: in my local project I put mysql settings same as in PA account.

What I am trying to achieve is running Django command locally that would add data to the website. This command simply parses data that I store on my computer and adds it to the website. So I only have one database - in PA - and both development ad production environments connect to it.

When I started switching from SQLite, I updated settings in my production environment and my website is already running on it. Did I have to update dev environment differently? My main problem is probably that I did not configure my local database settings correctly for this use case

When you have the SSH tunnel running as described in the page I linked to above (with a command like ssh -L 3306:username.mysql.pythonanywhere-services.com:3306 username@yoursshhostname) then there will be a server running on your machine which looks (to code running locally) like it is a MySQL server, but it will be tunnelling everything sent to it over the SSH connection to the real live MySQL server inside PythonAnywhere.

So you'll need to change the settings in your code to connect to that local server -- that is, the hostname will be "127.0.0.1" and the port will be 3306. The username and password will be the same as the ones you'd use for code running in PythonAnywhere.

If that's what you're already doing, could you share the database connection settings that you're using (with any passwords obscured, of course)?

OMG, PA support is the best! Thanks a lot, Giles!! I tried various changes before then but ended up with a duplicate of settings in PA account, which was wrong. I did not understand how tunneling works and, therefore, missed what appears to be an obvious solution now. As soon as I changed local settings to the local ip and port, everything started to work as it should. I'm now able to upload any data to my PA-hosted MySQL database.

Hmm... happiness did not last long. I populated part of my content successfully yesterday. But when I tried to continue today, I've got this:

ssh -L 3306:Skyscaner.mysql.pythonanywhere-services.com:3306 Skyscanner@ssh.pythonanywhere.com
<<<<<<:>~ PythonAnywhere SSH. Help @ https://help.pythonanywhere.com/pages/SSHAccess
Skyscanner@ssh.pythonanywhere.com's password: 
08:17 ~ $ channel 3: open failed: connect failed: Name or service not known

So ssh connects fie and I can see my remote repository if I wish to, but when I run my script I get this:

django.db.utils.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")

(I this triggers the above error in ssh terminal)

Are you sure that the settings you used on Sunday are the settings you used today?