Getting this SQL Error: GROUP BY incompatible with sql_mode=only_full_group_by

46

33

I am getting the following error:

Internal Server Error CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #18 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'creighton_mma_craft.structureelements.root' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

I have mysql 5.7.9 installed. Logging into the Control Panel is working fine, but the front end displays this message.

During the installation, the screen was stuck at the very end with "Ooops…" message.

I am new to terminal commands and mysql configurations so the more specific the better.

Thanks.

user3818623

Posted 2015-10-24T15:31:01.140

Reputation: 367

There are going to be 2 issues that need investigating here. 1) Whatever the underlying error message is you receiving during installation and 2) Whatever the template code is on the front-end that generated that SQL error. Can you do a fresh install to replicate #1, then zip up your craft/storage/runtime/logs folder and send it in to support@buildwithcraft.com? – Brad Bell – 2015-10-24T16:31:06.997

1Thanks for recommending to look into the logs. The clue was the sql_mode. Apparently as of MYSQL 5.7.5, the value is no longer set to empty. There are new default values separated by commas, and the culprit seems to be 'ONLY_FULL_GROUP_BY'. What I needed to do was create a 'my.cnf' file in "/etc" directory. There I created the new value with "sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES". Only thing is, I am not sure security-wise if that is the correct setting. There are other values that can be added. Maybe knows the best settings for security purposes? – user3818623 – 2015-10-25T05:47:25.877

1

Ahh... that's probably worth adding as an answer all by itself. :) It look like MySQL 5.7.5 changed the GROUP BY behavior, though (http://rpbouman.blogspot.com/2014/09/mysql-575-group-by-respects-functional.html) and (https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html) and Craft needs to take that into account now, it definitely is a bug. I'll look into it.

– Brad Bell – 2015-10-25T20:26:04.167

Hmm, I'm seeing this as well. Unfortunate as Vagrant Homestead box has just updated it's MySQL version. Is this unlikely to be resolved before v3 (judging from the answer below that seems to be the case)? Not keen on messing with settings, so guess I'll revert to previous box for now. – Rob – 2015-11-18T19:20:51.593

1Hmm.....so just to get this straight...so everyone installing a current mysql version....will get a fatal error on craft v2? With Craft3 not being around for quite some time....i would at least try to get that fix a bit more public into the installer or do backport it? Sounds a bit scary to me? no? – nexflo – 2015-11-18T13:25:04.890

I think it's not good to let everyone know what exactly you are working on. one might take the advance of a security flaw and take over your installation. i am talking about the database name which basically leads a way.. – Manticore – 2017-01-04T01:54:19.530

Answers

59

MySQL 5.7.5+ changed the way GROUP BY behaved in order to be SQL99 compliant (where in previous versions it was not).

There is a good overview of the changes here.

Unfortunately, this affects some core queries that Craft uses when fetching elements.

The good news is that this has been fixed already in Craft 3 since we refactored the way that elements query grabs data and it no longer uses a GROUP BY.

The bad news is that it's going to be pretty impractical to backport that fix into Craft 2.

The workaround for Craft 2, if you're running MySQL 5.7.5+ is to edit your my.cnf file and remove the ONLY_FULL_GROUP_BY option from sql_mode. That will change GROUP BY behavior back to its pre-MySQL 5.7.5 behavior.

Update:

If, for whatever reason (primarily you're on shared hosting), you aren't able to change the sql_mode of your box at the environment level, as of the next Craft release (post 2.6.2945), there is a new initSQLs database config settings you can set in your craft/config/db.php. It will accept an array of SQL statements to be executed immediately after the database connection has been established. If you're running into this MySQL 5.7 error, you can do:

'initSQLs' => array("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';")

Just keep in mind this method will add at least one extra SQL query to every request.

Brad Bell

Posted 2015-10-24T15:31:01.140

Reputation: 53 205

4This bit me today and feels important enough to me that it should be mentioned in the main Craft install docs somewhere, especially as MySQL 5.7.5+ usage is on the up (I hit this with a box provisioned via Laravel Forge) – Tom Davies – 2016-01-12T09:13:51.783

Fair enough! Added a note to https://craftcms.com/docs/requirements

– Brad Bell – 2016-01-13T03:04:28.070

Another reference: https://mattstauffer.co/blog/how-to-disable-mysql-strict-mode-on-laravel-forge-ubuntu

– Luke Holder – 2016-04-28T03:24:48.703

Brad, you might be able to do a check for whether the server is running 5.7.5+ and add ANY_VALUE() to your columns if so? http://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_any-value

– adrienne – 2016-05-11T23:24:22.857

1@adrienne It gets messy because the query is so dynamic. Element types can set any custom select and order by values, so those would have to be wrapped in ANY_VALUE depending on what the GROUP BY specifies. – Brad Bell – 2016-05-12T00:47:29.863

2

"Impractical to backport" - Then don't. Best practice for Mysql 5.7 is to set the sqlmode in your DB drive on connect. Craft installations should not require setting this on the entire database instance in my.cnf, which is terrible practice. You should be implementing the one line change required to set this when you call the database connection.

http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html

– Routhinator – 2016-07-05T00:22:02.247

Thanks, the initSQLs allowed me to carry on working without having to mess about installing an older MySQL version or updating Craft to 3.x – Alex Holsgrove – 2017-01-19T09:40:01.093

None of the solutions suggested are working for me. Is there any new issues as to why? My site is https://mentiondesign.co.uk/

– mention79 – 2017-03-30T22:34:26.060

HEADS UP to Cloud SQL users - when you config the DB in the GCP console, the sql_mode flag is a drop-down menu, and only accepts one value. I found out from this blog page that TRADITIONAL may work as an alias for the strict values you want, and none of the strict values you don't want. More on TRADITIONAL on the MySQL 5.7 reference page.

– ingernet – 2017-06-22T18:02:09.800

33

Updated 05/03/16: I had a struggle getting this to work with Homestead 5.2. The MySQL error everyone else is seeing, as well as mcrypt error, as mycrypt is not installed by default in Vagrant box laravel/homestead 0.4.1 (Fixed in vagrant box laravel/homestead 0.4.2 — See answer history for 0.4.1 how-to).

So, steps to success:

  1. vagrant ssh into your box
  2. sudo vim /etc/mysql/my.cnf
  3. Scroll to the bottom of file and A to enter insert mode
  4. 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
  5. esc to exit input mode
  6. :wq to save and close vim.
  7. sudo service mysql restart to restart MySQL.
  8. Done!

Original how-to article.

Rob

Posted 2015-10-24T15:31:01.140

Reputation: 650

1Hi @Rob the latest version of Homestead v0.4.2 adds mcrypt back in. – Gareth – 2016-03-05T08:41:07.320

@Gareth Great! So is it just the MySQL settings that need to be changed now? – Rob – 2016-03-05T19:18:49.163

1Yeah that's exactly right. I did need to destroy my homestead box and install a new one. For some reason the update didn't work for me. So you just need to do from 4 onwards in your points once you have v0.4.2. – Gareth – 2016-03-05T23:42:14.533

@Gareth I updated and it seems to be working fine for me. But some of my previous updates seem to have carried over. It does seem to be a dark art getting the damn thing working sometimes! Thanks for the tip-off. – Rob – 2016-03-06T19:32:35.990

yeah I'm still not sure all the messing you have to do is worth it. I'm going to stick with it for a while though and see how it goes. – Gareth – 2016-03-07T06:22:07.343

Had this problem on digitalocean too. This fix works :) – darylknight – 2016-07-03T15:15:03.400

