views:

126

answers:

3

I am developing a Java application with MySql as the database. I have to dump the MySql database from my application periodically(let say every day at 10 a.m.) and I have written a batch (.bat) file for dumping the database. The batch file is working fine, but the problem is that it is asking for password each time during its execution.

Is there any way to dump MySql database without prompting for password and achieve it from Java application periodically?

A: 

You'll want the following option for mysqldump

--password[=password], -p[password]

mysqldump documentation

Fabian
A: 

You don't tell how you are actually dumping the database, but I'll assume that you are using mysqldump.

You can specify the database password on the command line using the switch --password=.... Or if you want to be more secure, use a password in an option file. See man mysqldump for more information.

Adam Batkin
yes i am using mysqldump only.
dhiraj
+1  A: 

You can definitely put the password on the command line, as others have pointed out. As far as getting it to run from Java (though the purists may object), you can use Runtime.exec() to just call your mysql command: http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html.

If you're doing it from within your java app, you might also want to consider not just doing it periodically, though. Depending on your database usage (for instance frequent reads, but infrequent writes), you may find that you can dump only when you know that the database has been changed, or something along those lines.

If you really want to just dump the database periodically though, you might be better off just setting up a cron-job. Here's a previous post on SO regarding the Windows approach to cron: http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron.

Curtis
Thanx a lot for your reply. The mysqldump part is working fine. I have to look for the periodic dump part now. Yes I have used the Runtime.exec() only for that purpose.
dhiraj