Django 迁移失败,(2013, 'Lost connection to MySQL server at \'reading initial communication packet\')

Posted

技术标签:

【中文标题】Django 迁移失败,(2013, \'Lost connection to MySQL server at \\\'reading initial communication packet\\\')【英文标题】:Django migartion fail, (2013, 'Lost connection to MySQL server at \'reading initial communication packet\')Django 迁移失败,(2013, 'Lost connection to MySQL server at \'reading initial communication packet\') 【发布时间】:2019-08-31 11:37:48 【问题描述】:

公司工作购买了NameCheap下的域名(无Root权限)

我现在只需要设置数据库和网页就可以工作了

我公司系统使用:

Apache 版本:2.4.38

mysql 版本:10.1.38-MariaDB-cll-lve

架构:x86_64

操作系统:Linux

Python:3.7

Django:2.1.7

但是当我运行python manage.py migrate

我遇到了这个错误:

Traceback (most recent call last):
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/MySQLdb/connections.py", line 164, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2013, 'Lost connection to MySQL server at \'reading initial communication packet\', system error: 2 "No such file or directory"')

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
    self.check()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 59, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 227, in get_new_connection
    return Database.connect(**conn_params)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
    return Connection(*args, **kwargs)
  File "/home/letsilap/virtualenv/IOT/3.7/lib/python3.7/site-packages/MySQLdb/connections.py", line 164, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server at \'reading initial communication packet\', system error: 2 "No such file or directory"')

我在 *** 上做了一些搜索:

Error during Django "Running migrations": django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')

Trouble closing and reopening MySQL connection after catching/handling django.db.utils.OperationalError (2013, Lost connection to server)

但这不是很有帮助

对该主题的进一步挖掘使我相信 MYSQL 数据库拒绝访问我的 Django 应用程序。

我可以试试这个理论,因为我根本无法访问 MYSQL 设置

总结一下;我需要在 django 应用程序上迁移,但运行命令时出错。

请帮忙,如果你能帮助我,我真的很感激。

【问题讨论】:

【参考方案1】:

我是编码新手。也许你犯了一个初学者的错误?

单词不匹配?包括,大写下划线?

我不知道我是不是在帮忙。

只是建议

【讨论】:

【参考方案2】:

这可能是由于 MYSQL DB 有时间造成的。

您可以通过进入 MySQL Workbench 并设置长 DMSB 来纠正此问题。

这里是响应此错误的文章链接: https://anothercoffee.net/fix-error-code-2013-lost-connection-mysql-server-query/

关于这个问题的mysql官方文档; https://dev.mysql.com/doc/refman/5.7/en/error-lost-connection.html

希望有帮助

【讨论】:

以上是关于Django 迁移失败,(2013, 'Lost connection to MySQL server at \'reading initial communication packet\')的主要内容,如果未能解决你的问题,请参考以下文章

Django 迁移失败

Django:迁移到 NullBooleanField 失败,IntegrityError“包含空值”

Django 迁移在 Heroku 中失败

在迁移文件中使用特定模型时,Django 测试失败

Django 迁移失败 - 无法查询“peterson”:必须是“用户”实例

将 Django 部署到 Elastic Beanstalk,迁移失败