尝试存储表情符号时出错

时间:2017-01-25 15:52:08

标签: mysql node.js sequelize.js collation utf8mb4

我使用sequelize作为ORM,以便node.js与mysql数据库进行交互。

我的数据库字符集是utf8mb4, utf8mb4_general_ci

我的桌面字符集是utf8mb4, utf8mb4_general_ci

我的列TYPE是TEXT,charset是utf8mb4, utf8mb4_general_ci

在执行任何查询之前,我执行以下代码:

Sequelize.query("SET NAMES utf8mb4");
Sequelize.query("SET CHARACTER SET utf8mb4");

执行Sequelize.query(" SHOW VARIABLES LIKE' character_set _%'")之后,我得到了:

[ [ RowDataPacket { Variable_name: 'character_set_client', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_connection', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_database', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_filesystem', Value: 'binary' },
    RowDataPacket { Variable_name: 'character_set_results', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_server', Value: 'utf8' },
    RowDataPacket { Variable_name: 'character_set_system', Value: 'utf8' },
    RowDataPacket {
      Variable_name: 'character_sets_dir',
      Value: 'c:\\openserver\\modules\\database\\MySQL-5.7-x64\\share\\charsets\\' } ],
  [ RowDataPacket { Variable_name: 'character_set_client', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_connection', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_database', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_filesystem', Value: 'binary' },
    RowDataPacket { Variable_name: 'character_set_results', Value: 'utf8mb4' },
    RowDataPacket { Variable_name: 'character_set_server', Value: 'utf8' },
    RowDataPacket { Variable_name: 'character_set_system', Value: 'utf8' },
    RowDataPacket {
      Variable_name: 'character_sets_dir',
      Value: 'c:\\openserver\\modules\\database\\MySQL-5.7-x64\\share\\charsets\\' } ] ]

但是当我试图保存包含表情符号的字符串时,我遇到了错误:

Unhandled rejection SequelizeDatabaseError: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xF0\x9F\x91\x8D\xF0\x9F...' for column 'content' at row 1

2 个答案:

答案 0 :(得分:0)

我也遇到了这个问题并且很容易解决,你只需要将你的数据库设置为utf8_general_ci。在node.js上进行连接时,你需要添加" charset"参数:

Dispose

这对我有用。

答案 1 :(得分:0)

我在尝试使用Express和Sequelize将表情符号插入MySQL时遇到了同样的问题。您可以确认当前的字符集,如

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

上面的默认连接输出如下所示

character_set_client      utf8
character_set_connection  utf8
character_set_database    utf8mb4
character_set_filesystem  binary
character_set_results     utf8
character_set_server      utf8mb4
character_set_system      utf8
collation_connection      utf8_general_ci
collation_database        utf8mb4_unicode_ci
collation_server          utf8mb4_unicode_ci

如果您的MySQL配置文件/etc/my.cnf看起来像

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

但如果你跑

SET NAMES utf8mb4;

然后再次从上面SHOW VARIABLES WHERE...它将全面更新到utf8mb4。

character_set_client      utf8mb4
character_set_connection  utf8mb4
character_set_database    utf8mb4
character_set_filesystem  binary
character_set_results     utf8mb4
character_set_server      utf8mb4
character_set_system      utf8
collation_connection      utf8mb4_general_ci
collation_database        utf8mb4_unicode_ci
collation_server          utf8mb4_unicode_ci

因此,我找到的解决方案是将以下内容放在我的用户模型的第一行:

// make sure db/client/connection can support emoji
sequelize.query("SET NAMES utf8mb4;");
// confirm settings
sequelize.query("SHOW VARIABLES LIKE 'character_set_%'").then(function(data) {
     console.log(data);
});