来自 java 应用程序的 mysqldump

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20820213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:35:37  来源:igfitidea点击:

mysqldump from java application

javamysqldump

提问by bluesony

Try to backup mysql DB from Java application (IDE Netbeans) using the following command but can't seem to find the file even though I specified a path:

尝试使用以下命令从 Java 应用程序 (IDE Netbeans) 备份 mysql DB,但即使我指定了路径,似乎也找不到该文件:

Runtime.getRuntime().exec("C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "+fisier.getName()+" > C:\"+fisier.getName()+".sql;");

Also, I don't receive any errors, so I assumed that the backup has been done. How can I find the file? Regarding fisier.getName(): File fisier = jFileChooserSave.getSelectedFile();

另外,我没有收到任何错误,所以我认为备份已经完成。我怎样才能找到文件?关于 fisier.getName(): File fisier = jFileChooserSave.getSelectedFile();

采纳答案by Sanket Parikh

You can test the below mentioned code for testing your mysqldump command output. There can be two main reasons to why the file is not creating, per my assumptions:-

您可以测试下面提到的代码来测试您的 mysqldump 命令输出。根据我的假设,文件未创建的主要原因可能有两个:-

  1. If using windows then the UAC permissions for the destined location can be the issue.
  2. You might be facing a syntax issue in the final mysqldump command generated to be executed by the java runtime.

     //Process exec = Runtime.getRuntime().exec("C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "+fisier.getName()+" > C:\"+fisier.getName()+".sql;");
    
    Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "+fisier.getName()+" > C:\"+fisier.getName()+".sql;"});
    
    //Wait for the command to complete, and check if the exit value was 0 (success)
    if(exec.waitFor()==0)
    {
        //normally terminated, a way to read the output
        InputStream inputStream = exec.getInputStream();
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    }
    else
    {
        // abnormally terminated, there was some problem
                    //a way to read the error during the execution of the command
        InputStream errorStream = exec.getErrorStream();
        byte[] buffer = new byte[errorStream.available()];
        errorStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    
    }
    
  1. 如果使用 Windows,那么目标位置的 UAC 权限可能是问题所在。
  2. 在生成由 java 运行时执行的最终 mysqldump 命令中,您可能会遇到语法问题。

     //Process exec = Runtime.getRuntime().exec("C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "+fisier.getName()+" > C:\"+fisier.getName()+".sql;");
    
    Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump "+fisier.getName()+" > C:\"+fisier.getName()+".sql;"});
    
    //Wait for the command to complete, and check if the exit value was 0 (success)
    if(exec.waitFor()==0)
    {
        //normally terminated, a way to read the output
        InputStream inputStream = exec.getInputStream();
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    }
    else
    {
        // abnormally terminated, there was some problem
                    //a way to read the error during the execution of the command
        InputStream errorStream = exec.getErrorStream();
        byte[] buffer = new byte[errorStream.available()];
        errorStream.read(buffer);
    
        String str = new String(buffer);
        System.out.println(str);
    
    }
    

The redirection operator doesn't works when using Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

重定向运算符在使用时不起作用 Process exec = Runtime.getRuntime().exec("C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;");

It is becasue it does not invokes the command shell, so we cannot get the functionality of the redirection operator, in order to fix this we can execute a command prompt (cmd) followed by the mysqldump command, and it will work.

这是因为它不调用命令外壳,所以我们无法获得重定向操作符的功能,为了解决这个问题,我们可以执行一个命令提示符(cmd),然后是 mysqldump 命令,它会起作用。

Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});

Process exec = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","C:\\Program Files\\MySQL\\MySQL Server 5.6\\bin\\mysqldump "+fisier.getName()+" > C:\\"+fisier.getName()+".sql;"});

Here /c in the cmd.exe specify that execute the passed command and terminate the process. The actual command when executed by java runtime will become

这里 cmd.exe 中的 /c 指定执行传递的命令并终止进程。java运行时执行的实际命令将变为

cmd.exe /c yourmysqldumpCommand

cmd.exe /c yourmysqldump 命令

回答by StationaryTraveller

For non-windows users:

对于非 Windows 用户:

String dump = "mysqldump -usome_user -psome_pass database_name   > path/to/file.sql";
String[] cmdarray = {"/bin/sh","-c", dump};
Process p = Runtime.getRuntime().exec(cmdarray);
if (p.waitFor() == 0) {
    // Everything went fine
} else {
   // Something went wrong
}

Using the cmd array is important. Otherwise execcannot parse '>' and file name and you will get an error.

使用 cmd 数组很重要。否则exec无法解析 '>' 和文件名,您将收到错误消息。