我有一個設置讓我在遠程服務器上自動進行日誌檢查。我創建了這樣的方法,它使用exec命令尾日誌這樣的..通過ssh運行mysql命令
Process p = Runtime.getRuntime().exec("ssh <user>@<domain> tail -f /location/logs
我打印日誌到我的終端在那裏我可以運行正則表達式模式,以確保它們是正確的。
現在我需要檢查一些在服務器上的mysql表中的日誌條目。我建立了一個類似的方法,使用List來執行一系列命令來返回mysql表中的條目:
List<String> cmd = Arrays.asList("ssh [email protected] 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", "[email protected]", "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命令返回結果我的eclipse終端?
感謝。要使用多個查詢,可以使用例如-e'use數據庫'-e'SELECT column1 FROM database'。我試過,但只能得到一個查詢執行 –
你的工作很好,我發現多個查詢可以運行在相同的-e命令之間用';'隔開。 ........「-e'use database; SELECT * FROM database'」 –
@DanielCohen請注意,我已經使用'-D'參數選擇了數據庫。您也可以在不顯式「使用」數據庫的情況下執行查詢:'從database.table'選擇*。 – steffen