MySQL: Setting sql_mode permanently - mysql

Via the MySQL command line client, I am trying to set the global mysql_mode:
SET GLOBAL sql_mode = TRADITIONAL;
This works for the current session, but after I restart the server, the sql_mode goes back to its default: '', an empty string.
How can I permanently set sql_mode to TRADITIONAL?
If relevant, the MySQL is part of the WAMP package.
Thank you.

MySQL sql_mode "TRADITIONAL", a.k.a. "strict mode", is defined by the MySQL docs as:
“give an error instead of a warning” when inserting an incorrect value into a column.
Here's how to ensure that your sql_mode is set to "TRADITIONAL".
First, check your current setting:
mysql
mysql> SELECT ##GLOBAL.sql_mode;
+-------------------+
| ##GLOBAL.sql_mode |
+-------------------+
| |
+-------------------+
1 row in set (0.00 sec)
This returned blank, the default, that's bad: your sql_mode is not set to "TRADITIONAL".
So edit the configuration file:
sudo vim /etc/mysql/my.cnf
Add this line in the section labelled [mysqld]: sql_mode="TRADITIONAL" (as fancyPants pointed out)
Then restart the server:
sudo service mysql restart
Then check again:
mysql
mysql> SELECT ##GLOBAL.sql_mode;
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| ##GLOBAL.sql_mode |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Success! You are golden now.

