简体   繁体   English

如何进行MySQL数据库备份

[英]How to take backup of MySQL Database

  1. I need to take a backup of my entire database using MySql Query 我需要使用MySql Query备份整个数据库

2.Also to take a backup of DB using c# code 2.也可以使用c#代码备份数据库

My application is an standalone application and using vs2010. 我的应用程序是一个独立的应用程序,并使用vs2010。 This what I have tried so far.Not able to identify what error is thrown.any suggestions would be of help.Any help on this will be of great help 这是我到目前为止尝试过的。无法确定抛出了什么错误。任何建议都将有所帮助。对此的任何帮助都将有很大帮助

enter code here

        int year = Time.Year;
        int month = Time.Month;
        int day = Time.Day;
        int hour = Time.Hour;
        int minute = Time.Minute;
        int second = Time.Second;
        int millisecond = Time.Millisecond;

        //Save file to C:\ with the current date as a filename
        string path;
        path = "D:\\yourfoldername" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + "-" + second + "-" + millisecond + ".sql";
        StreamWriter file = new StreamWriter(path);


        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "mysqldump";
        psi.RedirectStandardInput = false;
        psi.RedirectStandardOutput = true;
        psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "root", "", "localhost", "database");
        psi.UseShellExecute = false;

        Process process = Process.Start(psi);

        string output;
        output = process.StandardOutput.ReadToEnd();
        file.WriteLine(output);
        process.WaitForExit();
        file.Close();
        process.Close();

enter code here

If it's an entire DB, then: 如果是整个数据库,则:

$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql

If it's all DBs, then: 如果是所有DB,那么:

$ mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql

If it's specific tables within a DB, then: 如果是数据库中的特定表,则:

$ mysqldump -u [uname] -p[pass] db_name table1 table2 > table_backup.sql

You can even go as far as auto-compressing the output using gzip (if your DB is very big): 您甚至可以使用gzip来自动压缩输出(如果您的数据库很大):

$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz

If you want to do this remotely and you have the access to the server in question, then the following would work (presuming the MySQL server is on port 3306): 如果要远程执行此操作,并且可以访问有问题的服务器,则可以使用以下命令(假设MySQL服务器位于端口3306上):

$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql

To IMPORT: 导入:

ype the following command to import sql data file: ype以下命令导入sql数据文件:

$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

In this example, import 'data.sql' file into 'blog' database using vivek as username: 在此示例中,使用vivek作为用户名将“ data.sql”文件导入“博客”数据库:

$ mysql -u sat -p -h localhost blog < data.sql

If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows: 如果您有专用的数据库服务器,则将localhost主机名替换为实际的服务器名或IP地址,如下所示:

$ mysql -u username -p -h 202.54.1.10 databasename < data.sql

OR use hostname such as mysql.cyberciti.biz 或使用主机名,例如mysql.cyberciti.biz

$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql

If you do not know the database name or database name is included in sql dump you can try out something as follows: 如果您不知道数据库名称或数据库名称包含在sql dump中,则可以尝试以下操作:

$ mysql -u username -p -h 202.54.1.10 < data.sql

Refer: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html 参考: http : //dev.mysql.com/doc/refman/5.6/en/mysqldump.html

Backing up Database in MySQL using C# 使用C#在MySQL中备份数据库

Backup a MySQL database 备份MySQL数据库

private void Backup()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ExportToFile(file);
                conn.Close();
            }
        }
    }
}

Restore a MySQL database 恢复MySQL数据库

private void Restore()
{
    string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
    string file = "C:\\backup.sql";
    using (MySqlConnection conn = new MySqlConnection(constring))
    {
        using (MySqlCommand cmd = new MySqlCommand())
        {
            using (MySqlBackup mb = new MySqlBackup(cmd))
            {
                cmd.Connection = conn;
                conn.Open();
                mb.ImportFromFile(file);
                conn.Close();
            }
        }
    }
}

Take a look at this project: 看一下这个项目:

MySqlBackup.NET - MySQL Backup Solution for C# MySqlBackup.NET-适用于C#的MySQL备份解决方案

You do not have to write a lot of code with that. 您不必为此编写很多代码。 I am commenting an example for you from official documentation: 我正在从官方文档中为您评论一个示例:

Simple Export Example 简单导出示例

using MySql.Data.MySqlClient;
string constring = "server=localhost;user=root;pwd=qwerty;database=test;";
string file = "C:\\backup.sql";
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
    using (MySqlBackup mb = new MySqlBackup(cmd))
         {
        cmd.Connection = conn;
        conn.Open();
        mb.ExportToFile(file);
        conn.Close();
         }
     }
 }

I hope it helps for future readers. 希望对以后的读者有所帮助。

Backing up Database in MySQL using C# 使用C#在MySQL中备份数据库

See this Windows Form 查看此Windows表单

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > " + textBackup.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

textBackup.Text is actually the path with file, ex. textBackup.Text实际上是文件的路径,例如。 e:\\tmp.sql. E:\\ tmp.sql。

IF YOU WANT TO ACCESS AN USER DEFINED PATH THEN WRITE LIKE THIS ---- 如果要访问用户定义的路径,请像下面这样写----

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysqldump -u root --password=12345 -h localhost ""DATABASE_NAME"" > e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

Restore a MySQL database using C# 使用C#还原MySQL数据库

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < " + textRestore.Text + "");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

textRestore.Text is actually the path with file, ex. textRestore.Text实际上是文件的路径,例如。 e:\\tmp.sql. E:\\ tmp.sql。

IF YOU WANT TO ACCESS AN USER DEFINED PATH THEN WRITE LIKE THIS ---- 如果要访问用户定义的路径,请像下面这样写----

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", @"/c mysql -u root -p12345 -h localhost ""DATABASE_NAME"" < e:\tmp.sql");
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin";
Process.Start(startInfo);

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

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