MySQL mysql database backup

I tried to run the following code, which should back up my database , but it shows some runtime errors.

But I tried to run the System.out.println () output part (which I commented on in this code) in the mysql shell, and it worked >.

It displays a problem with the io file. Plz someone helps 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 
+4
source share
3 answers

Please check if your global variable Global PATH <Path in MySQL> \ bin (Do a echo %PATH% and see). In fact, you will have to type your System.out.println () file at the Plain DOS prompt and be able to run it.

Even if this does not work, try changing the code to execute as below

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

This should ideally fix the problem.

UPDATE:

If you do not have a PATH environment variable, change the code to the following

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

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

I tried this one but it did not work, so I replaced it with

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

and he worked

0
source

I solved this problem by providing the full path to mysqldump.exe

You can get SO environment variables with

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

I set the system variable as follows:

  • Variable Name: MYSQLDUMP
  • Value: 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 do not have a password configured for the mysql server, simply remove the -p PASSWORD attribute from the code. And don't forget to restart your computer after creating a new system variable.

0
source

Source: https://habr.com/ru/post/1445964/


All Articles