我需要使用mysqldump
来避免错误并将数据库备份存储在单独的文件路径中public class NewClass {
public static void main(String args[]) throws IOException, SQLException {
String dbName = "test";
String dbUser = "root";
String dbPass = "root";
try {
String executeCmd = "";
executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup taken successfully");
} else {
System.out.println("Could not take mysql backup");
}
} catch (InterruptedException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
错误:运行: 线程“main”中的异常java.io.IOException:无法运行程序“mysqldump”:CreateProcess error = 2,系统找不到文件 规定 在java.lang.ProcessBuilder.start(ProcessBuilder.java:1042) 在java.lang.Runtime.exec(Runtime.java:615) 在java.lang.Runtime.exec(Runtime.java:448) 在java.lang.Runtime.exec(Runtime.java:345) at mypkg.NewClass.main(NewClass.java:27) 引起:java.io.IOException:CreateProcess error = 2,系统找不到指定的文件 at java.lang.ProcessImpl.create(Native Method) 在java.lang.ProcessImpl。(ProcessImpl.java:288) 在java.lang.ProcessImpl.start(ProcessImpl.java:133) 在java.lang.ProcessBuilder.start(ProcessBuilder.java:1023) ......还有4个 Java结果:1 建立成功(总时间:0秒)
答案 0 :(得分:0)
尝试将完整路径放入mysqldump程序
executeCmd = "/full/path/to/mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";
我也很想将完整路径放在要写入转储的文件中。
答案 1 :(得分:0)
你试过给exec函数一个字符串数组吗?这对我来说很有帮助。
String[] comm = new String[4];
comm[0] = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump"
comm[1] = "-u " + dbUser;
comm[2] = "-p " + dbPass + " " + dbName;
comm[3] = "-r backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(comm)
因为当使用单个String作为参数时我有一些奇怪的行为......
答案 2 :(得分:0)
只需修改
即可executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r backup.sql";
到
executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName;
并在if-else语句后添加
String str="";
PrintWriter pw = new PrintWriter("path for this file/backup.sql");
Scanner outScanner = new Scanner(runtimeProcess.getInputStream());
while(outScanner.hasNextLine()){
pw.println(outScanner.nextLine());
}
outScanner.close();
pw.close();
答案 3 :(得分:0)
我有同样的问题。对我来说,我只是在路径变量中添加了mysql。在路径变量中设置了旧安装。删除它并添加新路径。