Add this to your my.cnf file (or my.ini if you're using windows):
sql_mode="TRADITIONAL"
and restart the server

For mysql8 on windows:
# Set the SQL mode to strict
sql-mode="NO_ENGINE_SUBSTITUTION"
It's a dash, not an underscore: sql_mode / sql-mode

Related

How to fix 'SchemaUpdate - HHH000319: Could not get database metadata' because Packet for query is too large (5,526,600 > 65,535) [duplicate]

I am having a problem with BLOB fields in my MySQL database - when uploading files larger than approx 1MB I get an error Packets larger than max_allowed_packet are not allowed.
Here is what i've tried:
In MySQL Query Browser I ran a show variables like 'max_allowed_packet' which gave me 1048576.
Then I execute the query set global max_allowed_packet=33554432 followed by show variables like 'max_allowed_packet' - it gives me 33554432 as expected.
But when I restart the MySQL server it magically goes back to 1048576. What am I doing wrong here?
Bonus question, is it possible to compress a BLOB field?
Change in the my.ini or ~/.my.cnf file by including the single line under [mysqld] or [client] section in your file:
max_allowed_packet=500M
then restart the MySQL service and you are done.
See the documentation for further information.
The max_allowed_packet variable can be set globally by running a query.
However, if you do not change it in the my.ini file (as dragon112 suggested), the value will reset when the server restarts, even if you set it globally.
To change the max allowed packet for everyone to 1GB until the server restarts:
SET GLOBAL max_allowed_packet=1073741824;
One of my junior developers was having a problem modifying this for me so I thought I would expand this in greater detail for linux users:
open terminal
ssh root#YOURIP
enter root password
nano /etc/mysql/my.cnf (if command is not recognized do this first or try vi then repeat: yum install nano )
add the line: max_allowed_packet=256M (obviously adjust size for whatever you need) under the [MYSQLD] section. He made a mistake of putting it at the bottom of the file first so it did not work.
Control + O (save) then Enter (confirm) then Control + X (exit file)
service mysqld restart
You can check the change in the variables section on phpmyadmin
I think some would also want to know how to find the my.ini file on your PC. For windows users, I think the best way is as follows:
Win+R(shortcut for 'run'), type services.msc, Enter
You could find an entry like 'MySQL56', right click on it, select properties
You could see sth like "D:/Program Files/MySQL/MySQL Server 5.6/bin\mysqld" --defaults-file="D:\ProgramData\MySQL\MySQL Server 5.6\my.ini" MySQL56
I got this answer from http://bugs.mysql.com/bug.php?id=68516
Following all instructions, this is what I did and worked:
mysql> SELECT CONNECTION_ID();//This is my ID for this session.
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 20 |
+-----------------+
1 row in set (0.00 sec)
mysql> select #max_allowed_packet //Mysql do not found #max_allowed_packet
+---------------------+
| #max_allowed_packet |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet; //That is better... I have max_allowed_packet=32M inside my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 33554432 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> **SET GLOBAL max_allowed_packet=1073741824**; //Now I'm changing the value.
Query OK, 0 rows affected (0.00 sec)
mysql> select #max_allowed_packet; //Mysql not found #max_allowed_packet
+---------------------+
| #max_allowed_packet |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The new value. And I still have max_allowed_packet=32M inisde my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 1073741824 |
+-----------------------------+
1 row in set (0.00 sec)
So, as we can see, the max_allowed_packet has been changed outside from my.ini.
Lets leave the session and check again:
mysql> exit
Bye
C:\Windows\System32>mysql -uroot -pPassword
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.6.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CONNECTION_ID();//This is my ID for this session.
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 21 |
+-----------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The new value still here and And I still have max_allowed_packet=32M inisde my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 1073741824 |
+-----------------------------+
1 row in set (0.00 sec)
Now I will stop the server
2016-02-03 10:28:30 - Server is stopped
mysql> SELECT CONNECTION_ID();
ERROR 2013 (HY000): Lost connection to MySQL server during query
Now I will start the server
2016-02-03 10:31:54 - Server is running
C:\Windows\System32>mysql -uroot -pPassword
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 9 |
+-----------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The previous new value has gone. Now I see what I have inside my.ini again.
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 33554432 |
+-----------------------------+
1 row in set (0.00 sec)
Conclusion, after SET GLOBAL max_allowed_packet=1073741824, the server will have the new max_allowed_packet until it is restarted, as someone stated previously.
If getting this error while performing a backup, max_allowed_packet can be set in the my.cnf particularly for mysqldump.
[mysqldump]
max_allowed_packet=512M
I kept getting this error while performing a mysqldump and I did not understand because I had this set in my.cnf under the [mysqld] section. Once I figured out I could set it for [mysqldump] and I set the value, my backups completed without issue.
For those running wamp mysql server
Wamp tray Icon -> MySql -> my.ini
[wampmysqld]
port = 3306
socket = /tmp/mysql.sock
key_buffer_size = 16M
max_allowed_packet = 16M // --> changing this wont solve
sort_buffer_size = 512K
Scroll down to the end until u find
[mysqld]
port=3306
explicit_defaults_for_timestamp = TRUE
Add the line of packet_size in between
[mysqld]
port=3306
max_allowed_packet = 16M
explicit_defaults_for_timestamp = TRUE
Check whether it worked with this query
Select ##global.max_allowed_packet;
This error come because of your data contain larger then set value.
Just write down the max_allowed_packed=500M
or you can calculate that 500*1024k and use that instead of 500M if you want.
Now just restart the MySQL.
For anyone running MySQL on Amazon RDS service, this change is done via parameter groups. You need to create a new PG or use an existing one (other than the default, which is read-only).
You should search for the max_allowed_packet parameter, change its value, and then hit save.
Back in your MySQL instance, if you created a new PG, you should attach the PG to your instance (you may need a reboot).
If you changed a PG that was already attached to your instance, changes will be applied without reboot, to all your instances that have that PG attached.
Many of the answerers spotted the issue and already gave the solution.
I just want to suggest another solution, which is changing the Glogal variable value from within the tool Mysql Workbench. That is ofcourse IF you use Workbench running locally on server (or via SSH connection)
You just connect to your instance and go on menu:
Server -> Options File -> Networking -> max_allowed_packed
You set the desired value and then you need to restart MySql Service.
Set the max allowed packet size using MySql Workbench and restart the server
in MYSQL 5.7, max_allowed_packet is at most 1G. if you want to set it to 4G, it would failed without error and warning.
If you want upload big size image or data in database. Just change the data type to 'BIG BLOB'.
set global max_allowed_packet=10000000000;

Mysql restores ONLY_FULL_GROUP_BY after restart

I followed the 2nd answer of this SO question to disable the global option
ONLY_FULL_GROUP_BY.
mysql> SELECT ##sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ##sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)
mysql> SET sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0,00 sec)
mysql> SELECT ##sql_mode;
+------------------------------------------------------------------------------------------------------------------------+
| ##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 row in set (0,00 sec)
I just discovered that after a mysql service restart, this option is restored.
This is my environment
Server: Localhost via UNIX socket
MySQL 5.7.17-0ubuntu0.16.04.1
Ubuntu 16.04
I thinked it was in one of the config files, but I'm not able to find it. Can you point me where is it located or how to permanently disable it?
Found:
I must add a new line into /etc/mysql/mysql.conf.d/mysqld.cnf
At the end of the section [mysqld], I added
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Then restarted
sudo systemctl restart mysql
My settings now is preserved after restart.
You can find (or create) config file in the paths listed here - Using Option Files
In config file in [mysqld] section write options you need, for example:
[mysqld]
sql_mode=ONLY_FULL_GROUP_BY

