Mysql 备份超时 - C#

Timeout expired in Mysql backup - C#

我构建了一个应用程序来从服务器备份我的 MYSQL 数据库,日复一日,数据库变得比以前更大,有时会触发错误(从我的角度来看):

Message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. Full: MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.TimeoutException: Timeout in IO operation at MySql.Data.MySqlClient.TimedStream.StopTimer() at MySql.Data.MySqlClient.TimedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.IO.BufferedStream.Read(Byte[] array, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.ReadFully(Stream stream, Byte[] buffer, Int32 offset, Int32 count) at MySql.Data.MySqlClient.MySqlStream.LoadPacket() at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.FetchDataRow(Int32 statementId, Int32 columns) at MySql.Data.MySqlClient.Driver.FetchDataRow(Int32 statementId, Int32 columns) at MySql.Data.MySqlClient.ResultSet.GetNextRow() at MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlDataReader.Read() at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception) at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) at MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThreadAbort(Exception ex) at MySql.Data.MySqlClient.MySqlDataReader.Read() at MySql.Data.MySqlClient.MySqlBackup.Export_RowsData(String tableName, String selectSQL) at MySql.Data.MySqlClient.MySqlBackup.Export_Rows(String tableName, String selectSQL) at MySql.Data.MySqlClient.MySqlBackup.Export_TableRows() at MySql.Data.MySqlClient.MySqlBackup.ExportStart() at MySql.Data.MySqlClient.MySqlBackup.ExportToFile(String filePath) at MYSQL_Auto_Backup.Form1.Backup() in c:\Users\Belal\Documents\Visual Studio 2012\Projects\MYSQL Auto Backup\MYSQL Auto Backup\Form1.cs:line 132

代码:

// 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();
                    }
                }
            }

您可以使用 "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 字符串是不可变的。