原因在于mysql数据库编码格式utf8默认保存的是1到3个字节,而emoji表情采用4个字节保存,所以抛出异常。因此需要将编码格式转换为utf8mb4(mysql版本大于5.5)
设置数据库编码
// 设置编码
SET NAMES utf8mb4;
ALTER DATABASE `database_name` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;
查看数据库当前编码状态
// 查看编码状态
SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';
// 查看编码是否如下所示
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
character_sets_dir /usr/local/mysql/share/charsets/
collation_connection utf8mb4_general_ci
collation_database utf8mb4_general_ci
collation_server utf8mb4_unicode_ci
设置mysql连接chatset编码
// 最容易被忽略的步骤:mysql连接组件
var connection = mysql.createConnection({
host : 'host',
user : 'user',
password : 'password',
database : 'database',
charset : 'utf8mb4'
});
原文地址:http://www.fidding.me/article/43
happy coding!