MySQLdb OperationalError:(2004,"无法创建TCP / IP套接字(24)")

时间:2016-02-29 20:48:55

标签: python mysql django

我在这里呆了大约2天,但却找不到失败的原因。所以希望有人可以提供帮助。

我有一些多线程代码,其中一个方法进行如下查询:

make_query(self, test_name):
    ModelObject.objects.filter(test=test_name)

并且每个线程的目标都是上面的方法,如下所示:

Thread(target=make_query, args=(test_name,))

并创建许多线程。但是,我得到一个例外:

Exception in thread Thread-275:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/myapp/sources/model.py", line 23, in make_query
    for row in self.queryset:
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 258, in __iter__
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 52, in __iter__
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 837, in execute_sql
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 389, in as_sql
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 366, in compile
  File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py", line 79, in as_sql
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 366, in compile
  File "/Library/Python/2.7/site-packages/django/db/models/lookups.py", line 157, in as_sql
  File "/Library/Python/2.7/site-packages/django/db/models/lookups.py", line 150, in process_lhs
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 625, in db_type
    return connection.data_types[self.get_internal_type()] % data
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 184, in data_types
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
    return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95, in __exit__
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
OperationalError: (2004, "Can't create TCP/IP socket (24)")

我已经尝试增加mysql中的连接数(设置max_connections变量),但这没有做任何事情。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您有太多打开的文件。您需要增加MySQL配置文件中的open_files_limit。默认情况下,该变量设置为1,024个打开的文件。虽然我也可能会重新考虑你的应用程序的设计,但1,024非常多。