我將通過使用java代碼進行數據庫備份。此代碼正在執行正常,但我得到了int processComplete = runtimeProcess.waitFor();這種方法調用是返回整數爲1.所以最後我得到的消息,因爲無法創建備份爲sop。使用java進行Mysql數據庫備份
public static void main(String[] args) {
String path = "D:/databasebackup/databasbac.sql";
String username = "root";
String password = "";
String dbname = "rac";
String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --add-drop-database -B " + dbname + " -r " + path;
Process runtimeProcess;
try {
// System.out.println(executeCmd);//this out put works in mysql shell
runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
System.out.println(executeCmd);
// runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete"+processComplete);
if (processComplete == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
嘗試讀取進程的錯誤流以查找可能的失敗 – Sanjeev
System.out.println(executeCmd);''的輸出是什麼?你是否嘗試在命令行上執行這個確切的命令? –
這是我得到的/bin/mysqldump -u root -p --add-drop-database -B rac -r D:/databasebackup/databasbac.sql –
user3214269