小编为大家分享一篇关于Mysql的max_allowed_packet设定,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  max_allowed_packet是Mysql中的一个设定参数,用于设定所接受的包的大小,根据情形不同,其缺省值可能是1M或者4M,比如是4M的情况下,这个值的大小即为:4 *1024*1024= 4194304

现象

  当日志中出现了诸如:“Package for query is too large (xxxxxxxx > 4194304). You can change this value on the server by setting the max_allowed_package variable”的提示的时候,错误的本身就清楚的提示了对应的方式。

确认max_allowed_package

  使用如下的方式可以确认当前设定值

  1. mysql> select @@max_allowed_packet;
  2. +----------------------+
  3. |  @@max_allowed_packet | 
  4. +----------------------+
  5. |  4194304 | 
  6. +----------------------+
  7. 1 row in set (0.00 sec)
  8. mysql>

  或者

  1. mysql> show variables like 'max_allowed_packet';
  2. +--------------------+---------+
  3. |  Variable_name |  Value | 
  4. +--------------------+---------+
  5. |  max_allowed_packet |  4194304 | 
  6. +--------------------+---------+
  7. 1 row in set (0.00 sec)
  8. mysql>

修改

  可以使用set命令来修改,但是只是临时修改,重启后即丢失。也可以直接修改过 mysql的设定文件并重启mysql服务,即可永久化保证设定。

  根据mysql的安装不同修改的文件也会有所不同,普通安装方式可能会修改my.cnf,这里使用mysql的官方镜像,其修改的文件应该为:/etc/mysql/mysql.conf.d/mysqld.cnf

  修改内容:向此文件中添加如下设定

  max_allowed_packet = 256M

修改前

  1. [mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. #log-error = /var/log/mysql/error.log
  6. # By default we only accept connections from localhost
  7. #bind-address = 127.0.0.1
  8. # Disabling symbolic-links is recommended to prevent assorted security risks
  9. symbolic-links=0

修改后

  1. [mysqld]
  2. pid-file = /var/run/mysqld/mysqld.pid
  3. socket = /var/run/mysqld/mysqld.sock
  4. datadir = /var/lib/mysql
  5. #log-error = /var/log/mysql/error.log
  6. # By default we only accept connections from localhost
  7. #bind-address = 127.0.0.1
  8. # Disabling symbolic-links is recommended to prevent assorted security risks
  9. symbolic-links=0
  10. max_allowed_packet = 256M

重启容器并确认

  1. mysql> show variables like '%max_allowed_pack%';
  2. +--------------------------+------------+
  3. |  Variable_name |  Value | 
  4. +--------------------------+------------+
  5. |  max_allowed_packet |  268435456 | 
  6. |  slave_max_allowed_packet |  1073741824 | 
  7. +--------------------------+------------+
  8. 2 rows in set (0.01 sec)
  9. mysql>

  所以已经看到已经成功设定为256M(268435456)了

  1. liumiaocn:~ liumiao$ echo "256*1024*1024" | bc
  2. 268435456
  3. liumiaocn:~ liumiao$

总结

  以上就是Mysql数据库的max_allowed_packet设定方法的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,