0

I need to SSH into a db using Python, I'm using PythonDB for this. I saw this question which details how to do this but I can't seem to get the syntax right. Would someone be able to point me in the right direction....? I also need to use a private key, how would I go about inserting that...?

ssh -L 9990:127.0.0.0:3396 <79.xxx.xx.xxx>

database = MySQLdb.connect(host'127.0.0.0', port=3306, user='jack', passwd='pass', db='test')
Community
  • 1
  • 1
DavidJB
  • 2,272
  • 12
  • 30
  • 37
  • 1
    What's this got to do with SSH? You can't SSH into a MySQL database right off the bat? Two different things, SSH == Secure remote shell, MySQL == database engine.. they don't really work togeather in that sense. – Torxed Feb 07 '13 at 10:29
  • What you read, was two systems working togeather. **Step 1:** SSH into the machine. **Step 2:** Execute the script with the database conenction **or** **Step 1:** Create a SSH tunnel **Step 2:** Run scripts locally but whatever connections are made, are made through the tunnel. – Torxed Feb 07 '13 at 10:31
  • Hi, Sorry, I can see why my question was confusing! So what I want to do is Step1: Creat SSH tunnel using a private key Step2: Run script locally Step3: write to SQL database via the SSH connection. I though this was possible...? – DavidJB Feb 07 '13 at 11:26
  • 1
    It is possible, open a SSH tunnel (via say putty) or in the example you linked yourself via ssh in linux. You then tell what **loca** port you want the tunnel to listen on, and what **remote** port that your connection should en up on. `123 -> 80` for instance, if you connect to 127.0.0.1:123 you'll be routed to X host on port 80. that's the way the tunnel works.. it's not a `Python` implementation, it's a tunnel implementation :) Whatever script/sql statement you run on the locally tunnelned port, will be routed to the defined host on the other end. – Torxed Feb 07 '13 at 11:31

1 Answers1

2

As said in the answer to the other question in the ssh-tunnel you are forwarding from port 9990 on your local machine to the (standard mysql) port on the remove machine. To send requests through that ssh-tunnel you need to connect to port 9990 instead of 3306:

database = MySQLdb.connect(host'127.0.0.0', port=9990, ....
Community
  • 1
  • 1
Clemens Klein-Robbenhaar
  • 3,457
  • 1
  • 18
  • 27