Dumping mysql through ssh tunnel


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dumping mysql through ssh tunnel
# 1  
Old 06-24-2014
Dumping mysql through ssh tunnel

Hello wise scripters
This is my first post and I am glad to join this community.

I am trying to make work a mysqldump via ssh tunnel in a shell script.
The problem is that running this sequence of commands manually works fine, but when I put these in a bash script they give me the following error

"mysqldump: Got error: 2003: Can't connect to MySQL server on '127.0.0.1' (111) when trying to connect"


piece of script:
Code:
ssh -N xxx.xxx.xxx.xxx -L 3307:127.0.0.1:3306& 
mysqldump -P 3307 -h 127.0.0.1 -u user -ppassword remote_db_name | mysql local_db_namei -u user -ppassword

I hope someone can help to get out or at least give the right hint to try something else


Thanks!
# 2  
Old 06-24-2014
Does your SSH connect? Is port forwarding allowed on the remote host? Is your SSH session still running when your script tries to run the dump?
# 3  
Old 06-24-2014
It probably means what it says -- it can't connect to your mysql server. You should make sure ssh actually works in your script. Try
Code:
ssh -N xxx.xxx.xxx.xxx -L 3307:127.0.0.1:3306 echo "hello world"

just to see if it prints hello world. If not, there's an issue with your ssh.
# 4  
Old 06-24-2014
Can I suggest also using -g and -T:

Code:
$ ssh -g -T -N xxx.xxx.xxx.xxx -L 3307:127.0.0.1:3306&

For debugging purposes you could also run with the -v option in another terminal without background & and see if mysqldump is connecting properly you should see something like this:

Code:
$ ssh -v -g -T -N xxx.xxx.xxx.xxx -L 3307:127.0.0.1:3306
...
debug1: Local forwarding listening on 0.0.0.0 port 3307.
debug1: channel 1: new [port listener]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Connection to port 3307 forwarding to 127.0.0.1 port 3306 requested.
debug1: channel 2: new [direct-tcpip]
debug1: channel 2: free: direct-tcpip: listening port 3307 for 127.0.0.1 port
3306, connect from 127.0.0.1 port 65247 to 127.0.0.1 port 3307, nchannels 3

# 5  
Old 06-24-2014
Quote:
Originally Posted by Corona688
It probably means what it says -- it can't connect to your mysql server. You should make sure ssh actually works in your script. Try
Code:
ssh -N xxx.xxx.xxx.xxx -L 3307:127.0.0.1:3306 echo "hello world"

just to see if it prints hello world. If not, there's an issue with your ssh.
The SSH connection could work, but if the remote host is configured to disallow port forwarding the local dump process could still fail to connect.

I'm not familiar enough with the behavior of various versions of SSH to know what happens if port forwarding is disallowed. The basic SSH connection could still work.
# 6  
Old 06-25-2014
Quote:
Originally Posted by achenle
The SSH connection could work, but if the remote host is configured to disallow port forwarding the local dump process could still fail to connect.
He implies it works in shell but not in script, so I think it's safe enough to assume port forwarding is allowed. Autologin may be failing in some fashion however.
# 7  
Old 06-30-2014
As said by Corona688, I implied that all the commands worked fine in shell.

I checked out if it is an autologin (sshpass) issue and i think thats not the case because if i run all commands in script except "mysqldump -P 3307 -h 127.0.0.1 -u dbuser -pdbpass dbname --result-file=dump.sql" all works fine and i can (remote) dump the DB.

Any suggestion on how i can troubleshoot this??? looking at mysql logs didn't help

thanks in advance


edit: i will post the entire script asap
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Tunnel using SSH

I am not clear with the part of concept of Tunneling using ssh. ssh -f -N -L 1029 192.168.1.47:25 james@192.168.1.47 I found out that above code works for me . but didn't quite well understood how ti works and need to ask you guys some questions. since we are using tunnel through ssh ... (2 Replies)
Discussion started by: lobsang
2 Replies

2. Solaris

Tunnel X over ssh for 11.3

Hello Solaris experts: Trying to bring the 11.3 gdm screen over ssh to a Linux Box: I did the following: 1. made chanes to /etc/ssh/sshd_config & bounced ssh daemon: # X11 tunneling options X11Forwarding yes X11DisplayOffset 10 X11UseLocalhost yes 2. From the remote Linux box: ... (6 Replies)
Discussion started by: delphys
6 Replies

3. Proxy Server

WebSocket over SSH tunnel - is it possible?

Hello, I have a video streaming application that utilizes a WebSocket for the server <-> client communication. My goal is to make the video streaming service available over the internet in the cases where neither the server nor client have public IPs. One way to do this is over a VPN... (8 Replies)
Discussion started by: Vladislav
8 Replies

4. UNIX for Advanced & Expert Users

Ssh tunnel question

Hi all I have a suite of scripts that ssh to remote servers within a cluster and run some tests. This is done from a central server so that all of the test results can be captured in one location. Problem is I now have 509 tests and the number is growing. The scripts work by establishing a... (2 Replies)
Discussion started by: steadyonabix
2 Replies

5. IP Networking

Help with SSH tunnel?

I have a Java web app on machine (X) that needs to talk to an LDAP server (Y) on :636, but the LDAP server is only accessible on a particular network. I can login to a machine (Z) on that network from X, and this machine can talk to the LDAP server on :636. How can I tunnel so that X can... (2 Replies)
Discussion started by: spacegoose
2 Replies

6. UNIX for Dummies Questions & Answers

SSH tunnel working for ssh but not for sshfs

I'm trying to setup a link between my home pc (work-machine) and a server at work (tar-machine) that is behind a gateway (hop-machine) and not directly accessible. my actions: work-machine$ ssh -L 1234:tar-machine:22 hop-machine work-machine$ ssh -p 1234 user@127.0.0.1 - shh access on... (1 Reply)
Discussion started by: Vathau
1 Replies

7. Cybersecurity

RDP over SSH Tunnel

Hi all, I'm trying have an alternative way of connecting into a Corporate network. Mostly in case the VPN down as I cannot also change the security policy. I want to expose windows RDP over ssh tunnel. I have 3 hosts in my scenario 1- Host a : Windows 2k8 has no internet access just only an... (3 Replies)
Discussion started by: h@foorsa.biz
3 Replies

8. UNIX for Advanced & Expert Users

Stopping SSH tunnel

I have initiated a tunnel for vncserver. now i want to stop it. is there any way except sleep option? (2 Replies)
Discussion started by: majid.merkava
2 Replies

9. UNIX for Advanced & Expert Users

ssh decipher a tunnel

Two question here, but it's only one on the protocol point of view. If two persons use the same key to connect to a SSH server is there a risk they can decipher the other tunnel. In other terms is that less safe than if they have two separate keys. Same question if two persons use the same user... (2 Replies)
Discussion started by: moi
2 Replies

10. Programming

using a ssh tunnel with nx compression

hi everybody and thank you for this wondefrul forum this is my first thread posted here and i hope that i could find some help from your part (i am even sure) :D here is the situation: i am to develop an application of remote desktop access such as vnc, vpn and especially nx i want to develop... (0 Replies)
Discussion started by: bolboln01
0 Replies
Login or Register to Ask a Question