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')
4
  • 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. Commented Feb 7, 2013 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. Commented Feb 7, 2013 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...? Commented Feb 7, 2013 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. Commented Feb 7, 2013 at 11:31

1 Answer 1

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, ....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.