如何在本地主机(xampp)中打开/关闭 MySQL 严格模式?

我想知道如何检查在 localhost (xampp)中是否打开或关闭 MySQL 严格模式。

如果打开那么对于什么模式和如何关闭。

如果关了那么怎么开。

我已经关注了 http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-fullhttps://mariadb.com/kb/en/mariadb/sql_mode/以及其他相关网站。但我的问题没有得到确切的答案。

208593 次浏览

->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';

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=

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.

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

SELECT @@SQL_MODE, @@GLOBAL.SQL_MODE;

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

Check the value with

SELECT @@GLOBAL.sql_mode;

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

SET @@GLOBAL.sql_mode=''

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.

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

on server console:

$ mysql -u root -p -e "SET GLOBAL sql_mode = 'NO_ENGINE_SUBSTITUTION';"

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

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.

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.