python测试开发django-58.MySQL server has gone away错误的解决办法

python测试开发django-58.MySQL server has gone away错误的解决办法,第1张

概述前言 使用django执行sql相关 *** 作的时候,出现一个“MySQL server has gone away”错误,后来查了下是sql执行过程中,导入的文件较大时候,会出现这个异常。 检查了下sql语句,确实有插入图片,图片较大导致出现MySQL server has gone away。 该问题是max_allowed_packet配置的默认值设置太小,只需要相应调大该项的值之后再次导入便能成

前言

使用django执行sql相关 *** 作的时候,出现一个“MysqL server has gone away”错误,后来查了下是sql执行过程中,导入的文件较大时候,会出现这个异常。
检查了下SQL语句,确实有插入图片,图片较大导致出现MysqL server has gone away。
该问题是max_allowed_packet配置的默认值设置太小,只需要相应调大该项的值之后再次导入便能成功。
该项的作用是限制MysqL服务端接收到的包的大小,因此如果导入的文件过大则可能会超过该项设置的值从而导致导入不成功!
环境:
CentOS7.4
MysqL5.6

max_allowed_packet

首先连接数据库,查看max_allowed_packet的值。

以linux系统为例,输入MysqL -uroot -p进入MysqL,输入root用户密码进入MysqL

MysqL -uroot -p

[[email protected] ~]# MysqL -uroot -pEnter password: Welcome to the MysqL monitor.  Commands end with ; or \g.Your MysqL connection ID is 33700Server version: 5.6.42 MysqL Community Server (GPL)copyright (c) 2000,2018,Oracle and/or its affiliates. All rights reserved.Oracle is a registered Trademark of Oracle Corporation and/or itsaffiliates. Other names may be Trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MysqL>
查看 max_allowed_packet 的值

show global variables like ‘max_allowed_packet‘;

查看到max_allowed_packet值是4194304,也就是默认4M(410241024)

MysqL> show global variables like 'max_allowed_packet';+--------------------+---------+| Variable_name      | Value   |+--------------------+---------+| max_allowed_packet | 4194304 |+--------------------+---------+1 row in set (0.00 sec)MysqL>
修改max_allowed_packet 的值

默认情况下max_allowed_packet的大小只有4M,接下来将该值设置成100M(10241024100)

set global max_allowed_packet=104857600;

重新查看max_allowed_packet的大小

MysqL> set global max_allowed_packet=104857600;query OK,0 rows affected (0.00 sec)MysqL> show global variables like 'max_allowed_packet';+--------------------+-----------+| Variable_name      | Value     |+--------------------+-----------+| max_allowed_packet | 104857600 |+--------------------+-----------+1 row in set (0.00 sec)MysqL> exitBye

设置完成之后,exit退出数据库即可。

Navicat修改

如果你不会直接进数据库 *** 作,使用远程工具Navicat也是可以直接修改的

-- 修改max_allowed_packetset global max_allowed_packet=104857600;-- 查看show global variables like 'max_allowed_packet';

请注意通过在命令行中进行设置只对当前有效,重启MysqL服务之后则恢复默认值,但可以通过修改my.cnf配置文件来达到永久有效的目的

my.cnf配置

my.cnf配置文件一般在/etc/my.cnf或者/etc/MysqL/my.cnf目录,使用cat查看当前配置

[[email protected] ~]# cat /etc/my.cnf# For advice on how to change settings please see# http://dev.MysqL.com/doc/refman/5.6/en/server-configuration-defaults.HTML[MysqLd]## Remove leading # and set to the amount of RAM for the most important data# cache in MysqL. Start at 70% of total RAM for dedicated server,else 10%.# innodb_buffer_pool_size = 128M## Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin## Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed,experiment to find the optimal values.# join_buffer_size = 128M# sort_buffer_size = 2M# read_rnd_buffer_size = 2Mdatadir=/var/lib/MysqLsocket=/var/lib/MysqL/MysqL.sock# disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Recommended in standard MysqL setupsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_tableS [MysqLd_safe]log-error=/var/log/MysqLd.logpID-file=/var/run/MysqLd/MysqLd.pID[[email protected] ~]#

在可以在配置文件my.cnf中[MysqLd]添加max_allowed_packet=100M即可

总结

以上是内存溢出为你收集整理的python测试开发django-58.MySQL server has gone away错误的解决办法全部内容,希望文章能够帮你解决python测试开发django-58.MySQL server has gone away错误的解决办法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1191630.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存