-1

Multiple times in a day I type the same commands and therefore thought it might be good to write a shell script to save me the repetition. I also thought this might be a good way to learn a little more about shell coding, as I normally work with OOP languages.

The code should open an ssh connection to my server, then start mysql and select the database I wish to use. I'm struggling to make the code work. I guessing it's because the bash script is on my local machine and I need somehow to execute if on the remote machine. I can't find any examples of how to do this. Here's my code:

#!bin/bash

#Script to automate login to remote server and select specific mysql database

password="myPassword"
username="myUserName"
remoteuser="myUsername@my.se.rv.er.00"
echo "Attempting login to server.."

#attempt ssh connection to remote server...
status=$(ssh -o BatchMode=yes $remoteuser echo ok 2>&1)

    #if connection is made...
    if [[ $status == ok ]] ; then
        echo "connection established"
        echo "attempting mysql login..."

        #attempt to start mysql and select my database
         mysql --user=$username -p$password  -Bse"USE mydatabase; show tables;"

    elif [[ $status == "Permission denied"* ]] ; then
        echo "Permission denied"
    else
        echo "other_error"
    fi   

when I run this I get the error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

How do I fix this code? I am interpreting this error message correctly: the reason my script isn't working is because the mysql command is being ran on my machine rather than the server?

The aim of the code is not to boot a script running on my server, it's to make a complete script that connects to my server, confirms the connection and then logs me into mysql on the remote machine so that I can then continue working on it through my own terminal.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Jay Black
  • 107
  • 2
  • 12
  • Actually, I’ve been through several iterations of this calling it with bash-x to watch its behaviour including using ssh prefix to the MySQL line. I’m still get the same results. You can’t judge from the amount of code the amount of attempts made so your comment and vote is flippant – Jay Black Mar 24 '18 at 18:45
  • How am I supposed to know that? None of your attempts or processes have been stated in your question. – Aaron Rumery Mar 24 '18 at 18:46
  • @Aaron, the question seems valid. What difference does it make to add the info requested? – salah-1 Mar 24 '18 at 18:58
  • @AaronRumery I’m not sure why failed attempts are relevant to solving the problem? My general assumption when reading posts on stack is that people are asking a question because they’re trying to solve a problem and others are answering because they want to help other people learn and solve problems. I didn’t think it was any more complicated? Knowing what I’ve done wrong so far only helps people not answer erroneously. I’d assume someone answering knows what I’m doing wrong because they’ve been writing shell code longer than I have. – Jay Black Mar 24 '18 at 19:28
  • There are already many examples of SSHing into a remote server and running a MySQL command that are available on Stackoverflow and on various websites. – Aaron Rumery Mar 24 '18 at 19:32
  • @AaronRumery I’ve been looking at them, for example the post you cited as a possible duplicate. I can’t seem to get that to work. That’s why I’m asking about the error message. I want to check it’s not something to do with my set up and I’m not misreading the error message. I’m also assuming -h host in the post you cited is the IP address of the server. I’ve looked for an explanation of it but all I find is -h [host] and the IP address seems like the logical explanation. That doesn’t work either for me. – Jay Black Mar 24 '18 at 19:41

1 Answers1

0

You need to run the commands in the context of the SSH command. This can be done with a bash here document. See here: http://www.tldp.org/LDP/abs/html/here-docs.html

Example:

ssh otherhost << EOF
  ./someaction.sh 'some params'
EOF
Aaron Rumery
  • 552
  • 2
  • 9
  • That’s not what I’m asking though. I want to be able to make the whole thing automate from a client side script rather than by triggering a remote script. – Jay Black Mar 24 '18 at 18:47
  • Thanks Aaron. Thanks also for bearing with me till we got to the nub of the thing. – Jay Black Mar 24 '18 at 19:45