简体   繁体   English

Mysql 备份超时 - C#

[英]Timeout expired in Mysql backup - C#

I was built an application to backup my MYSQL DB from server, Day by Day, the database become bigger than before, which trigger an Error in some times (from my point of view):我构建了一个应用程序来从服务器备份我的 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.完整:MySql.Data.MySqlClient.MySqlException (0x80004005):超时已过期。 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.HandleTimeoutOrThr ---> System.TimeoutException:在 MySql.Data.MySqlClient.TimedStream.StopTimer() 的 IO 操作超时,在 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() 在 MySql.Data.MySqlClient.ResultSet.NextRow(CommandBehavior 行为) 在 MySql.Data.MySqlClient.MySqlDataReader.Read() 在 MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception 异常) 在 MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) 在 MySql.Data.MySqlClient.MySqlConnection.HandleTimeoutOrThr eadAbort(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 eadAbort(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) ) 在 MySql.Data.MySqlClient.MySqlBackup.Export_TableRows() 在 MySql.Data.MySqlClient.MySqlBackup.ExportStart() 在 MySql.Data.MySqlClient.MySqlBackup.ExportToFile(String filePath) 在 MYSQL_Auto_Backup.Form1.Backup() 在 c: \\Users\\Belal\\Documents\\Visual Studio 2012\\Projects\\MYSQL Auto Backup\\MYSQL Auto Backup\\Form1.cs:line 132

Code:代码:

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

You can change the timeout with the "CommandTimeout" property.您可以使用“CommandTimeout”属性更改超时。

There are a lot of other ways of doing the backup (eg from the database's administrative tools) that you might want to consider as well.还有很多其他的备份方法(例如从数据库的管理工具),您可能还想考虑。 See, for example, below:例如,请参见以下内容:
https://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/meb-scheduled-backups.html https://dev.mysql.com/doc/mysql-enterprise-backup/3.11/en/meb-scheduled-backups.html

One other minor point.另一个小点。 For the following line:对于以下行:

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

why not do something like为什么不做类似的事情

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

String concatenation like you're doing is actually fairly expensive in .NET because .NET strings are immutable.像您正在执行的字符串连接在 .NET 中实际上相当昂贵,因为 .NET 字符串是不可变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM