Mysql备份中超时-C#

本文关键字:-C# 超时 中超 备份 Mysql | 更新日期: 2023-09-27 18:00:02

我构建了一个应用程序来从服务器备份我的MYSQL数据库,一天比一天大,这在某些时候会触发错误(从我的角度来看):

消息:超时已过期。在操作完成或服务器没有响应之前经过的超时时间。完整:MySql.Data.MySqlClient.MySqlException(0x80004005):超时已过期。操作完成前经过的超时时间或服务器没有响应。--->System.TimeoutException:IO操作超时位于MySql.Data.MySqlClient.TimedStream.StopTimer()在MySql.Data.MySqlClient.TimedStream.Read(Byte[]缓冲区,Int32偏移量,Int32计数)在System.IO.BufferedStream.Read(Byte[]数组,Int32偏移量,Int32计数)位于MySql.Data.MySqlClient.MySqlStream.ReadFully(流流,Byte[]缓冲区,Int32偏移量,Int32计数)位于MySql.Data.MySqlClient.MySqlStream.LoadPacket()位于MySql.Data.MySqlClient.MySqlStream.ReadPacket()位于MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32语句Id,Int32列)位于MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId,Int32列)位于MySql.Data.MySqlClient.ResultSet.GetNextRow()位于MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior行为)位于MySql.Data.MySqlClient.MySqlDataReader.Read()位于MySql.Data.MySqlClient.ExceptionInterceptor.Sthrow(异常异常)位于MySql.Data.MySqlClient.MySqlConnection.Sthrow(异常示例)位于MySql.Data.MySqlClient.MySqlConnection.HHandleTimeoutOrThreadAbort(异常示例)位于MySql.Data.MySqlClient.MySqlDataReader.Read()位于MySql.Data.MySqlClient.MySqlBackup.Export_RowsData(字符串表名,字符串选择SQL)位于MySql.Data.MySqlClient.MySqlBackup.Export_Rows(字符串tableName,字符串selectSQL)位于MySql.Data.MySqlClient.MySqlBackup.Export_TableRows()位于MySql.Data.MySqlClient.MySqlBackup.ExportStart()位于MySql.Data.MySqlClient.MySqlBackup.ExportToFile(字符串文件路径)位于c:''Users''Belal''Documents''Visual Studio 2012''Projects''MYSQL自动备份''MYSQL自动备份''Form1.cs:line 132 中的MYSQL_Auto_Backup.Form1.Backup()

代码:

// Backup...
            DateTime Time = DateTime.Now;
            year = Time.Year;
            month = Time.Month;
            day = Time.Day;
            hour = Time.Hour;
            minute = Time.Minute;
            second = Time.Second;
            millisecond = Time.Millisecond;
            //Save file to Path with the current date as a filename
            string path;
            path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql";
            file = path;
            using (MySqlConnection conn = new MySqlConnection(connectionString))
            {
                using (MySqlCommand cmd = new MySqlCommand())
                {
                    using (MySqlBackup mb = new MySqlBackup(cmd))
                    {
                        cmd.Connection = conn;
                        conn.Open();
                        mb.ExportToFile(file);
                        conn.Close();
                    }
                }
            }

Mysql备份中超时-C#

您可以使用"CommandTimeout"属性更改超时。

您可能还需要考虑许多其他备份方法(例如,通过数据库的管理工具)。例如,请参见以下内容:
https://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/meb-scheduled-backups.html

还有一个小问题。对于以下线路:

string path;
path = txb_Path.Text + year + "-" + month + "-" + day + "--" + hour + "-" + minute + "-" + second + ".sql";
file = path;

为什么不做一些类似的事情呢

file = String.Format("{0}{1}-{2}-3--{4}-{5}...", txb_Path.Text, year, month...);

像您所做的字符串串联在.NET中实际上相当昂贵,因为.NET字符串是不可变的。