简体   繁体   English

如何让 mysqldump 以正确的编码和排序规则导出数据库?

[英]How to get mysqldump to export a database in the right encoding and collation?

I'm having a problem with the encoding when dumping a database using mysqldump .使用mysqldump转储数据库时,我遇到了编码问题。

The issue is that the file being generated is breaking non-ASCII characters (for ex. german and spanish characters).问题是正在生成的文件破坏了非 ASCII 字符(例如德语和西班牙语字符)。 The data in the DB is right, but it is exported wrong.数据库中的数据是正确的,但导出错误。

I have tried the following:我尝试了以下方法:

  1. using --default-character-set to utf8, utf8mb4, and latin1 (the last option because although the tables are using utf8_general_ci collation, the database itself is set to latin1, I don't know why).使用--default-character-set为 utf8、utf8mb4 和 latin1(最后一个选项,因为虽然表使用 utf8_general_ci 排序规则,但数据库本身设置为 latin1,我不知道为什么)。 Weirdly enough, the output differs in filesize, but the content (specially the problematic characters) shows the same issue in all three cases.奇怪的是,输出的文件大小不同,但内容(特别是有问题的字符)在所有三种情况下都显示相同的问题。 As if the option would be ignored.好像该选项将被忽略。
  2. importing the dumped file into a new mysql service, but since the characters are broken in the file, the import is also broken.将转储的文件导入新的mysql服务,但由于文件中的字符被破坏,导入也被破坏。 for ex.例如。 the dump with the utf8mb4 option is imported in a fresh database with character encoding utf8mb4, but since the source file is wrongly encoded, it is not being "transcoded back" to a right form.带有 utf8mb4 选项的转储被导入到具有字符编码 utf8mb4 的新数据库中,但由于源文件编码错误,它没有被“转码回”为正确的形式。
  3. Initially I thought that it could be an issue with the version of the mysql server being different (5.7 in the source, 8.0 in the destination server), but since the file seems to be already broken, I now think that this might not be the root-cause.最初我认为这可能是mysql服务器版本不同的问题(源服务器为5.7,目标服务器为8.0),但由于文件似乎已经损坏,我现在认为这可能不是根本原因。 Still lost, so I prefer to mention it just in case it helps.仍然迷路,所以我更愿意提及它以防万一它有帮助。

An example of the sentence I'm running:我正在运行的句子示例:

mysqldump --default-character-set=utf8mb4 --no-tablespaces -u database_user -p database_name > /home/username/database_name-utf8mb4-20220712.sql

No errors appear neither during the export nor during the import in the new server.在导出过程中和在新服务器中导入过程中都不会出现错误。 Everything seems to run smooth, but the character encoding is messed up, so something isn't OK.一切似乎都很顺利,但是字符编码搞砸了,所以有些事情不妙。

Any support is much appreciated.非常感谢任何支持。 Thank you!谢谢!

but the character encoding is messed up但是字符编码搞砸了

Give us an example.给我们一个例子。 Include a hex dump of a small portion of the file where garbage shows up.包括文件中出现垃圾的一小部分的十六进制转储。

It is likely that the original data was either in character set utf8 or latin1, but the dumping and/or reloading specified the wrong character set.原始数据可能是字符集 utf8 或 latin1,但转储和/或重新加载指定了错误的字符集。 Please provide more details of the dump and load.请提供转储和加载的更多详细信息。

Also see: Trouble with UTF-8 characters;另请参阅: UTF-8 字符问题; what I see is not what I stored 我看到的不是我存储的

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

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