使用 Force 在 Windows 上导入大型 MySQL .sql 文件

2021-12-25 00:00:00 windows sql import powershell mysql

我想在 Windows 7 机器上导入一个 350MB 的 MySQL .sql 文件.我通常通过使用

I would like to import a 350MB MySQL .sql file on a Windows 7 machine. I usually do this by using

mysql -uuser -p -e "source c:/path/to/file.sql" database

因为 <在 Powershell 中不起作用.

since < doesn't work in Powershell.

这次我的 .sql 文件有错误.我宁愿跳过坏行并继续导入.如何强制在 Windows 上继续导入?

My .sql file has an error in it this time. I'd prefer to just skip the bad row and continue the import. How can I force the import to continue on Windows?

在基于 unix/linux 的系统上,我可以使用

On a unix/linux based system, I could use

mysql --force ... < file.sql

但是 --force 似乎不适用于 -e "source ..." 命令(可以理解).

but --force doesn't seem to work with the -e "source ..." command (understandably so).

谢谢,迈克

推荐答案

为了正确使用 <,您可能必须让 Powershell 在标准控制台中执行此操作.从技术上讲,您可以使用 get-content 并将输出通过管道传输到 mysql,但我一直发现这很慢,而且它仍然以某种方式将文件内容保留在内存中Powershell 会话.

You're probably going to have to have Powershell execute this in the standard console in order to use < properly. Technically you could use get-content and pipe the output to mysql, but I've always found that to be slow, and it somehow still keeps the file contents in memory of the Powershell session.

这是我从 Powershell 提示符执行它的方式(更改文件路径以包含空格以演示内部引号,以防万一):

This is how I would execute it from the Powershell prompt (changed file path to include spaces to demonstrate inner quotes, just in case):

cmd /C 'mysql -uuser -p --force < "C:pathwith spaces	ofile.sql"'

[GC]::collect() 显然会清除它的内存,但无论如何你都不能这样做,直到它完成.说到mysqlmysqldump,我不介意Powershell.> 中使用的默认编码是 Unicode,除非您记得编写 |,否则 Powershell 中的转储文件是 cmd 中的两倍大.out-file dump.sql -enc ascii 而不是 >dump.sql.

[GC]::collect() would apparently clear it up the memory, but you can't do that until after it's done anyway. When it comes to mysql and mysqldump, I don't bother with Powershell. The default encoding used in > is Unicode, making dump files twice as big out of Powershell as out of cmd unless you remember to write | out-file dump.sql -enc ascii instead of > dump.sql.

相关文章