简体   繁体   English

备份mysql数据库的java代码

[英]backup mysql database java code

I tried to run the following code which is to create a backup of my database but it shows some run time errors. 我尝试运行以下代码来创建数据库备份,但它显示了一些运行时错误。

But, I tried to run the System.out.println() output part, (which I've commented in the given code) in mysql shell and it worked . 但是,我尝试在mysql shell中运行System.out.println()输出部分(在给定的代码中已对此进行了注释) ,并且它可以正常工作

It shows io file problem. 它显示io文件问题。 Plz somebody help me. 请有人帮助我。

package files;

 public class tableBackup_1 {

public boolean tbBackup(String dbName,String dbUserName, String dbPassword, String path) {

    String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --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(executeCmd);
            int processComplete = runtimeProcess.waitFor();

                if (processComplete == 0)
                {
                    System.out.println("Backup created successfully");
                    return true;
                }
                else
                {
                    System.out.println("Could not create the backup");
                }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
return false;
}

public static void main(String[] args){

        tableBackup_1 bb = new tableBackup_1();
        bb.tbBackup("test","harin","1234","C:/Users/Master/Downloads/123.sql");

}
}
    mysqldump -u harin -p1234 --add-drop-database -B test -r C:/Users/Master/Downloads/123.sql
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
        at java.lang.Runtime.exec(Runtime.java:593)
        at java.lang.Runtime.exec(Runtime.java:431)
        at java.lang.Runtime.exec(Runtime.java:328)
        at files.tableBackup_1.tbBackup(tableBackup_1.java:12)
        at files.tableBackup_1.main(tableBackup_1.java:34)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
        at java.lang.ProcessImpl.start(ProcessImpl.java:30)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
        ... 5 more

Please check whether your Global PATH environment variable has <Path to MySQL>\\bin in it (Do a echo %PATH% and see). 请检查您的Global PATH环境变量是否其中包含<MySQL的路径> \\ bin(执行echo %PATH%并查看)。 Effectively you should be able to type your System.out.println() content in a Plain DOS prompt and should be able to run it. 有效地,您应该能够在Plain DOS提示符下键入System.out.println()内容,并且应该能够运行它。

Even with that if it doesn't work try changing the code to execute like below 即使无法解决问题,也请尝试将代码更改为如下所示执行

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

This should ideally fix the issue. 理想情况下,这应该可以解决该问题。

UPDATE: 更新:

If you don't have it in the PATH environment variable change the code to following 如果您不在PATH环境变量中,请将代码更改为以下内容

String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path;

runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

i tried this one but it didn't work so i replaced it with 我尝试了这个,但是没有用,所以我换成了

this runtimeProcess = Runtime.getRuntime().exec(executeCmd);

and it worked 而且有效

I solved this problem by giving full path to mysqldump.exe 我通过给出mysqldump.exe的完整路径解决了这个问题

You can get SO environment variables by 您可以通过以下方式获取SO环境变量

Map<String, String> env = System.getenv();
final String LOCATION = env.get("MYSQLDUMP");

I setup the system variable like this : 我像这样设置系统变量:

  • Variable Name : MYSQLDUMP 变量名: MYSQLDUMP
  • Value : D:\\xampp\\mysql\\bin\\mysqldump.exe 值: D:\\xampp\\mysql\\bin\\mysqldump.exe

Now just execute this code below, 现在只需执行以下代码,

String executeCmd = LOCATION+" -u " + DBUSER + " --add-drop-database -B " + DBNAME + " -r " + PATH + ""+FILENAME
Process runtimeProcess = = Runtime.getRuntime().exec(executeCmd);

Note : If you don't have configured password for mysql server just remove the -p PASSWORD attribute from the code. 注意:如果您尚未为mysql服务器配置密码,只需从代码中删除-p PASSWORD属性即可。 And don't forget to restart your computer after creating a new system variable. 并且不要忘记在创建新的系统变量后重新启动计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM