1

I want to backup database in mysql 5.6.
For this I use this method:

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

        String executeCmd = "D://mysql-5.6.11-win32/bin/mysqldump -u " + dbUserName + " -p" + dbPassword
                + " --add-drop-database -B " + dbName + " -r " + path;
        Process runtimeProcess;
        try {

            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            // any error message?
            StreamGobbler errorGobbler = new StreamGobbler(runtimeProcess.getErrorStream(), "ERR");
            // any output?
            StreamGobbler outputGobbler = new StreamGobbler(runtimeProcess.getInputStream(), "OUT");
            // kick them off
            errorGobbler.start();
            outputGobbler.start();

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

StreamGobbler is Thread that wraps runtimeProcess.getErrorStream() and runtimeProcess.getInputStream() into BufferedReader. BufferedReader merely reads line by line using readLine() method. This is a hint I got on this article.

My problem is that method backupDB hangs on this line :

int processComplete = runtimeProcess.waitFor();

If I substitute waitFor method with exitValue I receive error

java.lang.IllegalThreadStateException: process has not exited

Therefore I must use waitFor method. And unfortunately it runs forever. I must press red icon on eclipse console to stop the JVM.

How to make process complete and backup mysql database? This is solved thanks!

EDIT To launch mysqldump.exe I need to write full path to it :

String executeCmd = "D://mysql-5.6.11-win32/bin/mysqldump -u "

Don't you know how to get rid of full path. I want to launch mysqldump independently on mysqldump.exe location on computer.

Maybe there is a way to get the path to mysqldump.exe having all connection details.

EDIT If I try to use mysqldump without full path to mysqldump.exe I receive this exception :

java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, Íå óäàåòñÿ íàéòè óêàçàííûé ôàéë
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at java.lang.Runtime.exec(Runtime.java:345)
    at dbViewer.model.ConnectionManager.backupDB(ConnectionManager.java:273)
    at dbViewer.model.ConnectionManager.main(ConnectionManager.java:337)
Caused by: java.io.IOException: CreateProcess error=2, Íå óäàåòñÿ íàéòè óêàçàííûé ôàéë
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
    at java.lang.ProcessImpl.start(ProcessImpl.java:133)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
    ... 5 more

Thanks!

Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83
  • 3
    What happens if you run the command manually from the commandline? Does it ask for user input? – dkatzel Aug 14 '13 at 17:43
  • http://stackoverflow.com/questions/9567699/how-to-mysqldump-on-java – Engineer2021 Aug 14 '13 at 17:47
  • Ah. You are right it asks me to enter password and after I press Enter indicating no password it successfully creates backup. But I use empty password. How to use empty password to backup database? – Volodymyr Levytskyi Aug 14 '13 at 17:48
  • 1
    you might be able to pass in a carriage return by writting one to the process's OutputStream – dkatzel Aug 14 '13 at 17:50
  • @dkatzel please tell me how to write carriage return(\n) to OutputStream. I used write('\n') and it does not help. Anyway now I can check if password is empty and if so execute mysqldump without -p option. Thanks, big help! – Volodymyr Levytskyi Aug 14 '13 at 18:06
  • It looks like you are using Windows so you may also need a '\r' in there too. It might be best to write the String returned by String.format("%n"). Depending on the OutputStream you may also need to flush the stream so the Process sees it. – dkatzel Aug 14 '13 at 18:14
  • You can try adding username and password in my.cnf file, that ways it should not prompt you for password. – Sachin Thapa Aug 14 '13 at 18:43
  • @dkatzel,brian and sachin please read my EDIT – Volodymyr Levytskyi Aug 15 '13 at 06:20
  • By the way: Exception text means "Не удается найти указанный файл". "File not found" :) Check the path. – DRCB Aug 15 '13 at 08:33
  • @DRCB Thanks! I want to get rid of path altogether. I want to launch mysqldump.exe without specifying its location on computer. – Volodymyr Levytskyi Aug 15 '13 at 08:40

1 Answers1

2

You can simply use mysqldump -p option:

mysqldump –u[user name] –p[password] [database name] > [dump file]
Frankie
  • 24,627
  • 10
  • 79
  • 121
  • PLease look at my first and second EDIT – Volodymyr Levytskyi Aug 15 '13 at 08:28
  • 1
    @VolodymyrLevytskyi Make a new question, you're not supposed to ask too many questions in one question, or edit your question once one question is resolved. – nos Aug 15 '13 at 08:33
  • @VolodymyrLevytskyi user "nos" is correct. If my answer was correct and solved your problem you should accept/up-vote it; and then ask a second question. On the particular case you mention, tough, you can't type mysqldump without the full location because the command `mysqldump` is not in your `path`. Google for "set windows path". – Frankie Aug 15 '13 at 11:01