How to take mysql database backup by using java? [duplicate]

天大地大妈咪最大 提交于 2019-12-13 08:03:39

问题


I am trying to take mysql database backup by using following code but I am getting exception like below when I run the program. Plz can anyone plz help out to solve this problem.

Java Code:

        String path = "D:/databasbac.sql";
        String username = "root";
        String password = "";
        String dbname = "ranjith";
        String executeCmd = "<Path to MySQL>/bin/mysqldump -u " + username + " -p" + password + " --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(new String[] { "cmd.exe", "/c", executeCmd });
//            runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup created successfully");

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

回答1:


There are at least two issues:

  1. Provide a full path to mysqldump.exe (e.g. C:\Program Files\MySQL\bin\mysqldump.exe)
  2. -p parameter without a password value forces mysqldump to prompt for it. And you don't want it since you running it in a batch mode. Therefore either provide a password -p"your password" or better (and more secure) use an option file to avoid giving the password on the command line. Read more on this End-User Guidelines for Password Security


来源:https://stackoverflow.com/questions/21772852/how-to-take-mysql-database-backup-by-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!