将 MySQL 数据库从拉丁语转换为 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2543993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:40:06  来源:igfitidea点击:

Convert a MySQL database from latin to UTF-8

mysqlutf-8character-encoding

提问by Matthieu Napoli

I am converting a website from ISO to UTF-8, so I need to convert the MySQL database too.

我正在将网站从 ISO 转换为 UTF-8,因此我也需要转换 MySQL 数据库。

On the Internet, I read various solutions, I don't know which one to choose.

在网上看了各种解决方案,不知道选哪个。

Do I really need to convert my varchar columns to binary, then to UTF-8 like that:

我真的需要将我的 varchar 列转换为二进制,然后像这样转换为 UTF-8:

ALTER TABLE t MODIFY col BINARY(150);
ALTER TABLE t MODIFY col CHAR(150) CHARACTER SET utf8;

It takes a long time to do that for each column, of each table, of each database.

为每个数据库的每个列、每个表的每个表执行此操作需要很长时间。

I have 10 databases, with 20 tables each, with around 2 - 3 varchar columns (2 queries each column), this gives me around 1000 queries to write! How to do it?

我有 10 个数据库,每个数据库有 20 个表,大约有 2-3 个 varchar 列(每列 2 个查询),这使我可以编写大约1000 个查询!怎么做?

Resolved: I post the code that I have used:

已解决:我发布了我使用过的代码:

PASSWORD=""
db=

mysqldump --password=$PASSWORD --set-charset --skip-set-charset --add-drop-table --databases "$db" > /home/dev/backup/bdd.sql

QUERY="ALTER DATABASE \`$db\` DEFAULT CHARACTER SET utf8;"
mysql --password=$PASSWORD --database "$db" -e "$QUERY"

mysql --password=$PASSWORD --default-character-set=utf8 < /home/dev/backup/bdd.sql

See the answer below for more information.

有关更多信息,请参阅下面的答案。

采纳答案by Your Common Sense

You can do that very easily using a dump. Make a dump using

您可以使用转储非常轻松地做到这一点。使用转储

mysqldump --skip-opt --set-charset --skip-set-charset 

Then create another database, set its default character set to UTF-8 and then load your dump back with:

然后创建另一个数据库,将其默认字符集设置为 UTF-8,然后使用以下命令加载转储:

mysql --default-character-set=<your iso encoding>

The main idea is to make a dump without any sign of data encoding.
So, at create time, the table's encoding would be inherit from the database encoding and set to UTF-8. And with --default-character-setwe tell MySQL to recode our data automatically.

主要思想是在没有任何数据编码迹象的情况下进行转储。
因此,在创建时,表的编码将从数据库编码继承并设置为 UTF-8。而随着--default-character-set我们告诉MySQL自动重新编码我们的数据。

回答by Klaus Hessellund

I'm using mysqldump Ver 10.11 Distrib 5.0.77

我正在使用 mysqldump Ver 10.11 Distrib 5.0.77

For some reason create options Engine and auto_increment was omitted. This caused a lot of insert errors, because auto_increment was gone on primary key fields.

出于某种原因,省略了创建选项 Engine 和 auto_increment 。这导致了很多插入错误,因为主键字段上的 auto_increment 消失了。

This worked for me. I'm using --opt and using sed to remove charset from sql file.

这对我有用。我正在使用 --opt 并使用 sed 从 sql 文件中删除字符集。

mysqldump -p --opt --skip-set-charset --add-drop-table dbname > /tmp/dbname.sql
sed -i 's/DEFAULT CHARSET=latin1//g' /tmp/dbname.sql 
ALTER DATABASE dbname DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_danish_ci;
mysql -p --default-character-set=utf8 db < /tmp/dbname.sql 

回答by Max Zhuk

mysqldump --opt --skip-set-charset --default-character-set='latin1' -u root -p revive_adserver --result-file='dump.sql'

vim dump.sql

:%s/latin1/utf8/gi

:wq

mysql -u root -p

ALTER DATABASE revive_adserver CHARACTER SET utf8;

\q

mysql -D revive_adserver -u root -p < dump.sql