Powershell将文件内容管道传输到应用程序中,而无需在内存中加载文件 [英] Powershell pipe file contents into application without loading file in memory
问题描述
使用cmd
,我将运行mysql -uroot database < filename.sql
来导入数据库转储(从文件读取并传递给MySQL).但是,<
在powershell中是保留"的.
With cmd
I'd run mysql -uroot database < filename.sql
to import a database dump (read from file and pass to MySQL). However, <
is "reserved" in powershell.
相反,在powershell中,我使用get-content filename.sql | mysql -uroot database
.需要注意的是,powershell在将filename.sql
完全读入内存之前,将其传递给MySQL,并且对于大型数据库转储,它仅会耗尽内存.
Instead, in powershell I use get-content filename.sql | mysql -uroot database
. The caveat is that powershell reads filename.sql
completely into memory before passing it along to MySQL, and with large database dumps it simply runs out of memory.
很明显,我可以通过cmd
执行此操作,但是我有一些Powershell脚本可以自动执行类似这样的各种任务,因此我不想批量重写它们.在这种情况下,filename.sql
是一个变量,它在自动化开始时通过PS参数指定.
Obviously, I could execute this via cmd
but I have a handful of powershell scripts automating various tasks like this and I don't want to have to rewrite them all in batch. In this particular case, filename.sql
is a variable that's specified via PS parameters when the automation kicks off.
那么如何解决此内存限制?还有另一种直接将文件内容通过管道传递到MySQL的方法吗?
So how do I get around this memory limitation? Is there another way to pipe the file contents into MySQL directly?
推荐答案
您可以尝试
mysql -uroot -pYourPassword -e "source C:\temp\filename.SQL"
或
mysql --user=root --password=YourPassword --execute="source C:\temp\filename.SQL"
如果事情开始变得复杂,也许您应该编写一个执行复杂任务的C#控制台应用程序.
If things start to get complicated maybe you should write a C# Console application that does the complex tasks.
这篇关于Powershell将文件内容管道传输到应用程序中,而无需在内存中加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!