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

以下是我的尝试:

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

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

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

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


当前回答

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

其他回答

设置全局max_allowed_packet=10000000000;

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

我的一个初级开发人员在为我修改这个问题时遇到了问题,所以我想我应该为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

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

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

对于那些运行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;