简体   繁体   English

从 SQL 转储还原数据库时启用二进制模式

[英]Enable binary mode while restoring a Database from an SQL dump

I am extremely new to MySQL and am running it on Windows.我对 MySQL 非常陌生,并且正在 Windows 上运行它。 I am trying to restore a Database from a dumpfile in MySQL, but I get the following error:我正在尝试从 MySQL 中的转储文件恢复数据库,但出现以下错误:

$ >mysql -u root -p -h localhost -D database -o < dump.sql
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode. Set --binary-mode to 1 if ASCII '\0' is expected. Query: 'SQLite format 3'.

I have tried putting --binary-mode in the ini file but it still gives the same error.我试过将--binary-mode放在 ini 文件中,但它仍然给出相同的错误。 What should I do?我该怎么办? Please help.请帮忙。

UPDATE更新

As suggested by Nick in his comment I tried $ > mysql -u root -p -h localhost -D database --binary-mode -o < dump.sql but it gave me the following ERROR at line 1: Unknown command '\\☻'.正如尼克在他的评论中所建议的,我尝试了$ > mysql -u root -p -h localhost -D database --binary-mode -o < dump.sql但它ERROR at line 1: Unknown command '\\☻'.给了我以下ERROR at line 1: Unknown command '\\☻'. It is a 500 Mb dump file, and when I view its contents using gVIM, all I can see is expressions and data which is not comprehensible.这是一个 500 Mb 的转储文件,当我使用 gVIM 查看其内容时,我只能看到无法理解的表达式和数据。

解压缩文件,然后再次导入。

I meet the same problem in windows restoring a dump file.我在 Windows 恢复转储文件时遇到了同样的问题。 My dump file was created with windows powershell and mysqldump like:我的转储文件是用 windows powershell 和 mysqldump 创建的,如:

mysqldump db > dump.sql

The problem comes from the default encoding of powershell is UTF16.问题来自powershell的默认编码是UTF16。 To look deeper into this, we can use "file" utility of GNU, and there exists a windows version here .为了更深入了解这一点,我们可以使用GNU的“文件”实用工具,并且存在一个Windows版本在这里
The output of my dump file is:我的转储文件的输出是:

Little-endian UTF-16 Unicode text, with very long lines, with CRLF line terminators. Little-endian UTF-16 Unicode 文本,带有很长的行,带有 CRLF 行终止符。

Then a conversion of coding system is needed, and there are various software can do this.然后需要进行编码系统的转换,有各种软件可以做到这一点。 For example in emacs,例如在 emacs 中,

Mx set-buffer-file-coding-system

then input required coding system such as utf-8.然后输入所需的编码系统,例如utf-8。

And in the future, for a better mysqldump result, use:将来,为了获得更好的 mysqldump 结果,请使用:

mysqldump <dbname> -r <filename>

and then the output is handled by mysqldump itself but not redirection of powershell.然后输出由mysqldump本身处理,而不是 powershell 的重定向。

reference: https://dba.stackexchange.com/questions/44721/error-while-restoring-a-database-from-an-sql-dump参考: https : //dba.stackexchange.com/questions/44721/error-while-restoring-a-database-from-an-sql-dump

In Windows machine, please follows the preceding steps.在 Windows 机器上,请按照上述步骤操作。

  1. Open file in notepad.在记事本中打开文件。
  2. Click on Save as点击另存为
  3. Select Encoding type UTF-8.选择编码类型 UTF-8。

Now source your db.现在来源你的数据库。

Extract your file with Tar archiving tool.使用 Tar 归档工具提取您的文件。 you can use it in this way:你可以这样使用它:

tar xf example.sql.gz

I had this error once, after running mysqldump on Windows PowerShell like so:在 Windows PowerShell 上运行mysqldump后,我遇到过一次此错误,如下所示:

mysqldump -u root p my_db --no-data --no-create-db --no-create-info --routines --triggers --skip-opt --set-gtid-purged=OFF > db_objects.sql

What I did was change it to this (pipe instead to Set-Content):我所做的是将其更改为此(管道而不是 Set-Content):

mysqldump -u root p my_db --no-data --no-create-db --no-create-info --routines --triggers --skip-opt --set-gtid-purged=OFF | Set-Content db_objects.sql

And the problem went away!问题就解决了!

Have you tried opening in notepad++ (or another editor) and converting/saving us to UTF-8?您是否尝试过在记事本++(或其他编辑器)中打开并将我们转换/保存为 UTF-8?

See: notepad++ converting ansi encoded file to utf-8请参阅: notepad++ 将 ansi 编码文件转换为 utf-8

Another option may be to use textwrangle to open and save the file as UTF-8: http://www.barebones.com/products/textwrangler/另一种选择可能是使用 textwrangle 打开文件并将其保存为 UTF-8: http : //www.barebones.com/products/textwrangler/

