运行 mysql 通过 ssh 命令
run mysql commands through ssh
我有一个设置,可以在远程服务器上自动检查日志。我已经创建了这样的方法,它使用 exec 命令来跟踪这样的日志..
Process p = Runtime.getRuntime().exec("ssh <user>@<domain> tail -f /location/logs
我将日志打印到我的终端,在那里我可以 运行 正则表达式模式以确保它们是正确的。
现在我需要根据服务器上的一些 mysql table 检查日志中的一些条目。我已经设置了一个类似的方法,使用 List 对 return 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);
但是没用。我玩弄了 exec 字符串的语法,看起来像这样:
List<String> cmd = Arrays.asList("ssh", "user@domain", "mysql", "-u", "user", "-ppassword", "-h", "ipAddress", "use databases", "SELECT columns FROM database");
现在我收到这个错误:
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.
如何 运行 mysql 命令将 return 结果发送到我的 eclipse 终端?
您可以使用 -e
参数直接从命令行传递查询。示例:
mysql -uuser -ppass -hhost -Ddatabase -e'show tables'
这是未经测试的,但它应该是这样的:
List<String> cmd = Arrays.asList(
"ssh",
"user@domain",
"mysql",
"-uuser",
"-ppassword",
"-h10.0.0.1",
"-Ddb",
"-e'SELECT columns FROM table'"
);
我认为你的问题是 -p 和密码之间没有 space。你应该尝试像这样构建你的命令:
mysql --user=user_name --password=your_password db_name
有关更多信息,请参阅文档:
http://dev.mysql.com/doc/refman/5.6/en/mysql.html
我有一个设置,可以在远程服务器上自动检查日志。我已经创建了这样的方法,它使用 exec 命令来跟踪这样的日志..
Process p = Runtime.getRuntime().exec("ssh <user>@<domain> tail -f /location/logs
我将日志打印到我的终端,在那里我可以 运行 正则表达式模式以确保它们是正确的。
现在我需要根据服务器上的一些 mysql table 检查日志中的一些条目。我已经设置了一个类似的方法,使用 List 对 return 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);
但是没用。我玩弄了 exec 字符串的语法,看起来像这样:
List<String> cmd = Arrays.asList("ssh", "user@domain", "mysql", "-u", "user", "-ppassword", "-h", "ipAddress", "use databases", "SELECT columns FROM database");
现在我收到这个错误:
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.
如何 运行 mysql 命令将 return 结果发送到我的 eclipse 终端?
您可以使用 -e
参数直接从命令行传递查询。示例:
mysql -uuser -ppass -hhost -Ddatabase -e'show tables'
这是未经测试的,但它应该是这样的:
List<String> cmd = Arrays.asList(
"ssh",
"user@domain",
"mysql",
"-uuser",
"-ppassword",
"-h10.0.0.1",
"-Ddb",
"-e'SELECT columns FROM table'"
);
我认为你的问题是 -p 和密码之间没有 space。你应该尝试像这样构建你的命令:
mysql --user=user_name --password=your_password db_name
有关更多信息,请参阅文档: http://dev.mysql.com/doc/refman/5.6/en/mysql.html