如何进行MySQL数据库备份
- 我需要使用MySql Query备份整个数据库
2。使用c#代码备份数据库
2.Also to take a backup of DB using c# code
我的应用程序是独立应用程序,并使用vs2010。
这是我到目前为止尝试过的。无法确定抛出了什么错误。任何建议都将有所帮助。对此的任何帮助都将有很大帮助
My application is an standalone application and using 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();
在此处输入代码
如果是整个数据库,则:
If it's an entire DB, then:
$ mysqldump -u [uname] -p[pass] db_name > db_backup.sql
如果是所有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
您甚至可以使用gzip自动压缩输出(如果您的数据库非常大):
You can even go as far as auto-compressing the output using gzip (if your DB is very big):
$ mysqldump -u [uname] -p[pass] db_name | gzip > db_backup.sql.gz
如果要远程执行此操作,并且可以访问位于问题,那么以下方法将起作用(假定MySQL服务器在端口3306上):
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):
$ mysqldump -P 3306 -h [ip_address] -u [uname] -p[pass] db_name > db_backup.sql
要导入:
使用以下命令导入sql数据文件:
ype the following command to import sql data file:
$ mysql -u username -p -h localhost DATA-BASE-NAME < data.sql
在此示例中,使用vivek将 data.sql文件导入 blog数据库作为用户名:
In this example, import 'data.sql' file into 'blog' database using vivek as username:
$ mysql -u sat -p -h localhost blog < data.sql
如果您有专用的数据库服务器,请将localhost主机名替换为实际的服务器名或IP地址如下:
If you have a dedicated database server, replace localhost hostname with with actual server name or IP address as follows:
$ mysql -u username -p -h 202.54.1.10 databasename < data.sql
或使用主机名,例如mysql.cyberciti.biz
OR use hostname such as mysql.cyberciti.biz
$ mysql -u username -p -h mysql.cyberciti.biz database-name < data.sql
如果您不知道数据库名称或数据库名称包含在sql dump中,则可以尝试如下操作:
If you do not know the database name or database name is included in sql dump you can try out something as follows:
$ mysql -u username -p -h 202.54.1.10 < data.sql
引用: http://dev.mysql.com/doc/refman/5.6/en/mysqldump.html
使用C#备份MySQL中的数据库
备份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();
}
}
}
}
还原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();
}
}
}
}