简体   繁体   English

通过ssh运行mysql命令

[英]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.. 我已经创建了像这样的方法,该方法使用exec命令来拖尾这样的日志。

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. 现在,我需要对照服务器上的某些mysql表来检查日志中的某些条目。 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设置了类似的方法来执行一系列命令以返回mysql表中的条目:

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: 我已经使用了exec字符串的语法,看起来像这样:

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. 用法:mysql [OPTIONS] [database]-?, --help显示此帮助并退出。 -I, --help Synonym for -? -I,--help是-的同义词? --auto-rehash Enable automatic rehashing. --auto-rehash启用自动重新哈希。 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. 使用--disable-auto-hash禁用。 (Defaults to on; use --skip-auto-rehash to disable.) -A, --no-auto-rehash No automatic rehashing. (默认为on;使用--skip-auto-rehash禁用。)-A,--no-auto-rehash不自动进行哈希处理。 One has to use 'rehash' to get table and field completion. 必须使用“重新哈希”来获得表和字段的完成。 This gives a quicker start of mysql and disables rehashing on reconnect. 这样可以更快地启动mysql,并在重新连接时禁用重新哈希。 --auto-vertical-output Automatically switch to vertical output mode if the result is wider than the terminal width. --auto-vertical-output如果结果大于端子宽度,则自动切换到垂直输出模式。 -B, --batch Don't use history file. -B,--batch不使用历史文件。 Disable interactive behavior. 禁用交互行为。 (Enables --silent.) --bind-address=name IP address to bind to. (启用--silent。)--bind-address = name绑定到的IP地址。 --character-sets-dir=name Directory for character set files. --character-sets-dir = name字符集文件的目录。 --column-type-info Display column type information. --column-type-in​​fo显示列类型信息。 -c, --comments Preserve comments. -c,--comments保留注释。 Send comments to the server. 将评论发送到服务器。 The default is --skip-comments (discard comments), enable with --comments. 默认值为--skip-comments(丢弃注释),使用--comments启用。

How can I run the mysql commands to return the result to my eclipse terminal? 如何运行mysql命令将结果返回到eclipse终端?

You can pass the query directly from command line using the -e argument. 您可以使用-e参数直接从命令行传递查询。 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. 我认为您的问题是-p和密码之间没有空格。 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 有关更多信息,请参见文档: http : //dev.mysql.com/doc/refman/5.6/en/mysql.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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