所以我有一个基本的运行Windows服务,需要将所有数据从一个MySQL连接复制到另一个,分解为:
MySQL连接A有5个模式,每个模式都有几个表。
我需要将所有5个Schema以及这些模式中的所有表从MySQL Connection A复制到MySQL Connection B.
第一次运行时需要自动重新创建模式和布局以将数据放入MYSQL Connection B.一旦完成,它必须每天凌晨3点运行到凌晨5点不断覆盖连接B哪里有变化在连接A中。
我已经掌握了服务运行的基础知识,但它实际上根本没有访问数据库!
代码如下所示:
public partial class BackupService : ServiceBase
{
private Timer timer1 = null;
bool backUpRunning = false;
public BackupService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 3600000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
timer1.Start();
LogLibrary.WriteErrorLog("Backup window service started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
System.Diagnostics.Debugger.Launch();
TimeSpan now = DateTime.Now.TimeOfDay;
TimeSpan start = new TimeSpan(12, 0, 0);
TimeSpan end = new TimeSpan(14, 0, 0);
LogLibrary.WriteErrorLog("Checking if it's time to run the backup service and checking if the service is already running");
if (backUpRunning)
LogLibrary.WriteErrorLog("Service is already running");
if ((now > start) && (now < end) && !backUpRunning)
{
LogLibrary.WriteErrorLog("Service is not already running and the time of day is valid to run the service.");
try
{
backUpRunning = true;
LogLibrary.WriteErrorLog("Initialising backup service");
}
catch (Exception ex)
{
LogLibrary.WriteErrorLog("The backup service failed. It will return to step 1 and try again: " + ex.Message);
}
finally
{
backUpRunning = false;
}
}
LogLibrary.WriteErrorLog("Timer ticked and job has been done successfully");
}
protected override void OnStop()
{
timer1.Enabled = false;
timer1.Stop();
LogLibrary.WriteErrorLog("Service stopped");
}
private void Backup()
{
System.Diagnostics.Debugger.Launch();
string constring = "server=; user=root; pwd=; database=testing;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM testing"))
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
//mb.ExportInfo.AddCreateDatabase = true;
//mb.ExportInfo.ExportTableStructure = true;
//mb.ExportInfo.ExportRows = true;
mb.ExportToFile(file);
conn.Close();
}
}
}
}
//IMPORT DATABASES to Gunther
private void Restore()
{
string constring = "server=; user=root; pwd=; database=testingbackup;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand("INSERT ALL INTO testingbackup"))
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
//mb.ImportInfo.TargetDatabase = "testingbackup";
//mb.ImportInfo.DatabaseDefaultCharSet = "utf8";
mb.ImportFromFile(file);
conn.Close();
}
}
}
}
服务器详细信息和密码已因商业原因而被删除,但服务器通常会输入连接地址,例如。 00.250.250.250:3306和11.250.250.250:3306