I was working on a legacy project recently and needed to import some data from MySQL 5.5. All the queries in the code worked perfectly in MySQL 5.5, so I assumed an upgrade to 5.7 would be seamless. Not so.
First I got errors due to DateTime columns being populated with zeros during import, then when running this query:
1 | select * |
I got this:
1 | Expression #1 of SELECT list is not in GROUP BY |
It turned out that the only_full_group_by mode was made default in version 5.7.5., which breaks many of such naïve queries. In fact, see here for more info.
I could rewrite all queries to be 5.7 compatible (hat tip to George Fekete for the solution) and do something as atrocious as this:
1 | select extf.* from ( |
… but this would make the already complex refactoring much more difficult. It would be much better to temporarily disable these checks and force MySQL to act like it did in 5.6.
Permanently changing SQL mode
First, we find out which configuration file our MySQL installation prefers. For that, we need the binary’s location:
1 | $ which mysqld |
Then, we use this path to execute the lookup:
1 | $ /usr/sbin/mysqld --verbose --help | grep -A 1 "Default options" |
We can see that the first favored configuration file is one in the root of the etc folder. That file, however, did not exist on my system so I opted for the second one.
First, we find out the current sql mode:
1 | mysql -u homestead -psecret -e "select @@sql_mode" |
Then, we copy the current string this query produced and remove everything we don’t like. In my case, I needed to get rid of NO_ZERO_IN_DATE, NO_ZERO_DATE and of course ONLY_FULL_GROUP_BY. The newly formed string then looks like this:
1 | STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
We open the configuration file we decided on before (/etc/mysql/my.cnf) and add the following line into the [mysqld] section:
1 | [mysqld] |
Save, exit, and restart MySQL:
1 | sudo service mysql restart |
Voilà, the SQL mode is permanently changed and I can continue developing the legacy project until I need some additional strictness.