Disable ONLY_FULL_GROUP_BY

后端 未结 27 1313
既然无缘
既然无缘 2020-11-22 00:22

I accidentally enabled ONLY_FULL_GROUP_BY mode like this:

SET sql_mode = \'ONLY_FULL_GROUP_BY\';

How do I disable it?

27条回答
  •  抹茶落季
    2020-11-22 01:10

    Add or Remove modes to sql_mode

    MySQL 5.7.9 or later

    To add or remove a mode from sql_mode, you can use list_add and list_drop functions.

    To remove a mode from the current SESSION.sql_mode, you can use one of the following:

    SET SESSION sql_mode = sys.list_drop(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY');
    SET sql_mode = sys.list_drop(@@sql_mode, 'ONLY_FULL_GROUP_BY');
    SET @@sql_mode = sys.list_drop(@@sql_mode, 'ONLY_FULL_GROUP_BY');
    

    To remove a mode from the GLOBAL.sql_mode that persists for the current runtime operation, until the service is restarted.

    SET GLOBAL sql_mode = sys.list_drop(@@GLOBAL.sql_mode, 'ONLY_FULL_GROUP_BY');
    

    MySQL 5.7.8 or prior

    Since the sql_mode value is a CSV string of modes, you would need to ensure that the string does not contain residual commas, which can be accomplished by using TRIM(BOTH ',' FROM ...).

    To remove a mode from the sql_mode variable, you would want to use REPLACE() along with TRIM() to ensure any residual commas are removed.

    SET SESSION sql_mode = TRIM(BOTH ',' FROM REPLACE(@@SESSION.sql_mode, 'ONLY_FULL_GROUP_BY', ''));
    SET GLOBAL sql_mode = TRIM(BOTH ',' FROM REPLACE(@@GLOBAL.sql_mode, 'ONLY_FULL_GROUP_BY', ''));
    

    To add a mode to the sql_mode variable, you would want to use CONCAT_WS(',', ...), to ensure a comma is appended with the current modes and TRIM() to ensure any residual commas are removed.

    SET SESSION sql_mode = TRIM(BOTH ',' FROM CONCAT_WS(',', 'ONLY_FULL_GROUP_BY', @@SESSION.sql_mode));
    SET GLOBAL sql_mode = TRIM(BOTH ',' FROM CONCAT_WS(',', 'ONLY_FULL_GROUP_BY', @@GLOBAL.sql_mode));
    

    NOTE: Changing the GLOBAL variable does not propagate to the SESSION variable, until a new connection is established.

    The GLOBAL variable will persist until the running service is restarted.

    The SESSION variable will persist for the current connection, until the connection is closed and a new connection is established.


    Revert to GLOBAL.sql_mode

    Since SET sql_mode = 'ONLY_FULL_GROUP_BY'; was executed without the GLOBAL modifier, the change only affected the current SESSION state value, which also pertains to @@sql_mode. To remove it and revert to the global default on server restart value, you would want to use the value from @@GLOBAL.sql_mode. [sic]

    The current SESSION value is only valid for the current connection. Reconnecting to the server will revert the value back to the GLOBAL value.

    To revert the current session state value to the current global value, you can use one of the following:

    SET SESSION sql_mode = @@GLOBAL.sql_mode;
    SET @@sql_mode = @@GLOBAL.sql_mode;
    SET sql_mode = @@GLOBAL.sql_mode;
    

    Change SESSION.sql_mode value to ONLY_FULL_GROUP_BY

    SET sql_mode = 'ONLY_FULL_GROUP_BY';
    SELECT @@sql_mode, @@GLOBAL.sql_mode;
    +--------------------+----------------------------------------------+
    | @@sql_mode         | @@GLOBAL.sql_mode                            |
    +--------------------+----------------------------------------------+
    | ONLY_FULL_GROUP_BY | NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION |
    +--------------------+----------------------------------------------+
    

    Revert the SESSION.sql_mode value to the GLOBAL.sql_mode value

    SET sql_mode = @@GLOBAL.sql_mode;
    SELECT @@sql_mode, @@GLOBAL.sql_mode;
    +----------------------------------------------+----------------------------------------------+
    | @@sql_mode                                   | @@GLOBAL.sql_mode                            |
    +----------------------------------------------+----------------------------------------------+
    | NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION | NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION |
    +----------------------------------------------+----------------------------------------------+
    

    Server Restart Persistent sql_mode using the option file

    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). [sic]

    Please see your version of MySQL to determine the supported and default modes.

    MySQL >= 5.7.5, <= 5.7.6 default

    [mysqld]
        
    sql-mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
    

    Please see the Option File Syntax for more information.

    The syntax for specifying options in an option file is similar to command-line syntax. However, in an option file, you omit the leading two dashes from the option name and you specify only one option per line. For example, --quick and --host=localhost on the command line should be specified as quick and host=localhost on separate lines in an option file. To specify an option of the form --loose-opt_name in an option file, write it as loose-opt_name.

    The value optionally can be enclosed within single quotation marks or double quotation marks, which is useful if the value contains a # comment character.

    Default sql_mode values

    Since the MySQL documentation per-version values have been removed, I have added them here for your reference.

    • MySQL 8.0 [sic]
    • MySQL 5.7 [sic]
    • MySQL 5.6 [sic]

    MySQL >= 8.0.11 8.0.5 - 8.0.10 Skipped

    ONLY_FULL_GROUP_BY STRICT_TRANS_TABLES NO_ZERO_IN_DATE NO_ZERO_DATE ERROR_FOR_DIVISION_BY_ZERO NO_ENGINE_SUBSTITUTION
    

    MySQL >= 5.7.8, <= 8.0.4

    ONLY_FULL_GROUP_BY STRICT_TRANS_TABLES NO_ZERO_IN_DATE NO_ZERO_DATE ERROR_FOR_DIVISION_BY_ZERO NO_AUTO_CREATE_USER NO_ENGINE_SUBSTITUTION
    

    MySQL 5.7.7

    ONLY_FULL_GROUP_BY STRICT_TRANS_TABLES NO_AUTO_CREATE_USER NO_ENGINE_SUBSTITUTION
    

    MySQL >= 5.7.5, <= 5.7.6

    ONLY_FULL_GROUP_BY STRICT_TRANS_TABLES NO_ENGINE_SUBSTITUTION
    

    MySQL >= 5.6.6, <= 5.7.4

    NO_ENGINE_SUBSTITUTION
    

    MySQL <= 5.6.5

    ''
    

提交回复
热议问题