我有.sql文件(550 MB),我想将其导入到运行mysql服务器.我知道mysql.exe的路径.
我的想法是模仿命令行导入mysql -u user -ppass db_name < file.sql.这从命令行运行良好(我已设置高max_allowed_packet).根据Stackoverflow上的另一个帖子,我发现这个工作:
Process process = new Process();
process.StartInfo.FileName = mysqlexepath;
process.StartInfo.Arguments = "-v -u user -ppassworddbname";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
try
{
process.Start();
StreamWriter input = process.StandardInput;
using (StreamReader sr = new StreamReader(sqlfilepath))
{
while ((line = sr.ReadLine()) != null)
{
if (process.HasExited == true)
throw new Exception("DB went away.");
input.WriteLine(line);
input.Flush();
}
}
process.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
我可以看到如何在DB中创建表.但我的问题是大约一半的过程退出.我正在谷歌搜索一些超时设置,但无法找到任何东西.
我还尝试先读取文件:
var file = FileInfo(sqlfilepath);
StreamReader reader = file.OpenText();
string fileContents = reader.ReadToEnd();
StreamWriter input = process.StandardInput;
input.AutoFlush = true;
input.Write(fileContents);
input.Close();
Run Code Online (Sandbox Code Playgroud)
但我得到OutOfMemory异常.所以正确的方法不会导致字符串.
我非常感谢任何建议如何找出问题所在.我甚至不知道它的进程超时或mysql超时或者问题是否在StreamReader中.
csh*_*net 14
我知道这不是你问题的直接答案,说实话,我不确定你的方法有什么问题.我可以帮助分享我们如何使用mysql.exe运行非常大的sql脚本...
"C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin\mysql.exe" -C -B --password=[password] -P 3306 --user=[username] --host=localhost --database=[database] -e "\. C:\Backups\Mybackup.sql"
大多数这些参数都是显而易见的,连接信息等.
什么是不很明显就是神奇的部分-e "\. [filename]"的-e参数指定的MySQL应该运行下面的命令,然后退出.前缀"\. "表示应使用输入文件,后跟该文件名.
我们使用它来恢复多千兆字节的数据库而不会出现问题.所以这里是完整的'运行scirpt'与mssql ...
public static int RunMySql(string server, int port, string user, string password, string database, string filename)
{
var process = Process.Start(
new ProcessStartInfo
{
FileName = @"C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin\mysql.exe",
Arguments =
String.Format(
"-C -B --host={0} -P {1} --user={2} --password={3} --database={4} -e \"\\. {5}\"",
server, port, user, password, database, filename),
ErrorDialog = false,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
WorkingDirectory = Environment.CurrentDirectory,
}
);
process.OutputDataReceived += (o, e) => Console.Out.WriteLine(e.Data);
process.ErrorDataReceived += (o, e) => Console.Error.WriteLine(e.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.StandardInput.Close();
process.WaitForExit();
return process.ExitCode;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3722 次 |
| 最近记录: |