Mysqldump.exe没有对mysql数据库进行全量备份
本文关键字:备份 数据库 mysql exe Mysqldump | 更新日期: 2023-09-27 18:12:02
我有一个备份MySQL数据库的过程。我也有不同的MySQL服务器。这个过程适用于一些MySQL服务器。但在某些服务器上,它不能正常工作,并创建一个大小为1kb的备份文件。
public void DatabaseBackup(string ExeLocation, string DBName)
{
try
{
string tmestr = "";
tmestr = DBName + "-" + DateTime.Now.ToString("hh.mm.ss.ffffff") + ".sql";
tmestr = tmestr.Replace("/", "-");
tmestr = "c:/" + tmestr;
StreamWriter file = new StreamWriter(tmestr);
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", "uid", "pass", "host", DBName);
proc.FileName = ExeLocation;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.Arguments = cmd;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();
}
catch (IOException ex)
{
}
}
public void DatabaseBackup(string ExeLocation, string DBName)
{
try
{
string tmestr = "";
tmestr = DBName + "-" + DateTime.Now.ToString("hh.mm.ss.ffffff") + ".sql";
tmestr = tmestr.Replace("/", "-");
tmestr = "c:/" + tmestr;
StreamWriter file = new StreamWriter(tmestr);
ProcessStartInfo proc = new ProcessStartInfo();
string cmd = string.Format(@"-u{0} -p{1} -h{2} {3}", "uid", "pass", "host", DBName);
proc.FileName = ExeLocation;
proc.RedirectStandardInput = false;
proc.RedirectStandardOutput = true;
proc.Arguments = cmd;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process p = Process.Start(proc);
string res;
res = p.StandardOutput.ReadToEnd();
file.WriteLine(res);
p.WaitForExit();
file.Close();
}
catch (IOException ex)
{
}
}
谁能告诉我问题是什么,我怎样才能解决它?
我终于得到了答案。我们需要对MySQL用户或数据库进行SELECT和LOCK_TABLE权限的备份。在数据库上设置这些权限后,我可以对该数据库进行完全备份。
备份语句在哪里?
以下是备份数据库的最佳方法:private void BackupDatabase()
{
string time = DateTime.Now.ToString("dd-MM-yyyy");
string savePath = AppDomain.CurrentDomain.BaseDirectory + @"Backups'"+time+"_"+saveFileDialogBackUp.FileName;
if (saveFileDialogBackUp.ShowDialog() == DialogResult.OK)
{
try {
using (Process mySqlDump = new Process())
{
mySqlDump.StartInfo.FileName = @"mysqldump.exe";
mySqlDump.StartInfo.UseShellExecute = false;
mySqlDump.StartInfo.Arguments = @"-u" + user + " -p" + pwd + " -h" + server + " " + database + " -r '"" + savePath + "'"";
mySqlDump.StartInfo.RedirectStandardInput = false;
mySqlDump.StartInfo.RedirectStandardOutput = false;
mySqlDump.StartInfo.CreateNoWindow = true;
mySqlDump.Start();
mySqlDump.WaitForExit();
mySqlDump.Close();
}
}
catch (IOException ex)
{
MessageBox.Show("Connot backup database! 'n'n" + ex);
}
MessageBox.Show("Done! database backuped!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
祝你好运!