(Python Django MySQL)在查询期间丢失了与MySQL服务器的连接

时间:2018-03-07 13:49:44

标签: python mysql django

我运行一个Python Djanogo应用程序,一天一次向数据库发出请求。

while True:
    today = datetime.date.today()
    for _ in range(3):
        print("checking...")
        # Access objects in the database.
        events = Event.objects.filter(deadline__gt=last, deadline__lt=today, processed=False)
        #Sleep once day.
        sleep(24*60*60)

我从python应用程序附加日志文件时收到错误,并且还附加了MySQL日志中的文件。

Python app log

0 events resolved.
checking...
0 events resolved.
checking...
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python36\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Python36\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python36\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python36\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "applicationFile.py", line 32, in handle
    for i, event in enumerate(events):
  File "C:\Python36\lib\site-packages\django\db\models\query.py", line 250, in __iter__
    self._fetch_all()
  File "C:\Python36\lib\site-packages\django\db\models\query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Python36\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "C:\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 894, in execute_sql
    raise original_exception
  File "C:\Python36\lib\site-packages\django\db\models\sql\compiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "C:\Python36\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python36\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python36\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Python36\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Python36\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Python36\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')

MySQL log

2018-01-26T13:46:04.518560Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 4844ms. The settings might not be optimal. (flushed=0 and evicted=0, during the time.)
2018-01-26T23:09:13.208203Z 451 [Note] Aborted connection 451 to db: 'mydb' user: 'user' host: 'localhost' (Got timeout reading communication packets)
2018-01-27T07:08:00.507190Z 449 [Note] Aborted connection 449 to db: 'mydb' user: 'user' host: 'localhost' (Got timeout reading communication packets)

更新

我尝试更改mysql的数据包大小

my.ini max_allowed_pa​​cket的= 40M

使连接超时无限制。

我尝试添加

'CONN_MAX_AGE': None

到数据库配置。我会在几天内看到它是如何工作的。

更新

mysql> SHOW GLOBAL VARIABLES LIKE "%vers%";

+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| innodb_version          | 5.7.20                       |
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| tls_version             | TLSv1,TLSv1.1                |
| version                 | 5.7.20-log                   |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Win64                        |
+-------------------------+------------------------------+
8 rows in set (0.19 sec)

mysql> SHOW GLOBAL VARIABLES LIKE "%connect%";

+-----------------------------------------------+-----------------+
| Variable_name                                 | Value           |
+-----------------------------------------------+-----------------+
| character_set_connection                      | utf8            |
| collation_connection                          | utf8_general_ci |
| connect_timeout                               | 10              |
| disconnect_on_expired_password                | ON              |
| init_connect                                  |                 |
| max_connect_errors                            | 100             |
| max_connections                               | 151             |
| max_user_connections                          | 0               |
| performance_schema_session_connect_attrs_size | 512             |
+-----------------------------------------------+-----------------+
9 rows in set (0.00 sec)

mysql> SHOW GLOBAL STATUS LIKE "%connect%";

+-----------------------------------------------+---------------------+
| Variable_name                                 | Value               |
+-----------------------------------------------+---------------------+
| Aborted_connects                              | 1                   |
| Connection_errors_accept                      | 0                   |
| Connection_errors_internal                    | 0                   |
| Connection_errors_max_connections             | 0                   |
| Connection_errors_peer_address                | 0                   |
| Connection_errors_select                      | 0                   |
| Connection_errors_tcpwrap                     | 0                   |
| Connections                                   | 27                  |
| Locked_connects                               | 0                   |
| Max_used_connections                          | 3                   |
| Max_used_connections_time                     | 2018-03-07 14:10:15 |
| Performance_schema_session_connect_attrs_lost | 0                   |
| Ssl_client_connects                           | 0                   |
| Ssl_connect_renegotiates                      | 0                   |
| Ssl_finished_connects                         | 0                   |
| Threads_connected                             | 3                   |
+-----------------------------------------------+---------------------+
16 rows in set (0.09 sec)

mysql> SHOW GLOBAL VARIABLES LIKE "%clean%";

+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| innodb_page_cleaners | 1     |
+----------------------+-------+
1 row in set (0.00 sec)

mysql> SHOW GLOBAL VARIABLES LIKE "%max%";

