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;
Related
The following is my session I have the data located in the dir with owner/group of mysql
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.18 MySQL Community Server - GPL
Copyright (c) 2000, 2019, 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> set global local_infile = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> use SSC;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> load data local infile '/storage/temp/myest1.csv' into table SSC fields terminated by ',' lines terminated by '\n' ignore 1 lines;
ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql> show variables like "local_infile";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> load data local infile '/storage/temp/myest1.csv' into table SSC;
ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql> load data local infile 'myest1.csv' into table SSC;
ERROR 1148 (42000): The used command is not allowed with this MySQL version
mysql> SELECT ##global.secure_file_priv;
+---------------------------+
| ##global.secure_file_priv |
+---------------------------+
| /storage/temp/ |
+---------------------------+
1 row in set (0.00 sec)
If you're using the LOCAL option with LOAD DATA INFILE, then the secure_file_priv isn't used for the location of the file you load. The file is loaded by the client, so the path you use is not on the server, it's on the client. If you give a relative path, it's relative to the current working directory when you started the client.
However, both the server and the client must enable the --local-infile option.
You've set the option in the server, but you also need to set it in the client like this:
mysql --local-infile -u root -p
I think the error message you got is pretty unhelpful. I logged a bug about it:
Bug #94396 Error message too broad: The used command is not allowed with this MySQL version
Please vote up that bug report if you agree!
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;
I just installed MySQL 5.7 on Windows 10. I am intending to run bugzilla on this computer, so I'd like the settings from this bugzilla mysql settings page, which are
max_allowed_packet=16M
ft_min_word_len=2
When I run mysql --help from cmd, I get a line that says
Default options are read from the following files in the given order:
C:\WINDOWS\my.ini C:\WINDOWS\my.cnf C:\my.ini C:\my.cnf C:\Program Files\MySQL\MySQL Server 5.7\my.ini C:\Program Files\MySQL\MySQL Server 5.7\my.cnf
None of those files exist until C:\Program Files\MySQL\MySQL Server 5.7\my.ini, which is where I added the settings from above. HOWEVER, if I open mysql -u username -p from the same window, I get
mysql> SHOW VARIABLES LIKE 'max_allowed_packet';
+--------------------+---------+
| Variable_name | Value |
+--------------------+---------+
| max_allowed_packet | 4194304 |
+--------------------+---------+
1 row in set (0.00 sec)
mysql> SHOW VARIABLES LIKE 'ft_min_word_len';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| ft_min_word_len | 4 |
+-----------------+-------+
1 row in set (0.00 sec)
And if I do mysql --help again, I get a line that says
max-allowed-packet 16777216
So I'm really confused. Am I logging into different mysql installs or something? Current full mysql --help here.
Find the spot where your my.ini is first picked up. A typical spot will be in the basedir visible with
select ##basedir;
Do the same for ##datadir and write those values down in case you make changes and want to undo what you have done. Such as messing it up and your data is not pointed to upon startup. You log file will tell you if it is broken.
Note too my comments above about specifying the port number with command line tools with a -P (uppercase).
For the my.ini you will need to Open as Administrator to make changes there anyway. Then perform a server restart. (mysqladmin).
Depending on your actual mysql version, attempt to modify, say, the verbosity level. And continue until you isolate where the file you want is picked up for your variables you wish to load on startup. Note in my below it suggests I have two servers running. Which I do on ports 3306 and 3307 for 5.6 and 5.7.13, respectively.
[mysqld]
basedir=C:\\Program Files\\MySQL\\MySQL Server 5.7\\
datadir=C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Data\\
port=3307
log_error_verbosity=2
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER
When you first setup the server on Windows, it is highly likely that you don't even have a my.ini that is active. There is a default stub not Live. And the values are merely baked in. It took me a while to realize there was truly no file loading anything until I found the spot.
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
I use PHP to access MySQL in XAMPP. My question is where I can find the MySQL log file if there is a DB error.
Also, can I change the default location/name of that log file?
Thank you
///// Based on the coments //////
mysql> show variables like '%log_file%';
+---------------------------+------------------------------------+
| Variable_name | Value |
+---------------------------+------------------------------------+
| general_log_file | C:/xampp/mysql/data/mysql.log |
| innodb_log_file_size | 5242880 |
| innodb_log_files_in_group | 2 |
| slow_query_log_file | C:/xampp/mysql/data/mysql-slow.log |
+---------------------------+------------------------------------+
4 rows in set (0.00 sec)
If you do
SHOW VARIABLES LIKE '%log_file%';
it will show exactly where they're being written.
The accepted answer is a bit old, for MySQL 5.1+
you may use the queries:
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file = 'my_log.log';
First will enable loging (which may be off by default)
and the second select updates the preferred file (by default under C:/xampp/mysql/data/).
NOTE: On windows 8 you may have to run your SQL IDE as ADMINISTRATOR for this commands to get saved.
NOTE2: you can also set this in the config, go to path_to_xampp/mysql/ and edit my.ini
(copy from my-default.ini if it does not exists) and add the settings there:
[mysqld]
general_log = 'ON';
general_log_file = 'my_log.log';
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
It's a *.err file.
You will find it here : C:\xampp\mysql\data
To trace you error correctly, open it with Notepad++ for example and Start Mysql. You Should see the error at the end of the file.
You can also try looking at localhost/phpmyadmin/ and click on the Variables tab.
On mac, it's likely to be at:
/Applications/XAMPP/xamppfiles/var/mysql
If there are a lot of error files there, do ls -la to see which one is most recent and most likely.