mysqldump not working when run from within an ASP .NET web app as a new process
Question
This, when run from the "run" window , works properly.
cmd.exe /K mysqldump --add-drop-database --add-drop-table --user=root --password=thepassword --databases theDatabase > C:\Backup\theBackup.sql
However, the same command, when I try to execute from my web application by calling an external process, fails.
Here's the code:
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", @"/K mysqldump --add-drop-database --add-drop-table --user=root --password=thepassword --databases theDatabase > C:\Backup\theBackup" + ".sql");
Process p = Process.Start(processInfo);
This is what I get...
alt text http://img40.imageshack.us/img40/8121/mysqldumperror.gif
Interestingly, the file - theBackup.sql - gets created, but is empty.
It isn't an environment PATH variable problem; the MySql bin directory path, which contains mysqldump is added into the environment PATH variable. To check this, if I open the command prompt, navigate to the path mentioned in the screenshot above and type mysqldump command manually, it recognizes the command...as shown below...
alt text http://img40.imageshack.us/img40/6879/mysqldumppathproper.gif
The problem is mysqldump specific, since the following piece of code works
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe", "/K ping stackoverflow.com");
What's wrong here?
Solution
my guess would be that the $PATH variable isn't defined to include the path to MySQL in the environment that ASP.NET is running in (either for the that user, or ASP.NET cleans out the environment).
Two things to try:
- (might work) Make sure the mysql directory is added to the global path -- not just the user-specific PATH variable. (note: this will require at least recycling IIS, maybe a reboot to take effect)
- Use the fully-qualified path to mysqldump (you will need to do something to quote or escape any spaces in the path).
OTHER TIPS
An exemple for mysqldump
Private sw As IO.StreamWriter
Public Sub ExecuteBackup()
Try
Dim mysqlDump As New Process
mysqlDump.StartInfo.FileName = "PahtToMysqlDump\mysqldump.exe"
mysqlDump.StartInfo.Arguments = "--user=myuser --password=mypassword --routines --all-databases"
mysqlDump.StartInfo.RedirectStandardOutput = True
mysqlDump.StartInfo.CreateNoWindow = True
mysqlDump.StartInfo.UseShellExecute = False
Console.WriteLine("Arguments:")
Console.WriteLine("mysqldump.exe " & mysqlDump.StartInfo.Arguments)
sw = New IO.StreamWriter("backup.sql")
AddHandler mysqlDump.OutputDataReceived, AddressOf mysqlDumpNewData
mysqlDump.Start()
mysqlDump.BeginOutputReadLine()
mysqlDump.WaitForExit()
sw.Close()
Console.WriteLine("Backup completed")
Catch ex As Exception
Console.WriteLine("BackupEngine.ExecuteBackup.", ex)
End Try
End Sub
Private Sub mysqlDumpNewData(sender As Object, e As DataReceivedEventArgs)
Dim line = e.Data
If line IsNot Nothing Then
sw.WriteLine(line)
If line.StartsWith("USE") Then
Dim ln = line.Split("`")(1)
Console.WriteLine("Database : " & ln.ToUpper)
End If
If line.StartsWith("CREATE TABLE") Then
Dim ln = line.Split("`")(1)
Console.WriteLine(" Table : " & ln.ToUpper)
End If
End If
End Sub