如何将数据库中的所有表更改为UTF8字符集?

发布于 2024-08-19 08:59:47 字数 41 浏览 4 评论 0原文

我的数据库不是 UTF8,我想将所有表转换为 UTF8,我该怎么做?

My database is not in UTF8, and I'd like to convert all the tables to UTF8, how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

攒一口袋星星 2024-08-26 08:59:47

对于单个表,您可以执行以下操作:

ALTER TABLE tab CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

对于整个数据库,我不知道除了类似于此的其他方法:

http://www.commandlinefu.com/commands/view/1575/convert-all-mysql-tables-and-fields-to-utf8

For single table you can do something like this:

ALTER TABLE tab CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

For the whole database I don't know other method than similar to this:

http://www.commandlinefu.com/commands/view/1575/convert-all-mysql-tables-and-fields-to-utf8

雨夜星沙 2024-08-26 08:59:47

my_database_name 替换为您的数据库名称,

SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'my_database_name' AND TABLE_TYPE != 'VIEW';

这将构建大量可以运行的查询

replace my_database_name with your database name

SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'my_database_name' AND TABLE_TYPE != 'VIEW';

this will build lots of queries which you can run

夏有森光若流苏 2024-08-26 08:59:47
mysqldump --user=username --password=password --default-character-set=latin1 --skip-set-charset dbname > dump.sql
sed -r 's/latin1/utf8/g' dump.sql > dump_utf.sql
mysql --user=username --password=password --execute="DROP DATABASE dbname; CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql --user=username --password=password --default-character-set=utf8 dbname < dump_utf.sql
mysqldump --user=username --password=password --default-character-set=latin1 --skip-set-charset dbname > dump.sql
sed -r 's/latin1/utf8/g' dump.sql > dump_utf.sql
mysql --user=username --password=password --execute="DROP DATABASE dbname; CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql --user=username --password=password --default-character-set=utf8 dbname < dump_utf.sql
驱逐舰岛风号 2024-08-26 08:59:47

要更改 phpMyAdmin 中的排序规则,只需按照以下简单步骤操作:

方法 1

  • 打开 phpMyAdmin 并选择您的数据库。

  • 单击数据库后,单击操作选项卡。
    输入图片此处描述

  • 接下来,向下滚动页面,您将看到排序规则部分。

    • 设置您想要的排序规则并点击复选框。
    • 您的排序规则将会更新。
      输入图片此处描述

注意:如果您在使用方法 1 时发现任何困难,请使用 sql 命令行按照方法 2 进行操作。

方法2

使用命令行

  • 打开phpMyAdmin并点击SQL选项卡。
    输入图片此处描述
  • 接下来,编写用于更改数据库排序规则的命令。
# syntax command:

ALTER DATABASE your_db_name CHARACTER SET utf8 COLLATE write_collation;

# e.g:
ALTER DATABASE temprory CHARACTER SET utf8 COLLATE utf8_general_ci;

To change the collation in phpMyAdmin just follow this simple steps:

Method 1

  • open phpMyAdmin and select your database.

  • After click on database click on operation tab.
    enter image description here

  • Next, Scroll down the page, you will see the collation section.

    • set the collation do you want and click the checkbox.
    • your collation will be updated.
      enter image description here

Note: If you find any difficulty using method 1 follow method 2 using sql command line.

Method 2

Using command Line

  • Open phpMyAdmin and click on SQL tab.
    enter image description here
  • Next, write command for changing the collation for your database.
# syntax command:

ALTER DATABASE your_db_name CHARACTER SET utf8 COLLATE write_collation;

# e.g:
ALTER DATABASE temprory CHARACTER SET utf8 COLLATE utf8_general_ci;
花开浅夏 2024-08-26 08:59:47

更好的是,使用 Percona 的工具包。我会在更新到 utf8mb4 之前审核您的索引,因为密钥长度存在问题。

SELECT CONCAT('pt-online-schema-change --alter "CONVERT TO CHARACTER SET utf8 
COLLATE utf8_unicode_ci" t=', TABLE_NAME, ',D=DB_NAME,u=USER_NAME --statistics --execute') 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'DB_NAME' AND TABLE_TYPE != 'VIEW' AND TABLE_COLLATION NOT LIKE '%utf8%';

Better yet, use Percona's tool kit. I'd audit your indices before updating to utf8mb4 as there are issues with key length.

SELECT CONCAT('pt-online-schema-change --alter "CONVERT TO CHARACTER SET utf8 
COLLATE utf8_unicode_ci" t=', TABLE_NAME, ',D=DB_NAME,u=USER_NAME --statistics --execute') 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'DB_NAME' AND TABLE_TYPE != 'VIEW' AND TABLE_COLLATION NOT LIKE '%utf8%';
长亭外,古道边 2024-08-26 08:59:47

就我而言,由于迁移失败,我有几个模式。

加入其他答案,在这种情况下,我为 charset=latin1 和 collat​​e=latin1_general_ci 执行了 ALTER 命令生成器,以获取与 latin1_general_ci 不匹配的所有模式的所有表

希望它有所帮助。

    SELECT CONCAT('ALTER TABLE ', tbl.ts,'.',tbl.tn, ' CHARACTER SET latin1
    COLLATE latin1_general_ci, CHANGE COLUMN \`', tbl.tc, '\` \`'
    ,tbl.tc,'\` ', tbl.tct, ' CHARACTER SET latin1 COLLATE 
    latin1_general_ci;' ) command FROM (SELECT table_schema ts, table_name 
    tn, column_name tc, COLUMN_TYPE tct FROM information_schema.columns  
    WHERE collation_name != 'latin1_general_ci' AND table_schema not in
    ('information_schema','mysql','performance_schema','sys')) tbl;

in my case I have several schemas due to a bad migration.

Joining the other answers I did an ALTER command generator in this case to charset=latin1 and collate=latin1_general_ci for all the tables of all the schemas that doesn`t match latin1_general_ci

Hope it helps.

    SELECT CONCAT('ALTER TABLE ', tbl.ts,'.',tbl.tn, ' CHARACTER SET latin1
    COLLATE latin1_general_ci, CHANGE COLUMN \`', tbl.tc, '\` \`'
    ,tbl.tc,'\` ', tbl.tct, ' CHARACTER SET latin1 COLLATE 
    latin1_general_ci;' ) command FROM (SELECT table_schema ts, table_name 
    tn, column_name tc, COLUMN_TYPE tct FROM information_schema.columns  
    WHERE collation_name != 'latin1_general_ci' AND table_schema not in
    ('information_schema','mysql','performance_schema','sys')) tbl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文