我试图创建一个简单的函数,我可以调用它来通过mysqldump.exe转储MySql数据库。这些函数有效,它会转储数据库,但速度非常慢。当我通过MySQL Workbench运行数据导出时,它将在1秒内导出数据库(最长为4),文件小于14MB。但是,通过我的功能,只需不到14分钟即可完成相同的转储。根据Workbench,数据库在我的本地,只有31MiB(测试数据库)。
我通过创建新的mysqldump.exe
并将System.Diagnostics.Process
附加到System.Diagnostics.DataReceivedEventHandler
事件来致电OutputDataReceived
。我也重定向了任何错误,但没有任何错误。
以下是我传递给mysqldump.exe
的论据:
String.Format("-h {0} -u {1} -p{2} --port=3306 --max_allowed_packet=1G --no-create-info=TRUE --no-create-db --databases {3}", host, user, pswd, schemaName);
这是完整的功能:
StreamWriter _outputStream;
private void OnDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (e.Data != null) _outputStream.WriteLine(e.Data);
}
void OnErrorReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
private void CreateBackup(string schemaName)
{
string mysqldumpPath = @"C:\Program Files\MySQL\MySQL Workbench 6.2 CE\mysqldump.exe";
string host = "127.0.0.1";
string user = "username";
string pswd = "password";
string cmd = String.Format("-h {0} -u {1} -p{2} --port=3306 --max_allowed_packet=1G --no-create-info=TRUE --no-create-db --databases {3}", host, user, pswd, schemaName);
string dumpPath = ConfigurationManager.AppSettings["BackupLocation"] + String.Format("\\{0}_{1}_{2}_{3}.sql",
schemaName, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
_outputStream = new StreamWriter(dumpPath);
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = mysqldumpPath;
startInfo.Arguments = cmd;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.ErrorDialog = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = startInfo;
proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(OnDataReceived);
proc.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(OnErrorReceived);
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
_outputStream.Flush();
_outputStream.Close();
proc.Close();
_outputStream = null;
}
我应该做些什么,或者可以做些什么,这会有助于它更快地运行?