If you don't have enough space or don't want to waste time decompressing it, Try this command.如果你没有足够的空间或者不想浪费时间解压,试试这个命令。

gunzip < compressed-sqlfile.gz | mysql -u root -p

Don't forget to replace compressed-sqlfile.gz with your file name.不要忘记用您的文件名替换compressed-sqlfile.gz。

.gz restore will not work without the command I provided above.如果没有我上面提供的命令,.gz restore 将无法工作。

可能是你的 dump.sql 在你的文件开头有垃圾字符或者开头有一个空行。

它必须你文件 dump.sql 问题。使用 Sequel Pro 检查你的文件编码。它应该是你的 dump.sql 中的垃圾字符。

I had the same problem, but found out that the dump file was actually a MSSQL Server backup, not MySQL.我遇到了同样的问题,但发现转储文件实际上是 MSSQL Server 备份,而不是 MySQL。

Sometimes legacy backup files play tricks on us.有时,旧的备份文件会欺骗我们。 Check your dump file.检查您的转储文件。

On terminal window:在终端窗口:

~$ cat mybackup.dmp 

The result was:结果是:

TAPE??G?"5,^}???Microsoft SQL ServerSPAD^LSFMB8..... etc...

To stop processing the cat command:要停止处理 cat 命令:

CTRL + C

zcat /path/to/file.sql.gz | mysql -u 'root' -p your_database

The file you are trying to import is a zip file.您尝试导入的文件是一个 zip 文件。 Unzip the file and then try to import again.解压缩文件,然后再次尝试导入。

Under linux Ungzip your file using gunzip Edit your unzip sql file using在 linux 下使用 gunzip Ungzip 您的文件使用编辑您的 unzip sql 文件

vi unzipsqlfile.sql vi 解压sqlfile.sql

Remove the first binary line with esc dd go to the bottom of the file with esc shift g remove the last binary line with dd save the file esc x: Then reimport to mysql with :使用 esc dd 删除第一个二进制行 使用 esc shift g 移至文件底部 使用 dd 删除最后一个二进制行 保存文件 esc x: 然后使用以下命令重新导入 mysql:

mysql -u username -p new_database < unzipsqlfile.sql mysql -u 用户名 -p new_database < unzipsqlfile.sql

I performed that with a 20go sql file from a jetbackup cpanel mysql backup.我使用来自 jetbackup cpanel mysql 备份的 20go sql 文件执行该操作。 Be patient to wait vi doing the job for big files耐心等待 vi 处理大文件

Your File should be only .sql extension, (.zip, .gz .rar) etc will not support.您的文件应该只是 .sql 扩展名,(.zip、.gz .rar)等将不支持。 example: dump.sql示例:dump.sql

您可以使用它来修复错误:

zcat {address_sql_database(.tar.gz)} | mysql -u root -p {database_name} --binary-mode

I know the original posters question was solved, but I came here via Google, and the various answers eventually led me to discovering that my SQL was dumped with a different default charset than the one used to import it.我知道最初的海报问题已经解决,但我是通过谷歌来到这里的,各种答案最终让我发现我的 SQL 被转储了一个不同于导入它的默认字符集。 I got the same error as the original question, but as our dump was piped into another MySQL client, we couldn't go the route of opening it with another tool and saving it differently.我遇到了与原始问题相同的错误,但是当我们的转储通过管道传输到另一个 MySQL 客户端时,我们无法使用其他工具打开它并以不同方式保存它。

For us, the solution turned out to be the --default-character-set=utf8mb4 option, to be used both on the call of mysqldump as well as the call to import it via mysql .对我们来说,解决方案是--default-character-set=utf8mb4选项,用于mysqldump的调用以及通过mysql导入它的调用。 Of course, the value of the parameter may differ for others facing the same problem, it's just important to keep it the same, as the servers (or the tools) default setting might be any charset.当然,对于面临相同问题的其他人,该参数的值可能会有所不同,保持它相同很重要,因为服务器(或工具)的默认设置可能是任何字符集。

Old but gold!老但金!

On MacOS (Catalina 10.15.7) it was a bit weird: I had to rename my dump.sql into dump.zip and after that, i had to use finder(!) to unzip it.在 MacOS (Catalina 10.15.7) 上,这有点奇怪:我不得不将dump.sql重命名为dump.zip ,之后,我不得不使用 finder(!) 解压缩它。 in terminal, unzip dump.zip oder tar xfz dump.sql[or .gz .tar ...] leads to error msgs.在终端中, unzip dump.ziptar xfz dump.sql[or .gz .tar ...]导致错误消息。

Finally, finder has unziped it totally fine, after that i could import the file without problems.最后,finder 已经完全解压了它,之后我可以毫无问题地导入文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM