python测试开发django-58.MySQL server has gone away错误的解决
前言 使用django执行sql相关操作的时候,出现一个“MySQL server has gone away”错误,后来查了下是sql执行过程中,导入的文件较大时候,会出现这个异常。 max_allowed_packet首先连接数据库,查看max_allowed_packet的值。 以linux系统为例,输入mysql -uroot -p进入mysql,输入root用户密码进入mysql
[[email?protected] ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 33700 Server 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 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> 查看 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)
重新查看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> exit Bye 设置完成之后,exit退出数据库即可。 Navicat修改如果你不会直接进数据库操作,使用远程工具Navicat也是可以直接修改的 -- 修改max_allowed_packet set 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 = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [[email?protected] ~]# 在可以在配置文件my.cnf中[mysqld]添加max_allowed_packet=100M即可 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |