来自Powershell的MysqlDump和Windows编码

来自Powershell的MysqlDump和Windows编码

本文介绍了来自Powershell的MysqlDump和Windows编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 & mysqldump -u root -p --default-character-set = utf8 -W -B dbname 
> C:\mysql_backup.sql

我的数据库/表使用UTF-8编码,我指定当我进行转储时,编码相同。但是当我用Notepad ++或Scite打开文件时,我看到一个UTF-16(UCS-2)的编码。如果在运行导入之前,我不使用iconv将文件转换为UTF-8,则出现错误。



似乎MS-DOS / CMD.exe正在重定向默认使用UTF-16。我可以更改吗?



一个附注:我使用Powershell来调用mysqldump。



更新:似乎它只在从Powershell调用mysqldump时发生。我使用我在PS脚本中使用的命令行更改命令行

解决方案

默认情况下,PowerShell会将文本代码为Unicode并保存它默认保存为Unicode的文件。您可以使用 Out-File cmdlet而不是> 运算符更改文件保存格式,例如: p>

  ... | Out-File C:\mysql_backup.sql -Encoding UTF8 

您可能还需要给PowerShell一个提示如何解释来自转储利用的UTF8文本。 展示了如何处理这种情况,实用程序没有输出正确的UTF8 BOM。


I'm doing an export from command line on ms-dos with mysqldump:

& mysqldump -u root -p --default-character-set=utf8 -W -B dbname
> C:\mysql_backup.sql

My database/tables are encoded with UTF-8 and I specify the same encoding when I did the dump. But when I open the file with Notepad++ or Scite I see an encoding of UTF-16 (UCS-2). If I don't convert the file with iconv to UTF-8 before running the import I got an error.

It seems that MS-DOS / CMD.exe is redirecting by default with UTF-16. Can I change this ?

A side note: I use Powershell to call mysqldump.

UPDATE: it seems that it occurs only when calling mysqldump from Powershell. I change the command line with the one I use in my PS script

解决方案

By default PowerShell represents text as Unicode and when you save it to a file it saves as Unicode by default. You can change the file save format by using the Out-File cmdlet instead of the > operator e.g.:

... | Out-File C:\mysql_backup.sql -Encoding UTF8

You may also need to give PowerShell a hint on how to interpret the UTF8 text coming from the dump utiltiy. This blog post shows how to handle this scenario in the event the utility isn't outputting a proper UTF8 BOM.

这篇关于来自Powershell的MysqlDump和Windows编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:41