i apply comment 1 and 2 alternatively.But i didn't get myqldump file using java.mysqldump working in cmd. i think in cmd it ask password after typing mysqldump comment. please help me fast.thanks in advance.
String dumpCommand = "C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump" + " -u " + user + " -p" + " " + database + " > " + path;
**//command 1**
Runtime rt = Runtime.getRuntime();
File test = new File(path);
PrintStream ps;
try {
// Process child = rt.exec("Cmd /c \"C:/Program Files/MySQL/MySQL Server 5.0/bin/mysqldump\" -u root -p saxco > "+path);//command 2
Process child = rt.exec(dumpCommand);
ps = new PrintStream(test);
InputStream in = child.getInputStream();
int ch;
while ((ch = in.read()) != -1) {
ps.write(ch);
System.out.write(ch);
}
As documented here you can specify the password in the command like this [...] -pPASSWORD [...]. Note that there must not be a space between the -p option flag and the actual password.
Keep in mind that specifying the password this way is a security risk!
If you don't use a password just don't use the -p option too.
String dumpCommand = "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump" + " -u " + user + database + " > " + path;
or this if you have one (without the gaps)
String dumpCommand = "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump" + " -u " + user + " -p" + password + " " + database + " > " + path;
try this-
String cmd="mysqldump -u "+USER+" -p"+PASSWORD+" "+database+" --routines -r"+PATH;
Runtime runtime = Runtime.getRuntime();
Process exec;
exec = runtime.exec(cmd);
int processComplete = exec.waitFor();
if(processComplete == 0){
System.out.println("Backup taken successfully in "+PATH+" on "+new Date());
} else {
System.out.println("Could not take mysql backup :ERROR:");
}
Related
I have a CSV file, and I'm trying to import the CSV file in the MongoDB database. Unfortunately, the experience is not working as I wish it would. The content of the CSV file is not relevant, since when I use MongoDB Compass to import it manually, there is no issue and it's appearing as I want. Thus, I'm trying to save it from local folder to the MongoDB using Java and Spring Boot if possible, but I can't find any good advice or explaination about how to do so.
I'm using Spring Boot 2.6.3 and Java11.
Thanks a lot.
public class MongoImportUtil {
public static void main(String[] args) {
String Host = "localhost";
String Port = "27017";
String DB = "userDemo";
String CollectionName = "users";
String FileName = "D:/Test.csv";
// Without Credential
String command = "C:\\Program Files\\MongoDB\\Server\\5.0\\bin\\mongoimport.exe --host " + Host + " --port " + Port + " --db " + DB + " --collection " + CollectionName + " --headerline --type=csv --file "+ FileName;
// With Credential
/* String command = "C:\\Program Files\\MongoDB\\Server\\5.0\\bin\\mongoimport.exe --host " + Host + " --port " + Port + " --db " + DB + " --collection " + CollectionName + "--authenticationDatabase admin --username " + user + " --password " + password + " --headerline --type=csv --file "+ FileName; */
try {
Process process = Runtime.getRuntime().exec(command);
System.out.println("Imported csv into Database");
} catch (Exception e) {
System.out.println("Error executing " + command + e.toString());
}
}
}
The following code is always returning 1. I do not understand why.
String executeCmd[] = {"mysql"," -u" + dbUser," -p" + dbPass," -A"," -D"+dbName ," < " + restorePath};
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Successfully restored from SQL : " + s);
} else {
System.out.println("Error at restoring");
}
I have grilled my head trying to solve this problem for a week and I have finally figured it out.
The problem is, the < in above code (stream redirection). You cannot use stream redirection when executing a process in Java (it simply does not work in this case).
So, you have to do it without using stream redirection. One way of doing it is:
mysql -u root -p**** --execute "SOURCE \\path\\to\\sql"
Try:
String executeCmd[] = {"mysql"," -u "+dbUser," -p"+dbPass," -A"," -D"+dbName," --execute"," \"SOURCE "+restorePath+"\""};
or simply:
String executeCmd = "mysql -u "+dbUser+" -p"+dbPass+" -A -D "+"dbName -e \"SOURCE "+restorePath+"\"";
Redirect < is interpreted by the command shell. Try
String executeCmd[] =
{"bash", "-c", "mysql", "-u", dbUser, "-p", dbPass, "-A","-D", dbName , "< ", restorePath};
I was trying to execute the mysql command using java but this always keeps giving error.
String dbName = "dth";
String dbUser = "root";
String dbPass = "root";
String executeCmd = "";
executeCmd = "mysqldump -u " + dbUser + " -p" + dbPass + " " + dbName + " -r C:\\backup.sql";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println("Backup taken successfully");
} else {
System.out.println("Could not take mysql backup");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
I tried the above code but I couldn't get it done, keeps giving me
CreateProcess error=2, The system cannot find the file specified
error.
I have tried it by creating a .sql file on the location and still I get the same error.
Add the directory in which mysqldump resides into your path environment variable or use the full pathname I.e c:\directory\mysqldump
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.
It shows io file problem. 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). Effectively you should be able to type your System.out.println() content in a Plain DOS prompt and should be able to run it.
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
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
You can get SO environment variables by
Map<String, String> env = System.getenv();
final String LOCATION = env.get("MYSQLDUMP");
I setup the system variable like this :
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 don't have configured password for mysql server just remove the -p PASSWORD attribute from the code. And don't forget to restart your computer after creating a new system variable.
I have a short question concerning the Runtime.getRuntime.exec("") command in java.
I am trying to make a tunnel to a gateway computer via SSH:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "#" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
Runtime.getRuntime().exec(tunnel);
This bit of code works properly except the annoying fact that a command prompt appears.
Now I tried to exit the prompt after executing the code:
String tunnel = "C:\\Program Files\\PuTTY\\putty.exe -ssh -X -P " + localPort + " " + tempUsername + "#" + localIP
+ " -pw " + tempPassword + " -L " + tunnelPort + ":" + gatewayName + ":"+gatewayPort;
String cmdCommands [] = {tunnel, "exit"};
Runtime.getRuntime().exec(cmdCommands);
Is it possible to close the command prompt in a similar way like I do or are there better ways? (This code doesnt work)
You'll need to either use an actual SSH library directly instead of putty, as in the comments, or capture the IO streams of the exec
Process p = Runtime.getRuntime().exec(cmdCommands);
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("exit\n");
It's generally a bad idea to hard code \n, for platform reasons, but you get the idea. Also, you will need to pick the right OutputStream. There are several subclasses(buffered etc.) which may be useful.