0

I'm new to Python yet managed to create a lot of good stuff for myself. The problem I faced is how to connect to an SQL database on a remote machine (VPS, VDS, Cloud)

I know that you would likely point me out to other answers on StackOverflow. Unfortunately, there is no one solved question on the website. None of the solutions worked for me.

One more time, I don't want to connect to an SQL database on a local machine. I need to access it remotely.

Can anyone provide me with working instructions?

[https://stackoverflow.com/questions/46913504/connecting-to-mysql-db-via-ssh-with-python][1]
[https://stackoverflow.com/questions/47069829/mysql-and-python-via-ssh][1]
[https://stackoverflow.com/questions/21903411/enable-python-to-connect-to-mysql-via-ssh-tunnelling][1]
[https://practicaldatascience.co.uk/data-science/how-to-connect-to-mysql-via-an-ssh-tunnel-in-python][1]

As you can see, there are numerous upvotes. But none of the approaches helped the topic starter. Otherwise, it would be marked as solved.

Alex Cardo
  • 365
  • 2
  • 4
  • 14
  • What's preventing you from executing the code on remote machine? You can scp the script to the remote machine and execute it. – MSH Jul 27 '21 at 10:57
  • I doubt my VPS might cope with Transformers, Torch, TensorFlow, etc. I just need to send the extracted data (and write them to the DB) and then process them with PHP – Alex Cardo Jul 27 '21 at 11:02

1 Answers1

0

If you have a VPS that you can access via SSH then you can also use SSH to forward the port of the MySQL server on the VPS to your local machine.

This is something you would do for the development process of your application

Use this command to forward the remote ssh port to your local machine.

ssh -L 3306:localhost:3306 username@hostname

This way you will be able to access your VPS's MySQL server trough port 3306 on your local machine.

Here is some SSH documentation.

https://www.ssh.com/academy/ssh/tunneling/example

It is also possible to create ssh tunnels from within python but this is not recommended for a use case like yours.

Anyways, if you want to learn about this, you can read about it here

https://github.com/pahaz/sshtunnel/

mama
  • 2,046
  • 1
  • 7
  • 24
  • Thank you for this hint. But still, my goal is automatization. I need to make some calculations in Python, no matter where (localhost, another GPU-powered machine), and then connect to VPS and INSERT data into MySQL DB. – Alex Cardo Jul 27 '21 at 11:22
  • Then take a look at the sshtunnel library for python. :) – mama Jul 27 '21 at 11:23