为什么 MySQL 不识别任何 sql_mode(s)?

Hem*_*lin 7 mysql mysql-5.7

尝试使用终端从终端启动 MySQL 时,mysql -u <username> -p<password>出现以下错误:

mysql: [错误] 未知变量 'sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

在 MySQL 正在寻找 .cnf 文件(通过运行找到它们sudo /usr/sbin/mysqld --verbose --help | grep -A 1 "Default options")的三个可能的位置中,只/etc/mysql/my.cnf存在。其中只有一个目录,包括!includedir /etc/mysql/conf.d/. 在那里,/etc/mysql/conf.d/mysql.cnf有以下几行:

 [mysql]
 sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Run Code Online (Sandbox Code Playgroud)

四处寻找答案,找不到任何答案。一一尝试从模式列表中排除特定的“坏模式”,它们都无法识别。

只有完全注释掉这一行,我才能登录并启动 MySQL。

有谁知道问题可能是什么?


平台和操作系统方面:

  • 我使用的 MySQL 版本是5.7.17
  • 操作系统为 Lubuntu 16.04,4.7.3-generic,x86_64

Rol*_*DBA 10

你的问题很简单

你有以下

[mysql]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Run Code Online (Sandbox Code Playgroud)

sql_mode 不是 mysql 客户端的选项。这就是错误消息是unknown variable.

请注意如何sql_modesql-mode不出现在 mysql 客户端选项中:

$ mysql --help | grep mode
                      Automatically switch to vertical output mode if the
                      work in batch mode. Disable with --disable-pager. This
  --ssl-mode=name     SSL connection mode.
  --ssl               Deprecated. Use --ssl-mode instead.
                      Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead.
                      also. Does not work in batch mode. Disable with
  --binary-mode       By default, ASCII '\0' is disallowed and '\r\n' is
                      \C and DELIMITER, in non-interactive mode (for input
                      expired password sandbox mode.
binary-mode                       FALSE
Run Code Online (Sandbox Code Playgroud)

解决方案

sql_mode是 mysqld 的一个选项。

只需将组标题更改为 [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
Run Code Online (Sandbox Code Playgroud)

您不必重新启动 mysql。只需登录并运行

mysql> SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Run Code Online (Sandbox Code Playgroud)

试一试 !!!