简体   繁体   English

如何在 MySQL 8 的 my.cnf 中设置 sql_mode?

[英]How to set sql_mode in my.cnf in MySQL 8?

I'm running MySQL 8.0.11 community version.我正在运行 MySQL 8.0.11 社区版。 I need to set sql_mode to exclude ONLY_FULL_GROUP_BY in my.cnf so that it's restart safe.我需要在 my.cnf 中设置 sql_mode 以排除 ONLY_FULL_GROUP_BY 以便它重新启动安全。 I tried the following variants:我尝试了以下变体:

sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
sql-mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

They all fail in the same manner whether the variable is named 'sql_mode' or 'sql-mode':无论变量名为“sql_mode”还是“sql-mode”,它们都以相同的方式失败:

mysqld --verbose --help | grep "sql[-_]mode"
2018-06-19T15:22:51.667734Z 0 [ERROR] [MY-011071] [Server] /usr/sbin/mysqld: Error while setting value 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' to 'sql_mode'
    --sql-mode=name     Syntax: sql-mode=mode[,mode[,mode...]]. See the manual
2018-06-19T15:22:51.675498Z 0 [ERROR] [MY-010119] [Server] Aborting

sql-mode sql模式

It would seem that mysqld process my.cnf and converts 'sql_mode' or 'sql-mode' to 'sql_mode', which then it rejects!看起来mysqld 处理my.cnf 并将'sql_mode' 或'sql-mode' 转换为'sql_mode',然后它拒绝!

The question is how to get around this?问题是如何解决这个问题?

The SQL mode NO_AUTO_CREATE_USER was removed in MySQL 8.0, and it's no longer recognized. SQL 模式NO_AUTO_CREATE_USER在 MySQL 8.0 中被删除,并且不再被识别。

https://dev.mysql.com/doc/refman/8.0/en/mysql-nutshell.html#mysql-nutshell-deprecations says: https://dev.mysql.com/doc/refman/8.0/en/mysql-nutshell.html#mysql-nutshell-deprecations说:

The following features related to account management are removed:删除了以下与帐户管理相关的功能:

  • Using GRANT to create users.使用GRANT创建用户。 Instead, use CREATE USER .相反,使用CREATE USER Following this practice makes the NO_AUTO_CREATE_USER SQL mode immaterial for GRANT statements, so it too is removed.遵循这种做法会使NO_AUTO_CREATE_USER SQL 模式对于GRANT语句变得无关紧要,因此它也被删除了。

Change your sql_mode to "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION".将您的 sql_mode 更改为“STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION”。 I tested this on my sandbox instance of 8.0.11 and it worked.我在我的 8.0.11 沙箱实例上测试了这个,它工作正常。

Either spelling of sql-mode or sql_mode are both fine. sql-mode 或 sql_mode 的拼写都可以。

Using quotes or omitting quotes are both fine.使用引号或省略引号都可以。

Assuming that "restart safe" just means permanent, the syntax is:假设“重启安全”只是意味着永久,语法是:

sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

From Setting the SQL Mode :设置 SQL 模式

To set the SQL mode at server startup, use the --sql-mode="modes" option on the command line, or sql-mode="modes" in an option file such as my.cnf (Unix operating systems) or my.ini (Windows).要在服务器启动时设置 SQL 模式,请在命令行上使用 --sql-mode="modes" 选项,或在选项文件中使用sql-mode="modes" ,例如 my.cnf(Unix 操作系统)或 my .ini (Windows)。 modes is a list of different modes separated by commas. modes是由逗号分隔的不同模式的列表。

If it doesn't work for your, perhaps you're placing it under the wrong section.如果它对您不起作用,则可能是您将其放在错误的部分下。 For server settings that needs to be [mysqld] , as in:对于需要为[mysqld]服务器设置,如:

[mysqld]
sql-mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Step 1. Check sql mode:步骤 1. 检查 sql 模式:

mysql -u root -p -e "SELECT @@GLOBAL.sql_mode;"

Step 2. Create a new configuration file under the /etc/mysql/conf.d/ directory:第二步,在/etc/mysql/conf.d/目录下新建一个配置文件:

sudo nano /etc/mysql/conf.d/disable_strict_mode.cnf 

Enter the text below on the editor:在编辑器中输入以下文本:

[mysqld]

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

Step 3. Restart MySQL:步骤 3. 重启 MySQL:

sudo service mysql restart

Step 4. Confirm the change:步骤 4. 确认更改:

mysql -u root -p -e "SELECT @@GLOBAL.sql_mode;"

NO_AUTO_CREATE_USER SQL mode has been removed in MySQL 8.0, please check the reference manual for the full list of SQL modes . NO_AUTO_CREATE_USER SQL 模式已在 MySQL 8.0 中删除,请查看参考手册以获取SQL 模式完整列表

I've not found a way around the problem using my.cnf.我还没有找到使用 my.cnf 解决问题的方法。 To be mysqld restart safe, I need to avoid having to do:为了 mysqld 重启安全,我需要避免这样做:

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

The only way I found to get around this is to set an environment variable:我发现解决这个问题的唯一方法是设置一个环境变量:

sudo systemctl set-environment MYSQLD_OPTS="--sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
sudo systemctl restart mysqld

Better solutions welcomed.欢迎更好的解决方案。

In recent versions of Ubuntu/Debian, in my case I am modifying the file /lib/systemd/system/mysql.service with:在最近版本的 Ubuntu/Debian 中,就我而言,我正在修改文件/lib/systemd/system/mysql.service

ExecStart=/usr/sbin/mysqld --sql-mode=NO_ENGINE_SUBSTITUTION

After that, then only execute:之后,然后只执行:

systemctl daemon-reload
systemctl restart mysql

Changes to *.cnf files do nothing.*.cnf文件的更改没有任何作用。

示例执行

Editing my.cnf was not working with MySQL 8. So I developed a workaround.编辑my.cnf不适用于 MySQL 8。所以我开发了一个解决方法。 I am using MySQL 8 in Ubuntu 20.04我在 Ubuntu 20.04 中使用 MySQL 8

I created a file /etc/mysql/mysqlmode.sql :我创建了一个文件/etc/mysql/mysqlmode.sql

SET GLOBAL sql_mode = (SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''));

Next, I edited the /etc/mysql/my.cnf and added these lines of code at the end:接下来,我编辑了/etc/mysql/my.cnf并在最后添加了这些代码行:

[mysqld]
init-file="/etc/mysql/mysqlmode.sql"

As for MacOs Catalina, I use MysqlWorkbench to switch the "persist" checkbox off in "Server/Status and../ =>System Variables and search sql_mode" Without that action, it ignores my.cnf settings/至于 MacOs Catalina,我使用 MysqlWorkbench 关闭“服务器/状态和../ =>系统变量并搜索 sql_mode”中的“持久”复选框,如果没有该操作,它会忽略 my.cnf 设置/

that is mine: [mysqld] sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"那是我的:[mysqld] sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

I just find that the option is not compatible with 'NO_AUTO_CREATE_USER' in my.cnf.我只是发现该选项与 my.cnf 中的“NO_AUTO_CREATE_USER”不兼容。 That may be conflict with some setting.这可能与某些设置冲突。

The following line works for me in MySQL 8.以下行在 MySQL 8 中对我有用。

[mysqld] sql-mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION" [mysqld] sql-mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM