我会另外应用注释1和2.但是我没有使用在cmd中工作的java.mysqldump来获取myqldump文件。我认为在cmd中输入mysqldump注释后会询问密码。请快速帮助我。谢谢。
String dumpCommand = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump" + " -u " + user + " -p" + " " + database + " > " + path;
**//command 1**
Runtime rt = Runtime.getRuntime();
File test = new File(path);
PrintStream ps;
try {
// Process child = rt.exec("Cmd /c \"C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump\" -u root -p saxco > "+path);//command 2
Process child = rt.exec(dumpCommand);
ps = new PrintStream(test);
InputStream in = child.getInputStream();
int ch;
while ((ch = in.read()) != -1) {
ps.write(ch);
System.out.write(ch);
}
答案 0 :(得分:0)
如文档here所示,您可以在命令中指定密码,例如[...] -pPASSWORD [...]
。请注意,{strong>不必须是-p
选项标记与实际密码之间的空格。
请注意,以这种方式指定密码存在安全风险!
答案 1 :(得分:0)
如果您不使用密码,请不要使用-p选项。
String dumpCommand =“C:\ Program Files \ MySQL \ MySQL Server 5.0 \ bin \ mysqldump”+“ - u”+ user + database +“>”+ path;
或者如果你有一个(没有间隙)
String dumpCommand =“C:\ Program Files \ MySQL \ MySQL Server 5.0 \ bin \ mysqldump”+“ - u”+ user +“ - p”+ password +“”+ database +“>”+ path;
答案 2 :(得分:0)
试试这个 -
String cmd="mysqldump -u "+USER+" -p"+PASSWORD+" "+database+" --routines -r"+PATH;
Runtime runtime = Runtime.getRuntime();
Process exec;
exec = runtime.exec(cmd);
int processComplete = exec.waitFor();
if(processComplete == 0){
System.out.println("Backup taken successfully in "+PATH+" on "+new Date());
} else {
System.out.println("Could not take mysql backup :ERROR:");
}