+------------------------------------------------------+----------------------+
| Variable_name                                        | Value                |
+------------------------------------------------------+----------------------+
| binlog_max_flush_queue_time                          | 0                    |
| ft_max_word_len                                      | 84                   |
| group_concat_max_len                                 | 1024                 |
| innodb_adaptive_max_sleep_delay                      | 150000               |
| innodb_change_buffer_max_size                        | 25                   |
| innodb_compression_pad_pct_max                       | 50                   |
| innodb_file_format_max                               | Barracuda            |
| innodb_ft_max_token_size                             | 84                   |
| innodb_io_capacity_max                               | 2000                 |
| innodb_max_dirty_pages_pct                           | 75.000000            |
| innodb_max_dirty_pages_pct_lwm                       | 0.000000             |
| innodb_max_purge_lag                                 | 0                    |
| innodb_max_purge_lag_delay                           | 0                    |
| innodb_max_undo_log_size                             | 1073741824           |
| innodb_online_alter_log_max_size                     | 134217728            |
| max_allowed_packet                                   | 41943040             |
| max_binlog_cache_size                                | 18446744073709547520 |
| max_binlog_size                                      | 1073741824           |
| max_binlog_stmt_cache_size                           | 18446744073709547520 |
| max_connect_errors                                   | 100                  |
| max_connections                                      | 151                  |
| max_delayed_threads                                  | 20                   |
| max_digest_length                                    | 1024                 |
| max_error_count                                      | 64                   |
| max_execution_time                                   | 0                    |
| max_heap_table_size                                  | 16777216             |
| max_insert_delayed_threads                           | 20                   |
| max_join_size                                        | 18446744073709551615 |
| max_length_for_sort_data                             | 1024                 |
| max_points_in_geometry                               | 65536                |
| max_prepared_stmt_count                              | 16382                |
| max_relay_log_size                                   | 0                    |
| max_seeks_for_key                                    | 4294967295           |
| max_sort_length                                      | 1024                 |
| max_sp_recursion_depth                               | 0                    |
| max_tmp_tables                                       | 32                   |
| max_user_connections                                 | 0                    |
| max_write_lock_count                                 | 4294967295           |
| myisam_max_sort_file_size                            | 107374182400         |
| optimizer_trace_max_mem_size                         | 16384                |
| parser_max_mem_size                                  | 18446744073709551615 |
| performance_schema_max_cond_classes                  | 80                   |
| performance_schema_max_cond_instances                | -1                   |
| performance_schema_max_digest_length                 | 1024                 |
| performance_schema_max_file_classes                  | 80                   |
| performance_schema_max_file_handles                  | 32768                |
| performance_schema_max_file_instances                | -1                   |
| performance_schema_max_index_stat                    | -1                   |
| performance_schema_max_memory_classes                | 320                  |
| performance_schema_max_metadata_locks                | -1                   |
| performance_schema_max_mutex_classes                 | 210                  |
| performance_schema_max_mutex_instances               | -1                   |
| performance_schema_max_prepared_statements_instances | -1                   |
| performance_schema_max_program_instances             | -1                   |
| performance_schema_max_rwlock_classes                | 40                   |
| performance_schema_max_rwlock_instances              | -1                   |
| performance_schema_max_socket_classes                | 10                   |
| performance_schema_max_socket_instances              | -1                   |
| performance_schema_max_sql_text_length               | 1024                 |
| performance_schema_max_stage_classes                 | 150                  |
| performance_schema_max_statement_classes             | 193                  |
| performance_schema_max_statement_stack               | 10                   |
| performance_schema_max_table_handles                 | -1                   |
| performance_schema_max_table_instances               | -1                   |
| performance_schema_max_table_lock_stat               | -1                   |
| performance_schema_max_thread_classes                | 50                   |
| performance_schema_max_thread_instances              | -1                   |
| range_optimizer_max_mem_size                         | 8388608              |
| slave_max_allowed_packet                             | 1073741824           |
| slave_pending_jobs_size_max                          | 16777216             |
+------------------------------------------------------+----------------------+
70 rows in set (0.00 sec)

mysql> SHOW GLOBAL VARIABLES LIKE "%thread%";

+-----------------------------------------+---------------------------+
| Variable_name                           | Value                     |
+-----------------------------------------+---------------------------+
| innodb_purge_threads                    | 4                         |
| innodb_read_io_threads                  | 4                         |
| innodb_thread_concurrency               | 9                         |
| innodb_thread_sleep_delay               | 0                         |
| innodb_write_io_threads                 | 4                         |
| max_delayed_threads                     | 20                        |
| max_insert_delayed_threads              | 20                        |
| myisam_repair_threads                   | 1                         |
| performance_schema_max_thread_classes   | 50                        |
| performance_schema_max_thread_instances | -1                        |
| thread_cache_size                       | 10                        |
| thread_handling                         | one-thread-per-connection |
| thread_stack                            | 262144                    |
+-----------------------------------------+---------------------------+
13 rows in set (0.00 sec)

mysql> SHOW GLOBAL STATUS LIKE "%thread%";

+------------------------------------------+-------+
| Variable_name                            | Value |
+------------------------------------------+-------+
| Delayed_insert_threads                   | 0     |
| Performance_schema_thread_classes_lost   | 0     |
| Performance_schema_thread_instances_lost | 0     |
| Slow_launch_threads                      | 0     |
| Threads_cached                           | 0     |
| Threads_connected                        | 3     |
| Threads_created                          | 3     |
| Threads_running                          | 1     |
+------------------------------------------+-------+
8 rows in set (0.00 sec)

0 个答案:

没有答案