This works for me! :) – vijay – 2018-01-20T08:39:43.797

15

To fix this I have created a .my.cnf in my home-directory with the following content:

[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

Lars Bilke

Posted 2015-10-24T15:31:01.140

Reputation: 569

2Only solution that worked on my mac. Thanks. – Gaurav Gandhi – 2016-05-04T13:30:26.497

7

We have the same problem. MySQL version (Ver 14.14 Distrib 5.7.9, for osx10.11 (x86_64) using EditLine wrapper)

Had problems SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression and by adding the code at my.cnf file resolved it for me:

[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

Joniel Roy Rosales

Posted 2015-10-24T15:31:01.140

Reputation: 71

1This fixed my problem with Laravel forge on DO with PHP 7 and MySQL 5.7.9. – Johannes Lamers – 2015-12-09T09:32:37.030

6

Here are some fairly specific instructions if you happen to be hosted on a server configured through ServerPilot.

https://serverpilot.io/community/articles/how-to-disable-strict-mode-in-mysql-5-7.html

Philip Jones

Posted 2015-10-24T15:31:01.140

Reputation: 346

6

For UBUNTU 16.04 LTS

commands:

cd /etc/mysql

sudo nano my.cnf

add these two lines:

[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'

save file and reset your computer.

Juan Gabriel

Posted 2015-10-24T15:31:01.140

Reputation: 61

4

MySQL 5.7.5+ changed the way GROUP BY behaved in order to be SQL99 compliant (where in previous versions it was not). Either modify the existing mysql queries to be compatible to the new mysql version or make mysql behave as in previous versions , by executing the below queries on the mysql .

SET sql_mode = ''; // current client

SET GLOBAL sql_mode = ''; // for other clients like hibernate

Source: http://www.proactiveclass.com/tutorials/java/hibernate/sql-error-group-by-incompatible-with-sql-mode-only-full-group-by

user4891166

Posted 2015-10-24T15:31:01.140

Reputation: 41

3

I faced this problem when I upgraded MySQL to version >= 5.7.5 and I'v fixed it by following these steps:

  1. SSH into your server.
  2. Add sql-mode="" to your MySQL config:

    echo -e '[mysqld]\nsql-mode=""' | tee --append /etc/mysql/conf.d/craft.cnf > /dev/null
    
  3. Restart MySQL service:

    service mysql restart
    

Note: you might need to add sudo to those commands.

dralshehri

Posted 2015-10-24T15:31:01.140

Reputation: 410

2

Following on from Rob's answer, for anyone running into this issue on Laravel Homestead, we can use Bash to script the change. That way, the edit to the my.cnf file is done automatically for any future VMs created using vagrant up.

To do this, simply add the following to your ~/.homestead/after.sh file:

STR="sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
if ! grep -Fxq "$STR" /etc/mysql/my.cnf
then
    printf "\n# Craft 2 compatibility\n[mysqld]\n$STR" >> /etc/mysql/my.cnf
    service mysql restart
fi

The above code searches /etc/mysql/my.cnf for the line 'sql_mode=...', and if it cannot be found, it is automatically appended, together with the preceding [mysqld] to the end of the file. The mysql service is then restarted to enable the change.

newtonne

Posted 2015-10-24T15:31:01.140

Reputation: 21

2

The proper fixes have already been answered here; but I'd like to note that another fix is to use MariaDB instead of MySQL. It's a drop-in replacement for it, it's more performant for a number of metrics, and it's created by the original author of MySQL.

Give it a go; you won't regret it.

andrew.welch

Posted 2015-10-24T15:31:01.140

Reputation: 5 136

1

open terminal :

"sudo nano /etc/mysql/my.cnf"

Scroll to the bottom of file and A to enter insert mode 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

save file then restart mysql service by the below command :

"sudo service mysql restart" to restart MySQL.

Devdutt Sharma

Posted 2015-10-24T15:31:01.140

Reputation: 111