将管道文件内容Powershell加载到应用程序中,而不将文件加载到内存中(Powershell pipe file contents into application without loading

编程入门 行业动态 更新时间:2024-10-24 22:30:18
将管道文件内容Powershell加载到应用程序中,而不将文件加载到内存中(Powershell pipe file contents into application without loading file in memory)

使用cmd我会运行mysql -uroot database < filename.sql来导入数据库转储(从文件读取并传递给MySQL)。 但是,在powershell中<是“保留”。

相反,在PowerShell中,我使用get-content filename.sql | mysql -uroot database get-content filename.sql | mysql -uroot database 。 需要注意的是,在将filename.sql传递给MySQL之前,powershell会将filename.sql 完全入内存 ,而在大型数据库转储时,它只会耗尽内存。

很显然,我可以通过cmd来执行此操作,但我有一些PowerShell脚本可以自动执行这样的各种任务,而且我不想将它们全部批量重写。 在这种特殊情况下, filename.sql是自动启动时通过PS参数指定的变量。

那么如何解决这个内存限制? 是否有另一种方式直接将文件内容传送到MySQL?

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.

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.

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.

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​​#控制台应用程序。

You can Try

mysql -uroot -pYourPassword -e "source C:\temp\filename.SQL"

or

mysql --user=root --password=YourPassword --execute="source C:\temp\filename.SQL"

If things start to get complicated maybe you should write a C# Console application that does the complex tasks.

更多推荐

本文发布于:2023-07-26 02:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   文件   应用程序   不将   管道

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!