Forums

Connecting Pycharm's database panel to MySQL hosted here?

I have a paid PA account and I am trying to connect to my MySQL server using Pycharm's database panel. I am trying to follow the instructions here but no luck yet: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/

I have confirmed that I am able to SSH from this same machine so at least that part is working.

When I look at the connection settings inside Pycharm, I am asked if I am using an SSH Tunnel. Assuming that is what I need, I have filled out those fields with the SSH info in the docs, but it will not connect.

Here is a screenshot: https://screencast.com/t/vHoBh1ck

Thanks.

Actually, now I can't ssh at all: ssh: connect to host ssh.pythonanywhere.com port 22: Connection refused

I don't know if SSH just suddenly went down but I did connect earlier...?

It looks like you might have been using the wrong password to log in -- I can see quite a lot of rejected login attempts for your account from a particular IP, and then our security systems cut in and blocked the IP for an hour because it looked like someone might have been trying to brute-force your password.

The block ended about 30 minutes after your second forum post, and I see there have been both successful and failed login attempts since then -- are you still having the problem?

@giles - Yes, there were some password issues and there is also a problem using $ in the database name which I have to workaround as described here: https://youtrack.jetbrains.com/issue/DBE-10067

With that workaround, "test connect" no longer errors out but it it still stuck on "Connecting to database" in Pycharm. Do you see anything else useful in the logs?

The IDE was stuck trying to disconnect so I shut it down and went for a walk. I came back and now it connects. All good at the moment.

Lessons learned:

  1. Pycharm, and likely other Jetbrains products do not appreciate $ in the database name. You may want to add this to the FAQ with the workaround link above.
  2. Your PA password is NOT your mySQL password. :)

ah that's quite the workaround. thanks for letting us know! So you were accessing the PythonAnywhere db over ssh tunnelling? (I'm thinking I will add the link to here)

@conrad - Yes, I had to use the ssh tunneling option in the DB config options as shown here:

https://screencast.com/t/dsavq9KYp

Many thanks! I've added a "JetBrains PyCharm" section to our SSH tunnelling for MySQL help page -- any suggestions for improvements would be appreciated :-)

Looks good to me. One suggestion: On this line,

The "proxy host" should be your SSH hostname

I would remind the reader of the two potential choices mentioned at the top of the page.

Good idea -- that's done.

Hi, I'm having the same issue in PyCharm, I've looked at the workaround https://youtrack.jetbrains.com/issue/DBE-10067

But it's not at all clear to me how to substitute in my connection details.

Please can anyone provide an actual example of a jdbc connect string that works for them, substituting real passwords of course!

Snyler - I was the original person who posted this issue. It's been a while and it's a bit fuzzy but this is what I can piece together. Hope it helps a bit:

  • When you add a mySQL data source in Jetbrains, there is a URL templates section at the bottom. Add a new one with their suggested string from the https://youtrack.jetbrains.com/issue/DBE-10067 article. The string I have is

jdbc:mysql://{host::localhost}?[:{port::3306}][/{database:database/[\p{L}\p{M}\p{N}[-_.#$+]]+}?][\?<&,user={user},password={password},{:identifier}={:identifier}>]

  • Move that to the top of the list of URL templates
  • Create a new project datasource and enter the SSH info as mentioned higher in the thread. Make sure that connection works or you are finished.
  • Go back to the General tab and enter the host/user/password info for your database on PythonAnywhere.
  • The URL should look like this - jdbc:mysql://YOUR-USERNAME.mysql.pythonanywhere-services.com:3306/YOUR-USERNAME$YOUR-DB-NAME.
  • Try the Test Connection button. If that works, you are set.

Beware even if you get the side panel data source to work, you still have to do something else to be able to CRUD the database from code on your local system. I never had the stomach to get through it all. Good luck. :)

I think that the JDBC connect string should remain as it is -- you need to update the URL template that is used. I don't have PyCharm installed on my machine, but from their docs, it sounds like it should be on the left-hand-side of the "Data sources and drivers" window somewhere?

[update] Ah, looks like @joeg has already posted a much better answer :-)

@Joeg, Thanks for your response. I tried the template containing "/[\p{L}\p{M}\p{N}[-_.#$+]]+}?][\" etc but was still getting no success, the jdbc connect string was generated but with some of the substitutions not completed.

However I tried this template... jdbc:mysql://{host::localhost}?[:{port::3306}][/{database}?][\?<&,user={user},password={password},{:identifier}={:identifier}>]

which generated this... jdbc:mysql://USER.mysql.pythonanywhere-services.com/USER$DB?[={database}?][\?<&,user={user},password

I just removed the "?[={database}?][\?<&,user={user},password" from the generated string... jdbc:mysql://USER.mysql.pythonanywhere-services.com:3306/USER$DB

and that works.

Cheers, Dave (snyler)

@Giles, thanks for your response too.

ps I'm using the latest version of PyCharm - 2020.3.3, maybe they've fixed the $ issue?

Great! Glad you got it working.