简体   繁体   English

如何在 MySQL 中启用严格的 sql_mode?

[英]How can I enable strict sql_mode in MySQL?

How can I enable strict sql_mode in MySQL?如何在 MySQL 中启用严格的sql_mode

I want to fetch data from SQL and process the same in strict mode.我想从 SQL 中获取数据并在strict模式下处理相同的数据。

My current sql_mode is:我目前的sql_mode是:

mysql> SELECT @@sql_mode;
+------------------------+
| @@sql_mode             |
+------------------------+
| NO_ENGINE_SUBSTITUTION |
+------------------------+

执行以下操作:

SET GLOBAL sql_mode='STRICT_TRANS_TABLES';

You basically have two ways of doing it, using SQL command or changing configuration file.您基本上有两种方法,使用 SQL 命令或更改配置文件。 If you set it using SQL command - it will change back after the server is restarted.如果您使用 SQL 命令设置它 - 它会在服务器重新启动后变回。

Doing it in SQL:在 SQL 中执行此操作:

SET GLOBAL sql_mode='STRICT_TRANS_TABLES';

Doing it in config file:在配置文件中执行:

[mysqld] sql_mode="STRICT_TRANS_TABLES"

File location varies depending on your operating system, more on where to find it here: https://dev.mysql.com/doc/refman/5.7/en/option-files.html文件位置因您的操作系统而异,更多关于在此处找到它的位置: https : //dev.mysql.com/doc/refman/5.7/en/option-files.html

Important to note, that you can have multiple modes specified:需要注意的是,您可以指定多种模式:

sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"

this is especially important when using SQL statement, since it could override your whole mode string.这在使用 SQL 语句时尤其重要,因为它可以覆盖整个模式字符串。

More stuff about SQL modes here: https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html更多关于 SQL 模式的内容: https : //dev.mysql.com/doc/refman/5.7/en/sql-mode.html

The other answers are correct, but they don't work (as-is) for AWS RDS.其他答案是正确的,但它们不适用于 AWS RDS(按原样)。

If you are running a MySQL server on AWS RDS, then you can't run SET GLOBAL sql_mode='STRICT_TRANS_TABLES';如果您在 AWS RDS 上运行 MySQL 服务器,则无法运行SET GLOBAL sql_mode='STRICT_TRANS_TABLES'; straightaway because you don't have the requisite permissions, even with admin-level credentials:直接,因为您没有必要的权限,即使有管理员级别的凭据:

mysql> SET GLOBAL sql_mode='STRICT_ALL_TABLES';
ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER or SYSTEM_VARIABLES_ADMIN privilege(s) for this operation

In AWS RDS, since it's a managed DB service, you don't have access to the my.cnf or other configuration files directly - so you can't change the settings there either.在 AWS RDS 中,由于它是一项托管数据库服务,因此您无法直接访问my.cnf或其他配置文件 - 因此您也无法更改那里的设置。

However, note that you can set sql_mode at the session-level, but this will be lost across session changes or reboots:但是,请注意,您可以在会话级别设置sql_mode ,但这将在会话更改或重新启动时丢失:

mysql> SET SESSION  sql_mode='STRICT_ALL_TABLES';
Query OK, 0 rows affected, 1 warning (0.30 sec)

mysql> SELECT @@session.sql_mode;
+---------------------+
| @@session.sql_mode  |
+---------------------+
| STRICT_ALL_TABLES   |
+---------------------+
1 row in set (0.31 sec)

So then how do you change sql_mode (or any other parameters for that matter) at a GLOBAL level so that they persist across restarts in AWS RDS MySQL?那么,您如何在全局级别更改sql_mode (或与此相关的任何其他参数),以便它们在 AWS RDS MySQL 中重新启动后仍然存在?

You need to create a custom DB Parameter Group in RDS (for example, using the web console) like this:您需要在 RDS 中创建自定义数据库Parameter Group (例如,使用 Web 控制台),如下所示:

AWS_RDS_CustomParameterGroup

Then you have to modify your RDS instance and apply the newly-created Parameter Group like so:然后您必须modify您的 RDS 实例并应用新创建的参数组,如下所示:

在此处输入图片说明

Finally, apply your modifications, and reboot (yes, reboot is required) the instance.最后,应用您的修改,并重启(是的,需要重启)实例。

And voila, you have your sql_mode set as needed, and it persists across reboots now:瞧,您已经根据需要设置了sql_mode ,现在它在重新启动后仍然存在:

mysql> SELECT @@sql_mode;
+------------------------------------------+
| @@sql_mode                               |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.69 sec)


mysql> SELECT @@global.sql_mode;
+------------------------------------------+
| @@global.sql_mode                        |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.62 sec)


mysql> SELECT @@session.sql_mode;
+------------------------------------------+
| @@session.sql_mode                       |
+------------------------------------------+
| STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION |
+------------------------------------------+
1 row in set (0.38 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滑轨。 MySQL的。 变量“ sql_mode”不能设置为“ STRICT_ALL_TABLES”的值 - Rails. MySQL. Variable 'sql_mode' can't be set to the value of 'STRICT_ALL_TABLES' 当在MySql中设置sql_mode = only_full_group_by时,如何以严格的字符串比较方式“分组”? - How to “group by” in strict string comparation when sql_mode=only_full_group_by is setted in MySql? 如何查看sql_mode的具体值? - How can I see the specific value of the sql_mode? com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:无法将变量“ sql_mode”设置为“ NO_STRICT_TRANS_TABLES”的值 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Variable 'sql_mode' can't be set to the value of 'NO_STRICT_TRANS_TABLES' MySQL sql_mode永久性地关闭严格模式Linux for WHMCS - MySQL sql_mode Turning Strict Mode Off Permanently Linux for WHMCS 如何在mysql中检查单个进程的SQL_MODE? - How to check the SQL_MODE in mysql for a single process? 如何在 MySQL 8 的 my.cnf 中设置 sql_mode? - How to set sql_mode in my.cnf in MySQL 8? 如何从 mysql 的 sql_mode 中删除 NO_ZERO_DATE? - How to remove NO_ZERO_DATE from sql_mode in mysql? SpringBoot程序中如何为mysql动态设置sql_mode? - How to dynamic setting sql_mode for mysql in SpringBoot program? 如何在phpMyAdmin中将sql_mode设置为non_strict? - How do you set sql_mode to non_strict in phpMyAdmin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM