我试图通过使用以下代码来进行mysql数据库备份,但是当我运行程序时,我会得到如下的异常。 Plz任何人都可以帮助解决这个问题。
Java代码:
String path = "D:/databasbac.sql";
String username = "root";
String password = "";
String dbname = "ranjith";
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 });
// runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
答案 0 :(得分:0)
至少有两个问题:
mysqldump.exe
的完整路径(例如C:\Program Files\MySQL\bin\mysqldump.exe
)-p
参数强制mysqldump提示它。而且你不需要它,因为你以批处理模式运行它。因此,要么提供密码-p"your password"
或更好(并且更安全),请使用选项文件以避免在命令行上提供密码。详细了解End-User Guidelines for Password Security