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

以下是我的尝试:

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

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

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

额外的问题,是否可以压缩一个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 Workbench中更改全局变量值。当然,如果您在服务器上使用本地运行的Workbench(或通过SSH连接)

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

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

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

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

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

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

SET GLOBAL max_allowed_packet=1073741824;

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

max_allowed_packet=500M

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

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

我想有些人也想知道如何在你的电脑上找到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上得到了这个答案

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

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,直到它重新启动,就像前面有人说的那样。