MySQL:永久设置 sql_mode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20373532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:36:30  来源:igfitidea点击:

MySQL: Setting sql_mode permanently

mysqlsql-mode

提问by George Newton

Via the MySQL command line client, I am trying to set the global mysql_mode:

通过 MySQL 命令行客户端,我试图设置全局 mysql_mode:

SET GLOBAL sql_mode = TRADITIONAL;

This works for the current session, but after I restart the server, the sql_mode goes back to its default: '', an empty string.

这适用于当前会话,但在我重新启动服务器后,sql_mode 恢复为默认值:'',一个空字符串。

How can I permanently set sql_mode to TRADITIONAL?

如何将 sql_mode 永久设置为 TRADITIONAL?

If relevant, the MySQL is part of the WAMP package.

如果相关,MySQL 是 WAMP 包的一部分。

Thank you.

谢谢你。

采纳答案by fancyPants

Add this to your my.cnf file (or my.ini if you're using windows):

将此添加到您的 my.cnf 文件(或 my.ini,如果您使用的是 Windows):

sql_mode="TRADITIONAL"

and restart the server

并重启服务器

回答by Michael Currie

MySQL sql_mode "TRADITIONAL", a.k.a. "strict mode", is defined by the MySQL docsas:

MySQL sql_mode "TRADITIONAL",又名“严格模式”,由MySQL 文档定义为:

“give an error instead of a warning” when inserting an incorrect value into a column.

向列中插入不正确的值时,“给出错误而不是警告”。

Here's how to ensure that your sql_mode is set to "TRADITIONAL".

以下是如何确保您的 sql_mode 设置为"TRADITIONAL".

First, check your current setting:

首先,检查您当前的设置:

mysql
mysql> SELECT @@GLOBAL.sql_mode;
+-------------------+
| @@GLOBAL.sql_mode |
+-------------------+
|                   |
+-------------------+
1 row in set (0.00 sec)

This returned blank, the default, that's bad: your sql_mode is not set to "TRADITIONAL".

这返回了空白,默认值,这很糟糕:您的 sql_mode 未设置为“TRADITIONAL”。

So edit the configuration file:

所以编辑配置文件:

sudo vim /etc/mysql/my.cnf

Add this line in the section labelled [mysqld]: sql_mode="TRADITIONAL"(as fancyPants pointed out)

在标记为[mysqld]:的部分中添加这一行sql_mode="TRADITIONAL"(如fancyPants 指出的那样)

Then restart the server:

然后重启服务器:

sudo service mysql restart

Then check again:

然后再次检查:

mysql
mysql> SELECT @@GLOBAL.sql_mode;
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| @@GLOBAL.sql_mode                                                                                                                                    |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Success! You are golden now.

成功!你现在是金色的。