title: run mysql commands through ssh
body: <p>I have a set up where I automate log checking on a remote server. I have created methods like this which use the exec command to tail logs like this..</p>
  
  <pre><code>Process p = Runtime.getRuntime().exec("ssh &lt;user&gt;@&lt;domain&gt; tail -f /location/logs
  </code></pre>
  
  <p>I print the logs to my terminal where I can run regex patterns to make sure they are correct. </p>
  
  <p>Now I need to check some of the entries in the logs against some mysql tables which are also on the server. I have set up a similar method using a List to execute a series of commands to return the entries in the mysql table:</p>
  
  <pre><code>List&lt;String&gt; cmd = Arrays.asList("ssh user@domain mysql -u user -ppassword -h ipaddress", "use database", "SELECT column1, column2, etc FROM database");
      Process dumpcapProcess = Runtime.getRuntime().exec(cmd.toArray(new String[]{}));
  
      String line;
      // Reading the InputStream stream 
      BufferedReader reader = new BufferedReader(new InputStreamReader(dumpcapProcess.getInputStream()));
      while ((line = reader.readLine()) != null) {
          System.out.println(line);
  
      }
  
      // Reading the error stream for debug
      reader = new BufferedReader(new InputStreamReader(dumpcapProcess.getErrorStream()));
      while ((line = reader.readLine()) != null) {
          System.out.println(line);
  </code></pre>
  
  <p>But it doesnt work. I have played around with the syntax of the exec strings to look like this:</p>
  
  <pre><code>List&lt;String&gt; cmd = Arrays.asList("ssh", "user@domain", "mysql", "-u",  "user", "-ppassword", "-h", "ipAddress", "use databases", "SELECT columns FROM database");
  </code></pre>
  
  <p>and now i I get this error:</p>
  
  <blockquote>
    <p>Usage: mysql [OPTIONS] [database]
      -?, --help          Display this help and exit.
      -I, --help          Synonym for -?
      --auto-rehash       Enable automatic rehashing. One doesn't need to use
                          'rehash' to get table and field completion, but startup
                          and reconnecting may take a longer time. Disable with
                          --disable-auto-rehash.
                          (Defaults to on; use --skip-auto-rehash to disable.)
      -A, --no-auto-rehash 
                          No automatic rehashing. One has to use 'rehash' to get
                          table and field completion. This gives a quicker start of
                          mysql and disables rehashing on reconnect.
      --auto-vertical-output 
                          Automatically switch to vertical output mode if the
                          result is wider than the terminal width.
      -B, --batch         Don't use history file. Disable interactive behavior.
                          (Enables --silent.)
      --bind-address=name IP address to bind to.
      --character-sets-dir=name 
                          Directory for character set files.
      --column-type-info  Display column type information.
      -c, --comments      Preserve comments. Send comments to the server. The
                          default is --skip-comments (discard comments), enable
                          with --comments.</p>
  </blockquote>
  
  <p>How can I run the mysql commands to return the result to my eclipse terminal?</p>