How to turn on/off MySQL strict mode in localhost (xampp)?
Asked Answered
B

12

96

I want to know how to check whether MySQL strict mode is on or off in localhost(xampp).

If on then for what modes and how to off.

If off then how to on.

I already followed http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-full and https://mariadb.com/kb/en/mariadb/sql_mode/ and other related sites too. But I didn't get an exact answer for my question.

Blotchy answered 30/11, 2016 at 6:34 Comment(6)
if you want to check strict mode then use: SELECT @@sql_modeStrath
this query return your sql_mode and returns blank if it offStrath
check this like [shopplugin.net/kb/mysql-strict-mode-issues/] to turn it on or off.Strath
@Milan Soni: Tx for your response. The link u shared gives 'Sorry, that page doesn’t exist!'Blotchy
sorry it was giving you "]" at last in url that's why. Please remove last character and check again.Strath
@Milan Soni: Thanks for your timely response.But I have a concern that if we enable in the corresponding file and disable through sql query in the run time, then it obeys what it has done through run time. Hence I think changing the setting through sql should be followed. Again it also works for all the server. We don't need to worry about the file name and location for different server type. What do you say?Blotchy
B
143

->STRICT_TRANS_TABLES is responsible for setting MySQL strict mode.

->To check whether strict mode is enabled or not run the below sql:

SHOW VARIABLES LIKE 'sql_mode';

If one of the value is STRICT_TRANS_TABLES, then strict mode is enabled, else not. In my case it gave

+--------------+------------------------------------------+ 
|Variable_name |Value                                     |
+--------------+------------------------------------------+
|sql_mode      |STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION|
+--------------+------------------------------------------+

Hence strict mode is enabled in my case as one of the value is STRICT_TRANS_TABLES.

->To disable strict mode run the below sql:

set global sql_mode='';

[or any mode except STRICT_TRANS_TABLES. Ex: set global sql_mode='NO_ENGINE_SUBSTITUTION';]

->To again enable strict mode run the below sql:

set global sql_mode='STRICT_TRANS_TABLES';
Blotchy answered 30/11, 2016 at 13:19 Comment(6)
If you restart mysql server then settings change back again, is there a way to change this permanentlyTerpsichorean
You can do it in the server config file. Example how this would look like: [mysqld] sql_mode="STRICT_TRANS_TABLES" File location varies depending on your operating system, more on where to find it here: dev.mysql.com/doc/refman/5.7/en/option-files.htmlColas
My command set global sql_mode='' does not work (my version is 5.7), but set sql_mode='' does. Any explanation ?Matless
@Matless you may not have permissions?Strow
To disable some mode - SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));Brief
downvote, because it's a temporary solutionDulcie
T
71

To Change it permanently in ubuntu do the following

in the ubuntu command line

sudo nano /etc/mysql/my.cnf

Then add the following

[mysqld]
sql_mode=
Terpsichorean answered 1/5, 2017 at 11:13 Comment(2)
On 16.04 in /etc/mysql/mysql.conf.d/mysqld.cnfRolfston
printf "[mysqld]\nsql_mode=\n" | sudo tee /etc/mysql/conf.d/sql_mode.cnfLethbridge
S
20

First, check whether the strict mode is enabled or not in mysql using:

     SHOW VARIABLES LIKE 'sql_mode';

If you want to disable it:

     SET sql_mode = '';

or any other mode can be set except the following. To enable strict mode:

     SET sql_mode = 'STRICT_TRANS_TABLES';

You can check the result from the first mysql query.

Sisyphus answered 27/10, 2017 at 0:16 Comment(2)
Can you add it to a specific database or globally?Undersized
only for temporary use, after restart xampp it is reset to defaultDamicke
S
9

Check the value with

SELECT @@GLOBAL.sql_mode;

then clear the @@global.sql_mode by using this command:

SET @@GLOBAL.sql_mode=''
Schaumberger answered 22/1, 2019 at 16:2 Comment(1)
You don't remove STRICT_TRANS_TABLES but you clear sql_mode completely. That's a serious difference.Tew
F
5

To change it permanently in Windows (10), edit the my.ini file. To find the my.ini file, look at the path in the Windows server. E.g. for my MySQL 5.7 instance, the service is MYSQL57, and in this service's properties the Path to executable is:

"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqld.exe" --defaults-file="C:\ProgramData\MySQL\MySQL Server 5.7\my.ini" MySQL57

I.e. edit the my.ini file in C:\ProgramData\MySQL\MySQL Server 5.7\. Note that C:\ProgramData\ is a hidden folder in Windows (10). My file has the following lines of interest:

# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

Remove STRICT_TRANS_TABLES, from this sql-mode line, save the file and restart the MYSQL57 service. Verify the result by executing SHOW VARIABLES LIKE 'sql_mode'; in a (new) MySQL Command Line Client window.

(I found the other answers and documents on the web useful, but none of them seem to tell you where to find the my.ini file in Windows.)

Fuse answered 8/1, 2019 at 19:14 Comment(0)
C
4

In my case, I need to add:

sql_mode="STRICT_TRANS_TABLES"

under [mysqld] in the file my.ini located in C:\xampp\mysql\bin.

Complicacy answered 14/6, 2019 at 3:38 Comment(0)
B
3

I want to know how to check whether MySQL strict mode is on or off in localhost(xampp).

SHOW VARIABLES LIKE 'sql_mode';

If result has "STRICT_TRANS_TABLES", then it's ON. Otherwise, it's OFF.

If on then for what modes and how to off.

If off then how to on.