How to change secure_file_priv on mac osX for MySQL 5.7.16 [duplicate]

I am trying to write the results of MySQL script to a text file using the following code in my script.
SELECT p.title, p.content, c.name FROM post p
LEFT JOIN category c ON p.category_id=c.id
INTO OUTFILE 'D:\MySql\mysqlTest.txt';
However, I am getting the following
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
How do I solve this?
Ubuntu 16.04 (EASY): Find out where you are allowed to write
mysql> SELECT ##GLOBAL.secure_file_priv;
+---------------------------+
| ##GLOBAL.secure_file_priv |
+---------------------------+
| /var/lib/mysql-files/ |
+---------------------------+
1 row in set (0.00 sec)
Then, just write there
mysql> SELECT * FROM train INTO OUTFILE '/var/lib/mysql-files/test.csv' FIELDS TERMINATED BY ',';
Query OK, 992931 rows affected (1.65 sec)
mysql>
Mac OSX: Mysql installed via MAMP
Find out where you are allowed to write
mysql> SELECT ##GLOBAL.secure_file_priv;
+---------------------------+
| ##GLOBAL.secure_file_priv |
+---------------------------+
| NULL |
+---------------------------+
1 row in set (0.00 sec)
NULL means you're screwed so you have to create the file "~/.my.cnf"
Enable read/write for MySQL installed via MAMP (on Mac):
open "MAMP" use spotlight
click "Stop Servers"
edit ~/.my.cnf (using vi or your favorite editor) and add the following lines:
$ vi ~/.my.cnf
[mysqld_safe]
[mysqld]
secure_file_priv="/Users/russian_spy/"
click "Start Servers" (in MAMP window)
Now check if it works:
a. start mysql (default MAMP user is root, password is also root)
$ /Applications/MAMP/Library/bin/mysql -u root -p
b. in mysql look at the white-listed paths
mysql> SELECT ##GLOBAL.secure_file_priv;
+---------------------------+
| ##GLOBAL.secure_file_priv |
+---------------------------+
| /Users/russian_spy/ |
+---------------------------+
1 row in set (0.00 sec)
c. Finally, test by exporting a table train into a CSV file
mysql> SELECT * FROM train INTO OUTFILE '/Users/russian_spy/test.csv' FIELDS TERMINATED BY ',';
Query OK, 992931 rows affected (1.65 sec)
mysql>
Edit the (/etc/my.cnf file for CentOS) or (my.ini file for Windows)
Add
secure-file-priv = ""
line at the end
Stop mysql service using systemctl stop mysqld
Restart it using systemctl start mysqld
It will now allow you to import and export the data.
Replace "\" to "/" in your file path.
Like this:
INTO OUTFILE 'D:/MySql/mysqlTest.txt';
You cannot export data as it is configured in mysql config files. Open my.cnf config file and check.
Quote from MySQL doc
This variable is used to limit the effect of data import and export operations, such as those performed by the LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE() function. These operations are permitted only to users who have the FILE privilege.
secure_file_priv may be set as follows:
If empty, the variable has no effect.
If set to the name of a directory, the server limits import and export
operations to work only with files in that directory. The directory
must exist; the server will not create it.
If set to NULL, the server disables import and export operations. This
value is permitted as of MySQL 5.7.6.
(An empty value is the default, or it can be explicitly specified in my.cnf as secure_file_priv="". A NULL value can be set with secure_file_priv=NULL.)
So, if you want to export data, then you need to comment this option and restart mysql server. Then you will be able to export.
Just create a file /etc/my.cnf with the following content
[mysqld]
secure_file_priv = ''
You can use this oneliner:
echo "[mysqld]\nsecure_file_priv\t\t= ''\n" | sudo tee /etc/my.cnf
And then restart mysql. If brew was used to install the mysql run the following command:
brew services restart mysql
FOR MAC OS, if installed via HOMEBREW:
Edit my.cnf
PATH: /usr/local/etc/my.cnf
COPY this:
# Default Homebrew MySQL server config
[mysqld]
# Only allow connections from localhost
bind-address = 0.0.0.0
secure-file-priv = ''
SAVE
Explanation:
Secure_file_prive = NULL -- Limit mysqld not allowed to import and export
Secure_file_priv = '/tmp/' -- Limit mysqld import and export can only occur in /tmp/ directory
Secure_file_priv = '' -- does not restrict the import of mysqld
This is the example of my SQL that worked perfectly in WAMP SERVER (windows):
SELECT display_name,user_email,user_registered FROM test_wp_users
ORDER BY user_registered ASC
INTO OUTFILE 'C:/wamp64/tmp/usersbyemail.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
PS: Note that bars in path should be left-to-right to work perfectly in MYSQL.
In my my.ini I only had
# Secure File Priv.
and I tried to put:
# Secure File Priv.
secure-file-priv = ""
and
# Secure File Priv.
secure-file-priv = NULL
without making it work.
I finally deleted the line and left alone:
secure-file-priv = ""
Working correctly.
That's because secure_file_priv is set to NULL
mysql> show variables like secure_file_priv;
| secure_file_priv | NULL |
You must stop mysql server
shell>/usr/local/mysql/support-files/mysql.server stop
Then restart mysql with the option defining where you want your files to be written to, for example to /tmp
shell>/usr/local/mysql/support-files/mysql.server start --secure-file-priv=/tmp
Then in mysql terminal you should see where your files can now be written to
mysql> show variables like secure_file_priv;
| secure_file_priv | /private/tmp/ |
On Mac you can find this folder by using Go To Folder /private/tmp in Finder
Go to C:\ProgramData\MySQL\MySQL Server 8.0 and you will find my.ini file. Open my.ini file in text editor (recommended notepad++) and then search for secure_file_priv and then replace will secure_file_priv="" and then save the file.
and restart your laptop.

