简体   繁体   中英

run mysql commands through ssh

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..

Process p = Runtime.getRuntime().exec("ssh <user>@<domain> tail -f /location/logs

I print the logs to my terminal where I can run regex patterns to make sure they are correct.

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:

List<String> 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);

But it doesnt work. I have played around with the syntax of the exec strings to look like this:

List<String> cmd = Arrays.asList("ssh", "user@domain", "mysql", "-u",  "user", "-ppassword", "-h", "ipAddress", "use databases", "SELECT columns FROM database");

and now i I get this error:

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.

How can I run the mysql commands to return the result to my eclipse terminal?

You can pass the query directly from command line using the -e argument. Example:

 mysql -uuser -ppass -hhost -Ddatabase -e'show tables'

This is untested, but it should be something like:

List<String> cmd = Arrays.asList(
    "ssh",
    "user@domain",
    "mysql",
    "-uuser",
    "-ppassword",
    "-h10.0.0.1",
    "-Ddb",
    "-e'SELECT columns FROM table'"
);

I think your Problem is that there is no space between the -p and the password. You should try to build your command like this:

mysql --user=user_name --password=your_password db_name

For more informations here is the documentation: http://dev.mysql.com/doc/refman/5.6/en/mysql.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM