问题描述
我有一个小应用程序,我将用它来备份和恢复一个mysql数据库,编码备份如下,它正常工作,
I have a small application which I am going to use to backup and restore a mysql database, coding for backup is as follows and it works properly,
....
String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
+ dataBase.getDatabaseName() + " -r " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";
System.out.println(command);
Process p = null;
try {
Runtime runtime = Runtime.getRuntime();
p = runtime.exec(command);
int processComplete = p.waitFor();
if (processComplete == 0) {
System.out.println("Backup created successfully");
} else {
System.out.println("Could not create the backup");
}
....
但是当我尝试恢复数据库时使用以下代码不起作用,请帮助
But when I try to restore the database using following codes it is not working, please help
........
String command = "mysqldump --host=" + dataBase.getHost() + " --user="+dataBase.getUserName() + " --password= " + dataBase.getPassword()+""+dataBase.getDatabaseName() + " " + dataBase.getBackupPath();
Process p = null;
try {
System.out.println(command);
Runtime runtime = Runtime.getRuntime();
p = runtime.exec(command);
int processComplete = p.waitFor();
if (processComplete == 0) {
System.out.println("Backup restored successfully");
} else {
System.out.println("Could not restore the backup");
}
......
经过如此多的谷歌搜索,我找到了答案,
After so many google searches I found the answer for my matter,
......
String[] executeCmd = new String[]{"mysql", [database], "--user=" + [username],"--password=" + [password], "-e", " source " + [absolute path to the sql file]};
p = Runtime.getRuntime().exec(executeCmd);
int processComplete = p.waitFor();
......
在上面的代码中最重要的是**源+ [sql文件的绝对路径] **'源'字和文件路径之间不应该有逗号。
In the above code most important thing is ** " source " + [absolute path to the sql file] ** there shouldn't be a comma between 'source' word and the file path.
这对我有用我希望它对你们也有用。
this worked for me I hope it'll work for you guys too.
推荐答案
我认为你没有使用 mysqldump
命令非常正确。您是否应该使用<
或>
字符来表示备份/恢复?详细讨论 。这样的东西要备份:
I don't think you are using the mysqldump
command quite right. Shouldnt you use <
or >
characters to denote the backup/restore? This is discussed at length here. Something like this to backup:
String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
+ dataBase.getDatabaseName() + " > " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";
并恢复:
String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
+ dataBase.getDatabaseName() + " < " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";
这篇关于如何使用java备份和恢复mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!