如何使用java备份和恢复mysql数据库

时间:2011-12-12 06:40:39

标签: java mysqldump

我有一个小应用程序,我将用它来备份和恢复一个mysql数据库,编译备份如下,它可以正常工作,

....
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");
            }
....

但是当我尝试使用以下代码恢复数据库时,它无效,请帮助

  ........
    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");
            }
     ......

经过如此多的谷歌搜索,我找到了答案,

  ......
  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();
  ......

在上面的代码中,最重要的是**“source”+ [sql文件的绝对路径] **'source'字和文件路径之间不应该有逗号。

这对我有用,我希望它对你们也有用。

4 个答案:

答案 0 :(得分:9)

我认为您没有正确使用mysqldump命令。您是否应该使用<>个字符来表示备份/恢复?这将详细讨论here。像这样备份:

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";

答案 1 :(得分:1)

我想感谢各位关于如何使用java代码恢复mysql数据库的支持。由于这些代码都不适用于我,但它给了我一个线索,让它起作用。

以下是我的最终工作代码。

String[] executeCmd = new String[]{"C:\\xampp\\mysql\\bin\\mysql.exe",mydb, "--user=" + dbUserName, "--password=" + dbPassword, "-e", " source " + source};

答案 2 :(得分:0)

也许是因为分配给终端的输出流...... 是否可以在cmd中运行mysqldump命令? 例如

String command = "cmd /K mysqldump..."

您可能也有兴趣阅读this文章:

希望,这有帮助

答案 3 :(得分:0)

这确实适用于恢复

 String comando = "C:\\MySQL\\bin\\mysql.exe  --host=localhost --port=3306 --user=root --password=123 < D:\\back.sql";
File f = new File("restore.bat");
FileOutputStream fos = new FileOutputStream(f);
fos.write(comando.getBytes());
fos.close();
Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");  

和备份

  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.forLanguageTag("ru"));
    java.util.Date currentDate = new java.util.Date();
    Process p = null;
    try {
        Runtime runtime = Runtime.getRuntime();
        p = runtime.exec("C:\\MySQL\\bin\\mysqldump.exe  --default-character-set=utf8 -uroot -p123 -c  -B shch2 -r " + "D:/" + dateFormat.format(currentDate) + "_backup" + ".sql");

//使用dbpass和dbname更改dbpass和dbname             int processComplete = p.waitFor();

        if (processComplete == 0) {

            System.out.println("Backup created successfully!");

        } else {
            System.out.println("Could not create the backup");
        }


    } catch (Exception e) {
        e.printStackTrace();
    }

但是你需要传达mysql.exe和mysqldump.exe的确切路径