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.

  • 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 Oct 24 '15 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? – user3818623 Oct 25 '15 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 Oct 25 '15 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 Nov 18 '15 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 Nov 18 '15 at 19:20

11 Answers 11

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.

  • 4
    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 Jan 12 '16 at 9:13
  • Fair enough! Added a note to craftcms.com/docs/requirements – Brad Bell Jan 13 '16 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 May 12 '16 at 0:47
  • 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. dev.mysql.com/doc/refman/5.7/en/sql-mode.html – Routhinator Jul 5 '16 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 May 8 at 22:18

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.

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

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
  • 2
    Only solution that worked on my mac. Thanks. – Gaurav Gandhi May 4 '16 at 13:30

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.

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
    This fixed my problem with Laravel forge on DO with PHP 7 and MySQL 5.7.9. – Johannes Lamers Dec 9 '15 at 9:32

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

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

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.

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.

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.

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.

protected by Community Jun 22 '17 at 18:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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