Disable ONLY_FULL_GROUP_BY

后端 未结 27 1301
既然无缘
既然无缘 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:01

    To whom is running a VPS/Server with cPanel/WHM, you can do the following to permanently disable ONLY_FULL_GROUP_BY

    You need root access (either on a VPS or a dedicated server)

    1. Enter WHM as root and run phpMyAdmin

    2. Click on Variables, look for sql_mode, click on 'Edit' and copy the entire line inside that textbox

    e.g. copy this:

    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
    
    1. Connect to you server via SFTP - SSH (root) and download the file /etc/my.cnf

    2. Open with a text editor my.cnf file on your local PC and paste into it (under [mysqld] section) the entire line you copied at step (2) but remove ONLY_FULL_GROUP_BY,

    e.g. paste this:

    # disabling ONLY_FULL_GROUP_BY
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    
    1. Save the my.cnf file and upload it back into /etc/

    2. Enter WHM and go to "WHM > Restart Services > SQL Server (MySQL)" and restart the service

    0 讨论(0)
  • 2020-11-22 01:02

    On:

    • Ubuntu 14.04
    • mysql Ver 14.14 Distrib 5.7.16, for Linux (x86_64) using EditLine wrapper

    Do:

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

    Copy and paste:

    [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
    

    To the bottom of the file

    $ sudo service mysql restart
    
    0 讨论(0)
  • 2020-11-22 01:05

    Give this a try:

    SET sql_mode = ''
    

    Community Note: As pointed out in the answers below, this actually clears all the SQL modes currently enabled. That may not necessarily be what you want.

    0 讨论(0)
  • 2020-11-22 01:07

    I'm using doctrine and I have added the driverOptions in my doctrine.local.php :

    return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host' => 'localhost',
                    'port' => '3306',
                    'user' => 'myusr',
                    'password' => 'mypwd',
                    'dbname' => 'mydb',
                    'driverOptions' => array(
                        PDO::MYSQL_ATTR_INIT_COMMAND => "SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''))"
                    ),
                ),
            ),
        ),
    ));
    

    In phpmyadmin the user needs SUPER activated in the privileges.

    0 讨论(0)
  • 2020-11-22 01:08
        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';
        mysql> set session 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> exit;
    
    0 讨论(0)
  • 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

    ''
    
    0 讨论(0)
提交回复
热议问题