首页 > 代码库 > Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)

Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)

利用Mysqldump备份和迁移,我想很多人都用过,具体参数不介绍了,这里主要讲.NET调用Mysqldump进行备份和.NET调用Mysql.exe进行导入数据。

这里使用的是5.1版的Mysqldump.exe和Mysql.exe(5.5版的用过,没测试成功)

因为要解决导入时视图嵌视图会报错的情况,所以本案分成两步操作。

第一步:只迁移表结构、视图、存储过程、函数、事件等。(为何这里数据不一起导呢,因为到视图需要加上--extended-insert=false,这样导数据会很慢,所以分成两步导。)

        string strError = string.Empty;//执行cmd获取输出信息。
Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.RedirectStandardInput = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true;//获取输出流 //p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardError = true;//获取错误信息流 p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.CreateNoWindow = true; //除数据外,表结构、视图、存储过程、函数、事件全部导出 p.Start(); p.StandardInput.WriteLine("c:"); p.StandardInput.WriteLine("cd C:\\Program Files (x86)\\MySQL\\MySQL Server 5.1\\bin"); p.StandardInput.WriteLine("mysqldump -h 192.168.0.1 -P3306 -uroot -p123456 -q -d -R -E --skip-add-drop-table --default-character-set=utf8
          --extended-insert=false
MysqlDBName > c:\MysqlDBNameNoData.sql"); //-q 快速读取,-d不导数据,-R存储存储过程和函数,--extended-insert=false 逐行执行(视图嵌视图时需要用到),-E 导出事件,--add-drop-tables 删除表,--skip-add-drop-table不删除表 p.StandardInput.WriteLine("exit"); p.BeginOutputReadLine(); strError = p.StandardError.ReadToEnd(); p.WaitForExit(); if (!string.IsNullOrWhiteSpace(strError))//执行失败则跳出 { return ; } //去除DEFINER(如果数据库在不同的服务器,很可能会有权限问题,将权限这部分代码替换掉) StreamReader sr = new StreamReader(storeDBnoDataPath); Regex rg3 = new Regex("DEFINER=`.{1,50}`@`%`");//如果用户名比较长,这里可以设置的更长一些,但不要过度长,否则会替换掉需要的数据 string sql = sr.ReadToEnd(); sr.Close(); string strOutput = rg3.Replace(sql, ""); StreamWriter sw = new StreamWriter(storeDBnoDataPath, false, System.Text.Encoding.UTF8); sw.Write(strOutput); sw.Close(); //除数据外,表结构、视图、存储过程、函数、事件全部导入 p.Start(); p.StandardInput.WriteLine("c:"); p.StandardInput.WriteLine("cd C:\\Program Files (x86)\\MySQL\\MySQL Server 5.1\\bin"); p.StandardInput.WriteLine("mysql -h 192.168.0.2 -P3306 -uroot -p123456 MysqlDBName < c:\MysqlDBNameNoData.sql"); p.StandardInput.WriteLine("exit"); strError = p.StandardError.ReadToEnd(); p.WaitForExit(); if (!string.IsNullOrWhiteSpace(strError))//执行失败则跳出 { return ; }

 

第二部:导数据。(为了使导入数据比较快,不用加上--extended-insert=false)

 

 //只导出数据            p.Start();            p.StandardInput.WriteLine("c:");
p.StandardInput.WriteLine("cd C:\\Program Files (x86)\\MySQL\\MySQL Server 5.1\\bin");
p.StandardInput.WriteLine("mysqldump -h 192.168.0.1 -P3306 -uroot -p123456 -q --no-create-info --default-character-set=utf8 --skip-add-locks
            MysqlDBName < c:\MysqlDBNameOnlyData.sql“);//-q --no-create-info 只导出数据 --skip-add-locks 不添加表lock p.StandardInput.WriteLine("exit"); strError = p.StandardError.ReadToEnd(); p.WaitForExit(); if (!string.IsNullOrWhiteSpace(strError)) { return ; } //导入数据 p.Start(); p.StandardInput.WriteLine("c:"); p.StandardInput.WriteLine("cd C:\\Program Files (x86)\\MySQL\\MySQL Server 5.1\\bin"); p.StandardInput.WriteLine("mysql -h 192.168.0.2 -P3306 -uroot -p123456 MysqlDBName < c:\MysqlDBNameOnlyData.sql“); p.StandardInput.WriteLine("exit"); strError = p.StandardError.ReadToEnd(); p.WaitForExit(); if (!string.IsNullOrWhiteSpace(strError)) { return ; } //关闭进程 p.Close();

 

Mysql备份迁移——Mysqldump(.NET调用Mysqldump.exe方式)——(解决视图嵌视图报错)