Question

I want to make an alias that is kept in my bashrc file to log into a remote MySQL db via SSH. Assume that I can't add/alter any files on the remote machine that I'm SSHing into. Here's the relevant code.

function ssh_mysql {
  echo "SSHing to $server"
  ssh -t -t $suser@$server <<ENDSSH
    eval "mysql -h "$host" -u $user -p $pass $db"
ENDSSH
}
alias wt_mysql=ssh_mysql

The Problem: Entering 'wt_mysql' into the terminal as an alias SSHs and logs into MySQL fine.. but when trying to enter any command/query/etc at the MySQL prompt, none of what I've submitted is executed/run. Including the 'exit' command. I have to ctrl C to get back to my local terminal. although its a bit out of my understanding I believe the problem is related to this topic, Terminating SSH session executed by bash script
How can I make sure that mysql and any subsequent commands are executed remotely?
Thanks!

Was it helpful?

Solution

I don't understand why you're using eval (or why you're passing the -t switch twice).

I would expect this ssh command to do what you want:

ssh -t $suser@$server "mysql -h '$host' -u $user -p $pass $db"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top