71

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.

6
  • 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 [email protected]?
    – Brad Bell
    Commented Oct 24, 2015 at 16:31
  • 1
    Thanks 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? Commented Oct 25, 2015 at 5:47
  • 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 (rpbouman.blogspot.com/2014/09/…) and (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
    Commented Oct 25, 2015 at 20:26
  • 1
    Hmm.....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
    Commented Nov 18, 2015 at 13:25
  • 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
    Commented Nov 18, 2015 at 19:20

13 Answers 13

99

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.

12
  • 5
    This 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
    Commented Jan 12, 2016 at 9:13
  • Fair enough! Added a note to craftcms.com/docs/requirements
    – Brad Bell
    Commented Jan 13, 2016 at 3:04
  • 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
    Commented May 12, 2016 at 0:47
  • 3
    "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. dev.mysql.com/doc/refman/5.7/en/sql-mode.html Commented Jul 5, 2016 at 0:22
  • 1
    @biko, if you're using initSQLs, it will execute whatever the value is on every request no matter the MySQL version.
    – Brad Bell
    Commented May 8, 2018 at 22:18
41

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.

7
  • 1
    Hi @Rob the latest version of Homestead v0.4.2 adds mcrypt back in.
    – Gareth
    Commented Mar 5, 2016 at 8:41
  • @Gareth Great! So is it just the MySQL settings that need to be changed now?
    – Rob
    Commented Mar 5, 2016 at 19:18
  • 1
    Yeah 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
    Commented Mar 5, 2016 at 23:42
  • @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
    Commented Mar 6, 2016 at 19:32
  • 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
    Commented Mar 7, 2016 at 6:22
19

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
1
  • 4
    Only solution that worked on my mac. Thanks. Commented May 4, 2016 at 13:30
11

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.

1
  • This one works. Commented Sep 5, 2018 at 9:13
8

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
1
  • 1
    This fixed my problem with Laravel forge on DO with PHP 7 and MySQL 5.7.9. Commented Dec 9, 2015 at 9:32
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

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

4

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.

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.

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.

2

If you're using MAMP, it will constantly complain about existence of a "global" my.cnf file.
So, you need to update MySQL config template file inside your MAMP application.

  1. Start MAMP
  2. Go File > Edit Template > MySQL (my.cnf) > "choose whatever version you're using"
  3. Dump these two lines in the end of the opened file:

[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

Cheers!

2

Just in case somebody has problems with 'initSQLs' on ddev: I had to remove 'NO_AUTO_CREATE_USER'.

<?php
/**
 * Database Configuration
 */

return array(
    '*' => array(
        'server' => 'db',
        'user' => 'db',
        'password' => 'db',
        'database' => 'db',
        'initSQLs' => array("SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';")
    ),

);
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.