gpt4 book ai didi

MySQL转换为UTF8而不改变结构

转载 作者:行者123 更新时间:2023-11-29 01:37:21 25 4
gpt4 key购买 nike

我有一个相当大的数据库,我正在尝试将其从字符集和排序规则 latin1/latin1_swedish_ci 转换为 utf8mb4/utf8mb4_unicode_ci。我希望设置复制到一个从站,运行转换,然后在完成后提升从站以避免停机。

我注意到在运行查询时...

ALTER TABLE `sometable` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

...MySQL自动将text转换为mediumtext或将mediumtext转换为longtext

有没有办法关闭这个功能? MySQL 有这个功能很好,但问题是它会破坏复制,因为从属服务器上的表结构与主服务器不同。

最佳答案

根据 ALTER TABLE Syntax 记录:

For a column that has a data type of VARCHAR or one of the TEXT types, CONVERT TO CHARACTER SET will change the data type as necessary to ensure that the new column is long enough to store as many characters as the original column. For example, a TEXT column has two length bytes, which store the byte-length of values in the column, up to a maximum of 65,535. For a latin1 TEXT column, each character requires a single byte, so the column can store up to 65,535 characters. If the column is converted to utf8, each character might require up to three bytes, for a maximum possible length of 3 × 65,535 = 196,605 bytes. That length will not fit in a TEXT column's length bytes, so MySQL will convert the data type to MEDIUMTEXT, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a VARCHAR column might be converted to MEDIUMTEXT.

To avoid data type changes of the type just described, do not use CONVERT TO CHARACTER SET. Instead, use MODIFY to change individual columns. For example:

ALTER TABLE t MODIFY latin1_text_col TEXT CHARACTER SET utf8;
ALTER TABLE t MODIFY latin1_varchar_col VARCHAR(<strong><em>M</em></strong>) CHARACTER SET utf8;

关于MySQL转换为UTF8而不改变结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37356693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com