首页 经验

在 mysql 中默认使用 utf8mb4,而不是 utf8|Use utf8mb4 by default in mysql, not utf8 · Issue #16576 · yiisoft/yii2

时间: 2024-04-30 21:12:30

(部分内容来自网络,其真实性存疑,为了避免对您造成误导,请谨慎甄别。)


MySQL的“utf8”字符集,甚至不应该被称为,因为它不是实际的utf8,这是几年前发生的耻辱,因为MySQL团队中的某个人很愚蠢。MySQL 中真正符合 utf8 的字符集是 utf8mb4。在2018年,我们应该使用它,并鼓励将其用作MySQL的默认值。
例如,我认为与旧MySQL版本的任何假设兼容性问题都不会比当前与过时的php版本的现有不兼容性更严格,更值得关注。
使用MySQL的“utf8”字符集的灾难性后果要严重得多。

我安装了高级应用程序项目模板,我为它创建了一个数据库,其中 utf8mb4 utf8mb4_unicode_ci作为默认排序规则。然后我配置了main_local.php

            'dsn' => 'mysql:host=localhost;dbname=iframe2',
            //...
            'charset' => 'utf8mb4',

我按照其余的说明(包括运行迁移)启动并运行应用程序,我注意到已创建的用户表已将 utf8_unicode_ci(即“utf8”字符集)作为其默认值及其所有字段。

我不确定这是否是用户模型表的特定选择,因为相信允许在用户名中使用 4 字节字符(例如表情符号)不是一个好主意(这可能值得怀疑但值得商榷),或者这是否来自一般的“我们默认在任何地方都使用 utf8”政策。如果是后者,那我就很担心了。
尽管数据库默认值为 utf8mb4,但我不希望看到框架创建的更多带有 utf8 的表,特别是强制作为字符集。我可以理解强制使用特定的字符集而不是使用数据库默认值来执行某些事情,但是对于任何没有特定原因成为其他字符的东西,该特定字符集应该是 utf8mb4(这又是真正的 utf-8)。

在基本和高级应用程序的入门指南中,我看到您建议将 utf8 作为 MySQL 数据库配置中的字符集。同样,它应该是 utf8mb4

基本应用程序提供的实际配置文件也具有 UTF8。

希望这只是模板应用程序项目和文档中示例提供的默认配置文件的问题(显然在高级应用程序中还有其他东西,因为尽管数据库默认和配置,utf8 还是分配给了用户模型表),仅此而已。


MySQL's "utf8" charset, which shouldn't even be called that since it is not actual utf8, was a disgrace that happened years ago because someone in the MySQL team was stupid. The real utf8-conforming character set in MySQL is utf8mb4. In 2018, we should be using that and encouraging to use that as a default for MySQL.
Any hypothetical compatibility issue with old MySQL versions, I don't think is any more stringent, and worth caring about, than current existing incompatibilities with obsolete php versions, for example.
And the disastrous effects of using MySQL's "utf8" charset are much worse.

  1. I installed the Advanced Application Project template, I created a database for it with utf8mb4 utf8mb4_unicode_ci as the default collation. Then I configured main_local.php with

            'dsn' => 'mysql:host=localhost;dbname=iframe2',
            //...
            'charset' => 'utf8mb4',

I followed the rest of instructions (including running the migration) and have the application up and running, and I notice that the user table that has been created has utf8_unicode_ci (i.e. "utf8" charset) as its default and for all its fields.

I'm not sure whether this is a specific choice for the User model table based on the belief that allowing 4-byte characters such as smilies in a username wouldn't be a good idea (which could be questionable but arguable) or if this comes from a general "we use utf8 everywhere by default" policy. If it's the latter, then I'm seriously worried.
I wouldn't like to see any more tables created by the framework with utf8 specifically forced as the charset despite the database default being utf8mb4. I can understand forcing a specific charset rather than using the database default for some things, but then that specific charset should be utf8mb4 (which again is true utf-8) for anything that doesn't have a specific reason to be something else.

  1. In the Getting Started guides both for the basic and advanced apps I see you suggest utf8 as the charset in the configuration for a MySQL database. Again, it should be utf8mb4

  2. the actual config file provided with the basic app also has utf8.

Hopefully it's just a matter of the default configuration files provided with the template application projects and the examples in the docs (and apparently something else in the Advanced app, because utf8 was assigned to the user model table despite the database default and the config), and not much more than that.


上一个 mysql中text,longtext,mediumtext区别小结 文章列表 下一个 2024笔记本电脑新品

最新

工具

© 2019-至今 适观科技

沪ICP备17002269号