我应该禁用 MySQL 严格模式吗?
我一直在一个启用了 MySQL 严格模式的网站上工作。有人在我们的日志表中记录了很长的用户代理字符串,不幸的是,该用户代理字符串超出了列的限制,从而引发了警告。数据根本没有插入。
为了避免这样的麻烦,我应该禁用 MySQL 严格模式还是应该自己想出一些办法(我正在使用 PHP)?
I have been working on a site that had MySQL strict mode enabled. One person had a long user agent string that was logged in our log table and unfortunately the user agent string exceeded the limit for the column and thus caused a warning. The data was not inserted at all.
To avoid such troubles, should I disable the MySQL strict mode or should I come up with something on my own (I'm using PHP)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在将数据插入数据库之前验证数据。如果字符串太大而无法放入表中,则可能是您的列太窄,或者数据无效。您需要决定是否在存储之前截断它,进行一些错误报告,或两者兼而有之。
不要不要关闭 DBMS 的安全功能,这是完全错误的做法。
Validate the data before inserting it into your database. If a string is too big to fit in your table, either your column is too narrow, or the data is invalid. You need to decide whether you truncate it before storing it, do some error reporting, or both.
Do not turn off the safety features of your DBMS, that's the completely wrong thing to do.
您是否愿意让您的数据默默地被截断(可能导致数据损坏),或者您至少想知道您有问题吗?
我建议启用严格模式并在 PHP 中对数据进行边界检查。您的 PHP 应用程序知道,或者至少应该知道如何处理太长的字符串。如果你关闭严格模式并将决定权留给 MySQL,那么 MySQL 将默默地截断你的字符串,最终你将得到一个充满垃圾的数据库。
更改和修复代码很容易,修复损坏的数据通常是不可能的。
如果禁用严格模式,您最终会遇到如下奇怪的问题:
Would you rather have your data silently truncated (possibly leading to broken data) or would you at least like to know that you have a problem?
I'd recommend leaving strict mode enabled and bounds checking your data in your PHP. Your PHP application knows, or at least should know, what to do with a string that's too long. If you turn off strict mode and leave that decision to MySQL then MySQL will silently truncate your strings and you will end up with a database full of garbage.
Changing and fixing code is easy, fixing broken data is often impossible.
If you disable strict mode, you'll end up with strange problems like this:
好吧,这取决于你在禁用严格模式的情况下尝试一下,看看事情是如何工作的,我从来没有遇到过禁用它的问题,但要小心。
但是,如果长用户代理是严格模式的唯一问题,并且您无法直观地阅读它们,那么我建议您对 UA 进行哈希处理或增加字段长度。
Well it's up to you try it with strict mode disabled and see how things work, I've never had an issue with it disabled but be careful.
However, if long user agents are the only issue with strict mode and you don't visually read them than I recommend just hashing your UA's or increasing the field length.
如果您需要这样做,查询失败,并且您没有知识或预算来修复它 - 那么是的。
如果有人问如何正确禁用严格模式,只需在 MySQL 配置中添加以下内容即可:
If you have a need to do so, failing queries, and you do not have knowledge or budget to fix it - then yes.
If anyone is asking this is how to disable strict mode properly, just by adding this in MySQL config: