1

I am trying to connect to a mysql server running at B.edu. I want to connect via SSH tunnel.

I first set this up in MySQL Workbench:

Connection Method: Standard TCP/IP via SSH
SSH hostname: B.edu
SSH username: me
MySQL hostname: B.edu
MySQL port: 3306 

This works fine. I connect and can do as much as I can through Workbench.

Now I'd like to connect via terminal command line. I try:

ssh -N -L 9999:127.0.0.1:3306 me@B.edu &
mysql -h 127.0.0.1 -u me -P 9999

The ssh connection works fine, but the mysql command fails with the error messages:

channel 2: open failed: connect failed: Connection refused

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

I see that I'm not really asking for the same thing through the two methods, because from the command line I'm using 127.0.0.1 as the mysql host name, while from Workbench I'm using B.edu as the hostname. Since the Workbench version works, I'd like to change my command line approach to work similarly, but I don't see how.

My ultimate application, once I have the command line version working, is to automate using Python and PyMySQL (presumably with paramiko to handle the tunneling), but I'd like to cross that bridge later and get the terminal line approach working first.

  • Probably something someone can answer here, but I assume this might get a better audience on something like StackOverflow, this is probably more to do with the port forwarding than MySQL: stackoverflow.com/questions/5755819/… – ConstantineK May 16 '16 at 23:28
  • That sounds very much like an X-Y problem. What is it that you're trying to achieve, exactly? – mustaccio May 17 '16 at 1:22
  • I'm trying to connect to mysql from a terminal. The host demands connections through an ssh tunnel. At some point, I'll want to do the same thing through python, but I don't need to do that yet--I mention only because I want to focus on making the tunnel work, rather than finding workarounds that don't involve a tunnel. – jbrodsky May 17 '16 at 17:39
  • @jbrodsky your workbench settings are connecting to the external IP address (B.edu) rather than localhost (127.x). Try replaying the 127.0.0.1 in SSH with B.edu. – Ziggy Crueltyfree Zeitgeister May 18 '16 at 22:14
1

Wrestled this morning with a very similar issue. This worked for me:

ssh -N -L LOCALPORT:localhost:REMOTEPORT -i MYKEYFILE USER@REMOTE
mysql --port LOCALPORT --protocol=TCP -p
0

Your SSH session might be dying for lack of a terminal (the use of & launches it to the background and disconnects STDIN and STDOUT from it).

Use two terminals, run SSH on one, and mysql on another once the SSH session is connected.

  • Looks like this wasn't it. The concern you raise is taken care of by the -N flag. – jbrodsky May 18 '16 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.