SQLSTATE[HY000]: General error: 1366 Incorrect string value

在Laravel项目的 storages/logs/Laravel.log看到的错误信息片段:

SQLSTATE[HY000]: General error: 1366 Incorrect string value: 'xF0x9Fx91x8BxF0x9F...' for column 'name' at row 1 

出错原因:检查数据库此字段的字符集与整理字符集是否与SQL语句传递数据的字符集相同;不相同则会引发MySQL1366错误。

解决方法:

#1.检查数据表所有字段的状态
mysql> SHOW FULL COLUMNS FROM tdb_goods;
#2.发现collatioin项非utf8,需要进行修改,使用如下的语句
mysql> alter table tdb_goods change goods_name name varchar(100) character set utf8 collate utf8_unicode_ci not null default '';
#3.检查数据表所有字段的状态
mysql> SHOW FULL COLUMNS FROM tdb_goods;
mysql> select * from tdb_goodsg

mysql语句的g等同于;,G:将查询到的横向表格纵向输出,方便阅读


未进行修改my.cnf,下面内容可跳过。记录自己的了解和尝试

参考别人的博文,说是 mysql的配置sql-model不正确导致的,需要将 my.cnf中的sql-model中的STRICT_TRANS_TABLES删除

STRICT_TRANS_TABLES(存储引擎启用严格模式,非法数据值被拒绝)

我的mysql配置文件是在/etc/my.cnf

sudo cp /etc/my.cnf /etc/my.cnf_bak190628
sudo vim /etc/my.cnf

[mysqld]
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

改为

[mysqld]
sql_mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

删除了STRICT_TRANS_TABLES,然后重启 mysql


不修改 my.cnf (在不清楚my.cnf存放的位置时可使用的,但是在mysql服务重启之后,这个设置会失效)

show variables like 'sql_mode';

set global sql_mode='ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
References
  1. SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'xxx' at row 1
  2. mysql出现ERROR 1366 (HY000):的解决办法
  3. mysql中G和g的作用
  4. MySQL中g和G的作用
  5. How do I find the MySQL my.cnf location
原文地址:https://www.cnblogs.com/fsong/p/11259046.html