user2734304
user2734304

Reputation: 21

How do I dump a remote MySQL database using Java?

I've seen this work with local MySQL databases:

Runtime.getRuntime().exec("mysqldump -u USERNAME -p PASSWORD DBNAME > /path/to/location/backup.sql");

But is it possible to dump a remote MySQL database from a web server?

I'm making a program that backs up both my website files via FTP (already done with this part), and backs up my MySQL databases on the server as well. Therefore I need to know how to do this.

Upvotes: 1

Views: 3056

Answers (2)

Barranka
Barranka

Reputation: 21047

It can be done if you have access to the server. You can tell mysqldump to connect to a remote server:

mysqldump -h yourServerAddress -u yourUser -pYourPassword dbName > output_file.sql

yourServerAddress is the IP address where the MySQL server is.

Upvotes: 0

villanueva.ricardo
villanueva.ricardo

Reputation: 195

if you are trying to dump your database data from other computer you'll need to grand privileges to an specific user for this task.

for example, the user is: 'remoteAdminUser'

GRANT SELECT, LOCK TABLES 
ON *.* 
TO 'remoteAdminUser'@'localhost' IDENTIFIED BY 'dbPass';

and then you can use this java code

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “remoteAdminUser”;
String dbPass = “dbPass”;
String filePath = “c:\\sqlDump\myFirstDump.sql”;
/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = "";
executeCmd = "mysqldump -u "+dbUser + " -p"+dbPass + " "+dbName + " -r "+filePath;
}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){
out.println("Backup taken successfully");
} else {
out.println("Could not take mysql backup");
}

Upvotes: 1

Related Questions