如何使用java进行mysql数据库备份? [英] How to take mysql database backup by using java?
本文介绍了如何使用java进行mysql数据库备份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码备份 mysql 数据库,但在运行程序时出现如下异常.请任何人请帮忙解决这个问题.
I am trying to take mysql database backup by using following code but I am getting exception like below when I run the program. Plz can anyone plz help out to solve this problem.
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();
}
推荐答案
至少有两个问题:
- 提供
mysqldump.exe
的完整路径(例如C:\Program Files\MySQL\bin\mysqldump.exe
) -p
没有密码值的参数会强制 mysqldump 提示输入.而且您不想要它,因为您以批处理模式运行它.因此,要么提供密码-p"your password"
要么更好(更安全)使用选项文件来避免在命令行上提供密码.阅读有关此最终用户密码安全指南一个>
- Provide a full path to
mysqldump.exe
(e.g.C:\Program Files\MySQL\bin\mysqldump.exe
) -p
parameter without a password value forces mysqldump to prompt for it. And you don't want it since you running it in a batch mode. Therefore either provide a password-p"your password"
or better (and more secure) use an option file to avoid giving the password on the command line. Read more on this End-User Guidelines for Password Security
这篇关于如何使用java进行mysql数据库备份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文