Enabling mysql Event scheduler on server restarts

I am running phpmyadmin and installed apache server on my personal computer. My problem is that I am trying to set MySQL event_scheduler to always be enabled even when the server restarts. I was reading that by setting the following command line in the server configuration file (my.cnf or my.ini) it should work: event_scheduler=DISABLED. However, where do I locate this my.cnf or my.ini file, and also should the command line be event_scheduler=DISABLED or event_scheduler=ENABLED seeing that I want it to always be enabled?
You should set 'ON' value (not ENABLED).
In the configuration file in [mysqld] section specify 'event-scheduler' option (not event_scheduler).
Also, you can start your MySQL server with '--event-scheduler' option, e.g. -
shell> mysqld --event-scheduler=ON
More information - event_scheduler system variable.
Add to my.cnf file to [mysqld] section.
GLOBAL event_scheduler=ON
Restart your mysql server. Check status with this command :
mysql> select ##GLOBAL.event_scheduler;
+--------------------------+
| ##GLOBAL.event_scheduler |
+--------------------------+
| ON |
+--------------------------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'event_scheduler';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| event_scheduler | ON |
+-----------------+-------+
1 row in set (0.01 sec)
Here the path for my.ini on XAMPP:
xampp\mysql\bin\my.ini
Open my.ini and add the following
[mysqld]
event_scheduler=ON
then restart MySQL service.
To check the status use the below MySQL query:
SELECT ##event_scheduler
For WAMP:
Edit your my.ini file and under the [mysqld] section, add this:
event-scheduler=on
restart all services
verify by running this query:
select ##event_scheduler;
To get to your my.ini file, just click the WAMP tray icon and hover over the 'MySQL' menu, and click 'my.ini'.
FOR MAMP on OS X:
the default installation does not include a my.cnf file so you need to create one and set up your default configuration. Therefore, to enable the the scheduler on an OS X MAMP stack, you need to,
stop your MySQL server
create a my.cnf file in your /Applications/MAMP/conf/ folder, you will need root access to do this (open a terminal window),
sudo touch /Applications/MAMP/conf/my.cnf
sudo nano /Applications/MAMP/conf/my.cnf
the 2nd cmd will open the nano editor, paste the following in your file,
[mysqld]
event_scheduler=ON
Save (^O) and exit (^X) the nano editor
restart your MySQL server.