For Windows,

  1. Go to C:\Program Files\MariaDB XX.X\data
  2. Open the my.ini file.
  3. *On the line with "sql_mode", modify the value to turn strict mode ON/OFF.
  4. Save the file
  5. **Restart the MySQL service
  6. Run SHOW VARIABLES LIKE 'sql_mode' again to see if it worked;

*3.a. To turn it ON, add STRICT_TRANS_TABLES on that line like this: sql_mode=STRICT_TRANS_TABLES. *If there are other values already, add a comma after this then join with the rest of the value.

*3.b. To turn it OFF, simply remove STRICT_TRANS_TABLES from value. *Remove the additional comma too if there is one.

**6. To restart the MySQL service on your computer,

  1. Open the Run command window (press WINDOWS + R button).
  2. Type services.msc
  3. Click OK
  4. Right click on the Name MySQL
  5. Click Restart
Bohr answered 7/10, 2020 at 11:19 Comment(1)
Nicely written! And practical too.Zamindar
D
2

You can check the local and global value of it with:

SELECT @@SQL_MODE, @@GLOBAL.SQL_MODE;
Dextrorotation answered 5/10, 2018 at 11:54 Comment(0)
G
2

on server console:

$ mysql -u root -p -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';" 
Glyceride answered 10/9, 2020 at 2:15 Comment(0)
A
1

on Debian 10 I start mysql from ./opt/lampp/xampp start

I do strace ./opt/lampp/sbin/mysqld and see that my.cnf is there:

stat("/opt/lampp/etc/my.cnf", {st_mode=S_IFREG|0644, st_size=5050, ...}) = 0
openat(AT_FDCWD, "/opt/lampp/etc/my.cnf", O_RDONLY|O_CLOEXEC) = 3

hence, I add sql_mode config to /opt/lampp/etc/my.cnf instead of /etc/mysql/my.cnf

Albert answered 12/3, 2020 at 4:40 Comment(0)
M
0

For ubuntu :

  • Once you are connected to your VPS via SSH, please try connecting to your mysql with "root"

user: mysql -u root -p

  • Enter "root" user password and you will be in the mysql environment (mysql>), then simply check what is sql_mode, with the following command:

    SHOW VARIABLES LIKE 'sql_mode';

Basically, you will see the table as your result, if the table has a value of STRICT_TRANS_TABLES, it means that this option is enabled, so you need to remove the value from this table with the following command:

set global sql_mode='';

This will set your table's value to empty and disable this setting. Like this:

 +---------------+-------+
 | Variable_name | Value |
 +---------------+-------+
 | sql_mode      |       |
 +---------------+-------+

Please make sure to perform these commands within the MySQL environment and not simply via SSH. I think this moment was missed in the article provided below and the author assumes that the reader understands it intuitively.

Madrigal answered 25/11, 2020 at 5:14 Comment(0)
Z
0

Today I was trying to set the sql_mode=TRADITIONAL permanently but all efforts were in vain not because there are wrong answers but due to the way xampp configured the mysqld startup script. Let me explain in detail.

Of course you all try our best before coming to SO, so do I. I followed the comments in A:\xampp\mysql\bin\my.ini (given below):

# You can copy this file to
# A:/xampp/mysql/bin/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is A:/xampp/mysql/data) or
# ~/.my.cnf to set user-specific options.

So I tried A:/xampp/mysql/bin/my.cnf, A:/xampp/mysql/data/my.cnf but it wasn't even reading those files. Hours wasted in creating .cnf files in above locations. Worst part was it wasn't even working if I edit those my.ini files (i.e A:/xampp/mysql/bin/my.ini and A:/xampp/mysql/data/my.ini)

Then I checked all the folders to know how that control panel works and found thta xampp uses the mysql_start.bat script to start the msql deamon. Here is the bat file contents:

@echo off
cd /D %~dp0
echo Diese Eingabeforderung nicht waehrend des Running beenden
echo Please dont close Window while MySQL is running
echo MySQL is trying to start
echo Please wait  ...
echo MySQL is starting with mysql\bin\my.ini (console)

mysql\bin\mysqld --defaults-file=mysql\bin\my.ini --standalone

if errorlevel 1 goto error
goto finish

:error
echo.
echo MySQL konnte nicht gestartet werden
echo MySQL could not be started
pause

:finish

Here we can clearly see that it is explicitly using the argument --defaults-file to tell MySQL daemon from where to read the files. Now I hope you have plenty of ideas to fix this.

Note: I've already added A:/xampp/mysql/bin to my PATH.

Now we have several options as I've mentioned below:

  1. Add the exact path to the --defaults-file (i.e. --defaults-file=mysql\bin\my.cnf)
  2. You can just ommit the flag and let mysqld read from default locations (can see those using mysql --help) Now you've 2 options:
    • either edit those default my.ini files or
    • follow the comments to create my.cnf files according to your installation directory.

I just deleted that --defaults-file flag and let it run with MySQL's default configuration instead of xampp's. By the was I also have to change A:\xampp\mysql\data\my.ini from this:

[mysqld]
datadir=C:/xampp/mysql/data
[client]

to

[mysqld]
datadir=A:\xampp\mysql\data
[client]

to update the data directory. After that I just created a my.conf file in A:\xampp\mysql\data (data dir). with sql_mode option in it. It also worked with my.cnf in the A:\xampp\mysql\bin.

I have attached some screenshots for better understanding:

Updated data dir in A:\xampp\mysql\data\my.ini:

enter image description here

(option 1) Add my.cnf in A:\xampp\mysql\data:

enter image description here

(option 2) Add my.cnf in A:\xampp\mysql\bin:

enter image description here

You may found another solution too. Hope you are able to fix whatever issue you have regarding those config files.

Zug answered 22/6, 2021 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.