summernote 编辑器保存的内容超出了 MySQL TEXT 的长度限制

更新日期: 2018-01-26 阅读次数: 12579 分类: MySQL

昨晚收到一堆微信小程序后台的报警邮件。但是邮件内容看不到,登录服务器查看 laravel 日志,发现是 summernote 保存的商品描述过长,超出了 TEXT 的长度限制

String data, rig
ht truncated: 1406 Data too long for column 'description' at row 1 at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:107, PDOException(code: 22001):
 SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'description' at row 1 at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.p
hp:105)

至此,我也明白了为啥邮件中的异常信息看不到的原因。邮件内容过多,QQ 邮箱 APP 渲染不出来。

联想到用户反馈的 504 超时问题,大概有了答案。

编辑保存 504 错误,原因是客户直接将淘宝上的商品描述,复制粘贴到了我们的小程序后台,而淘宝的商品描述中包含大量的 HTML 样式代码,同时部分图片是 Base64 格式,造成后台数据库存储错误(数据超长),同时引起报警邮件无法发出,从而阻塞了请求,所以报了超时 504 错误。

临时的解决方案是

  • 将 TEXT 类型换成 LONGTEXT
  • 图片不要从淘宝直接拷贝,最好手动上传

MySQL 中 TEXT 与 LONGTEXT 的差异

TEXT

A TEXT column with a maximum length of 65,535 (216 − 1) characters. The effective maximum length is less if the value contains multibyte characters. Each TEXT value is stored using a 2-byte length prefix that indicates the number of bytes in the value.

用工具看了一下,从淘宝复制过来的商品描述,共计 24 万多,而 TEXT 只能存储 6.5 万个字符。所以,summernote 这种富文本字段,最好还是使用 LONGTEXT.

LONGTEXT

A TEXT column with a maximum length of 4,294,967,295 or 4GB (232 − 1) characters. The effective maximum length is less if the value contains multibyte characters. The effective maximum length of LONGTEXT columns also depends on the configured maximum packet size in the client/server protocol and available memory. Each LONGTEXT value is stored using a 4-byte length prefix that indicates the number of bytes in the value.

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式