How to change max_allowed_packet size

I am having a problem with BLOB fields in my MySQL database - when uploading files larger than approx 1MB I get an error Packets larger than max_allowed_packet are not allowed.
Here is what i've tried:
In MySQL Query Browser I ran a show variables like 'max_allowed_packet' which gave me 1048576.
Then I execute the query set global max_allowed_packet=33554432 followed by show variables like 'max_allowed_packet' - it gives me 33554432 as expected.
But when I restart the MySQL server it magically goes back to 1048576. What am I doing wrong here?
Bonus question, is it possible to compress a BLOB field?
Change in the my.ini or ~/.my.cnf file by including the single line under [mysqld] or [client] section in your file:
max_allowed_packet=500M
then restart the MySQL service and you are done.
See the documentation for further information.
The max_allowed_packet variable can be set globally by running a query.
However, if you do not change it in the my.ini file (as dragon112 suggested), the value will reset when the server restarts, even if you set it globally.
To change the max allowed packet for everyone to 1GB until the server restarts:
SET GLOBAL max_allowed_packet=1073741824;
One of my junior developers was having a problem modifying this for me so I thought I would expand this in greater detail for linux users:
open terminal
ssh root#YOURIP
enter root password
nano /etc/mysql/my.cnf (if command is not recognized do this first or try vi then repeat: yum install nano )
add the line: max_allowed_packet=256M (obviously adjust size for whatever you need) under the [MYSQLD] section. He made a mistake of putting it at the bottom of the file first so it did not work.
Control + O (save) then Enter (confirm) then Control + X (exit file)
service mysqld restart
You can check the change in the variables section on phpmyadmin
I think some would also want to know how to find the my.ini file on your PC. For windows users, I think the best way is as follows:
Win+R(shortcut for 'run'), type services.msc, Enter
You could find an entry like 'MySQL56', right click on it, select properties
You could see sth like "D:/Program Files/MySQL/MySQL Server 5.6/bin\mysqld" --defaults-file="D:\ProgramData\MySQL\MySQL Server 5.6\my.ini" MySQL56
I got this answer from http://bugs.mysql.com/bug.php?id=68516
Following all instructions, this is what I did and worked:
mysql> SELECT CONNECTION_ID();//This is my ID for this session.
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 20 |
+-----------------+
1 row in set (0.00 sec)
mysql> select #max_allowed_packet //Mysql do not found #max_allowed_packet
+---------------------+
| #max_allowed_packet |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet; //That is better... I have max_allowed_packet=32M inside my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 33554432 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> **SET GLOBAL max_allowed_packet=1073741824**; //Now I'm changing the value.
Query OK, 0 rows affected (0.00 sec)
mysql> select #max_allowed_packet; //Mysql not found #max_allowed_packet
+---------------------+
| #max_allowed_packet |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The new value. And I still have max_allowed_packet=32M inisde my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 1073741824 |
+-----------------------------+
1 row in set (0.00 sec)
So, as we can see, the max_allowed_packet has been changed outside from my.ini.
Lets leave the session and check again:
mysql> exit
Bye
C:\Windows\System32>mysql -uroot -pPassword
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.6.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CONNECTION_ID();//This is my ID for this session.
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 21 |
+-----------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The new value still here and And I still have max_allowed_packet=32M inisde my.ini
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 1073741824 |
+-----------------------------+
1 row in set (0.00 sec)
Now I will stop the server
2016-02-03 10:28:30 - Server is stopped
mysql> SELECT CONNECTION_ID();
ERROR 2013 (HY000): Lost connection to MySQL server during query
Now I will start the server
2016-02-03 10:31:54 - Server is running
C:\Windows\System32>mysql -uroot -pPassword
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 9 |
+-----------------+
1 row in set (0.00 sec)
mysql> Select ##global.max_allowed_packet;//The previous new value has gone. Now I see what I have inside my.ini again.
+-----------------------------+
| ##global.max_allowed_packet |
+-----------------------------+
| 33554432 |
+-----------------------------+
1 row in set (0.00 sec)
Conclusion, after SET GLOBAL max_allowed_packet=1073741824, the server will have the new max_allowed_packet until it is restarted, as someone stated previously.
If getting this error while performing a backup, max_allowed_packet can be set in the my.cnf particularly for mysqldump.
[mysqldump]
max_allowed_packet=512M
I kept getting this error while performing a mysqldump and I did not understand because I had this set in my.cnf under the [mysqld] section. Once I figured out I could set it for [mysqldump] and I set the value, my backups completed without issue.
For those running wamp mysql server
Wamp tray Icon -> MySql -> my.ini
[wampmysqld]
port = 3306
socket = /tmp/mysql.sock
key_buffer_size = 16M
max_allowed_packet = 16M // --> changing this wont solve
sort_buffer_size = 512K
Scroll down to the end until u find
[mysqld]
port=3306
explicit_defaults_for_timestamp = TRUE
Add the line of packet_size in between
[mysqld]
port=3306
max_allowed_packet = 16M
explicit_defaults_for_timestamp = TRUE
Check whether it worked with this query
Select ##global.max_allowed_packet;
This error come because of your data contain larger then set value.
Just write down the max_allowed_packed=500M
or you can calculate that 500*1024k and use that instead of 500M if you want.
Now just restart the MySQL.
For anyone running MySQL on Amazon RDS service, this change is done via parameter groups. You need to create a new PG or use an existing one (other than the default, which is read-only).
You should search for the max_allowed_packet parameter, change its value, and then hit save.
Back in your MySQL instance, if you created a new PG, you should attach the PG to your instance (you may need a reboot).
If you changed a PG that was already attached to your instance, changes will be applied without reboot, to all your instances that have that PG attached.
Many of the answerers spotted the issue and already gave the solution.
I just want to suggest another solution, which is changing the Glogal variable value from within the tool Mysql Workbench. That is ofcourse IF you use Workbench running locally on server (or via SSH connection)
You just connect to your instance and go on menu:
Server -> Options File -> Networking -> max_allowed_packed
You set the desired value and then you need to restart MySql Service.
Set the max allowed packet size using MySql Workbench and restart the server
in MYSQL 5.7, max_allowed_packet is at most 1G. if you want to set it to 4G, it would failed without error and warning.
If you want upload big size image or data in database. Just change the data type to 'BIG BLOB'.
set global max_allowed_packet=10000000000;