[英]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 控制台),如下所示:
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.