How to disable MySQL strict mode on CodeIgniter
by bernt & torsten
I recently installed a CodeIgniter 3.x application and another PHP application on a new server, I did run into some issues immediately, the apps were broken, it connected to the database. Did not allow me to insert new data, gave me an error, took me some time to figure it out, the issue was that MySQL on the new server was set to strict mode.
MySQL has had a strict mode since 5.1, but in 5.7 it became the default.
For the CodeIgniter 3.x App, I fixed the issue in the code: edit your
database.php
config file, and set the key
stricton
with a value of
false
For my other PHP application, I did not have that choice. One option that I could do was to add into my code after selecting the database
mysql_connect($db_host,$db_user,$db_pass); mysql_select_db($db_name,$link);
the following code
mysql_query("SET SESSION sql_mode = ''");
This will remove the strict mode only when you execute the specific PHP script, you would need to add it in all places where you do mysql_connect.
Another way to disable strict mode globally on your server (maybe some variations between different servers) is as follows:
Your configuration file MySQL actually looks at five different places for configuration files, so you can make the change in several places. It’ll look in
/etc/my.cnf
,/etc/mysql/my.cnf
,SYSCONFDIR/my.cnf
,$MYSQL_HOME/my.cnf
,-
~/my.cnf
.
~/.my.cnf
is user-specific, and the third and fourth options rely on specifics from your environment.
On a default server, MySQL configuration will live in
/etc/mysql/my.cnf
so let’s put our changes there. SSH into your server and use Vim to edit that file.
Open the file my.cnf and scroll down the file a bit, you’ll find the [mysqld] section. We’re going to add a new key, sql_mode. On MySQL 5.7, the default values for this key out of the box are:
STRICT_TRANS_TABLES, ONLY_FULL_GROUP_BY, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION
The strict mode comes from STRICT_TRANS_TABLES
. So, let’s overwrite the sql_mode and set it to be the same as the default, but without strict mode.
[mysqld] sql_mode = ONLY_FULL_GROUP_BY, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Save the file, and restart MySQL. From the command line that would be
sudo /etc/init.d/mysql restart
That’s it!
Why It’s Important to Exercise When You’re Over 60
Many of us find ourselves in a pickle as the years pile up. Once reliable sidekicks, our...
A Poem: The Last Time
You never know when it will be,
The last time you ski down slopes of snow,
A Poem: Time Millionaire
When the morning wakes, still and clear,
No more alarms, no more rush....