开发者

Should I disable MySQL strict mode?

开发者 https://www.devze.com 2023-02-21 11:49 出处:网络
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 ta开发者_开发问答ble and unfortunately the user agent string exceed

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 ta开发者_开发问答ble 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)?


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.


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:

VARCHAR(4) storing more characters than four


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.


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:

sql_mode=NO_ENGINE_SUBSTITUTION
innodb_strict_mode=0
0

精彩评论

暂无评论...
验证码 换一张
取 消