test development python django-58.MySQL server has gone away error solution

Foreword

Use the time to perform sql django-related operations, a "MySQL server has gone away" error, then the next check is the process of sql execution, import larger files when there will be the exception.
Examined under the sql statement, indeed insert a picture, the larger picture cause MySQL server has gone away appear.
The problem is the default configuration settings max_allowed_packet is too small, will be able to successfully import again only after the need for large values adjusted accordingly.
The effect is to limit the mysql server receives the packet size, so if the imported file is too large may exceed the value of the resulting set of import is unsuccessful!
Environment:
CentOS7.4
mysql5.6

max_allowed_packet

First connect to the database, view the value of the max_allowed_packet.

To linux system, for example, enter into the mysql -uroot -p mysql, mysql enter the root user password to enter

mysql -uroot -p

[root@yoyo ~]# 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> 

Check the value of max_allowed_packet

show global variables like 'max_allowed_packet';

View the max_allowed_packet value is 4194304, which is the default 4M (4 1024 1024)

mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 4194304 |
+--------------------+---------+
1 row in set (0.00 sec)

mysql> 

Modify the value of max_allowed_packet

Max_allowed_packet size only 4M, the next value is set to 100M (1024 by default 1024 100)

set global max_allowed_packet=104857600;

Re-view the size of 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

After the setup is complete, exit out of the database can be.

Navicat modification

If you do not operate directly into the database, Navicat also use Remote Tools can be directly modified

-- 修改max_allowed_packet
set global max_allowed_packet=104857600;

-- 查看
show global variables like 'max_allowed_packet';

Please note that by setting only valid for the current command line, and then restart the mysql service to restore the default values, but can be achieved by modifying the purpose of permanent my.cnf configuration file

my.cnf Configuration

my.cnf configuration file usually in /etc/my.cnf or /etc/mysql/my.cnf catalog, view the current configuration using cat

[root@yoyo ~]# 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
[root@yoyo ~]# 

In max_allowed_packet = 100M may be added to the configuration file in the my.cnf [mysqld]

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11421178.html