我在我的MySQL数据库中的BLOB字段有一个问题-当上传大于大约1MB的文件时,我得到一个错误,大于max_allowed_packet是不允许的。

以下是我的尝试:

在MySQL查询浏览器中,我运行了一个像“max_allowed_packet”这样的显示变量,它给了我1048576。

然后我执行查询集全局max_allowed_packet=33554432,然后显示像'max_allowed_packet'这样的变量-它像预期的那样给我33554432。

但是当我重新启动MySQL服务器时,它神奇地回到1048576。我哪里做错了?

额外的问题,是否可以压缩一个BLOB字段?


更改my.ini或~/.my.cnf文件,在[mysqld]或[client]部分中加入一行:

max_allowed_packet=500M

然后重新启动MySQL服务,就完成了。

有关更多信息,请参阅文档。


此错误是因为您的数据包含大于设定值。

只需要写下max_allowed_packed=500M 或者你可以计算500*1024k,如果你想用它来代替500M。

现在重新启动MySQL。


max_allowed_packet变量可以通过运行查询全局设置。

但是,如果您没有在my.ini文件中更改它(如dragon112所建议的那样),则该值将在服务器重新启动时重置,即使您全局地设置了它。

更改每个人允许的最大数据包为1GB,直到服务器重新启动。

SET GLOBAL max_allowed_packet=1073741824;

我想有些人也想知道如何在你的电脑上找到my.ini文件。对于windows用户,我认为最好的方法如下:

Win+R(“运行”的快捷方式),输入服务。msc,输入 你可以找到一个像'MySQL56'这样的条目,右键单击它,选择属性 你可以看到“D:/Program Files/MySQL/MySQL Server 5.6/bin\mysqld”——defaults-file=“D:\ProgramData\MySQL\MySQL Server 5.6\my.ini”MySQL56

我从http://bugs.mysql.com/bug.php?id=68516上得到了这个答案


我的一个初级开发人员在为我修改这个问题时遇到了问题,所以我想我应该为linux用户扩展更详细的内容:

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


如果在执行备份时出现此错误,可以在my.cnf中设置max_allowed_packet,特别是mysqldump。

[mysqldump]
max_allowed_packet=512M

我一直在执行mysqldump时得到这个错误,我不明白,因为我在[mysqld]节下的my.cnf中设置了这个。一旦我发现我可以设置它为[mysqldump],我设置了值,我的备份完成没有问题。


如果你想上传大尺寸的图片或数据库中的数据。只需将数据类型更改为“BIG BLOB”。


对于那些运行wamp mysql服务器

Wamp托盘图标-> 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

向下滚动到最后,直到你找到

[mysqld]
port=3306
explicit_defaults_for_timestamp = TRUE

在中间添加packet_size行

[mysqld]
port=3306
max_allowed_packet = 16M
explicit_defaults_for_timestamp = TRUE

检查它是否适用于此查询

Select @@global.max_allowed_packet;

按照所有的指示,这就是我所做和工作的:

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)

因此,正如我们所看到的,max_allowed_packet已经从my.ini中更改了。

让我们离开会话并再次检查:

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)

结论,在SET GLOBAL max_allowed_packet=1073741824之后,服务器将拥有新的max_allowed_packet,直到它重新启动,就像前面有人说的那样。


许多受访者发现了这个问题,并给出了解决方案。

我只是想建议另一种解决方案,即从工具Mysql Workbench中更改全局变量值。当然,如果您在服务器上使用本地运行的Workbench(或通过SSH连接)

你只需要连接到你的实例,然后进入菜单:

Server -> Options File -> Networking -> max_allowed_wrapped . zip

您设置了所需的值,然后需要重新启动MySql服务。


对于任何在Amazon RDS服务上运行MySQL的人来说,这个更改是通过参数组完成的。您需要创建一个新的PG或使用一个现有的PG(除了默认的只读PG)。

您应该搜索max_allowed_packet参数,更改其值,然后点击保存。

回到您的MySQL实例,如果您创建了一个新的PG,您应该将PG附加到您的实例(您可能需要重新启动)。 如果您更改了已附加到实例的PG,则更改将在不重新引导的情况下应用到已附加该PG的所有实例。


设置全局max_allowed_packet=10000000000;


使用MySql Workbench设置允许的最大数据包大小并重新启动服务器


在MYSQL 5.7中,max_allowed_packet最大为1G。如果你想把它设置为4G,它会失败,没有错误和警告。