Heroku-无法连接到本地MySQL服务器

时间:2018-07-11 13:07:04

标签: python mysql django database heroku

我已经部署了包含以下内容的项目

  • Django
  • 其他框架
  • Reactjs
使用this medium post作为参考

Heroku 。现在,我将代码保存在deploy-heroku分支中,因此我这样做了:

git push heroku deploy-heroku:master

现在,构建成功,但是部署失败。

这是追溯

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
    return Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/MySQLdb/__init__.py", line 86, in Connect
    return Connection(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
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 "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 332, in execute
    self.check()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 57, in _run_checks
    issues = run_checks(tags=[Tags.database])
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/checks/database.py", line 10, in check_database_backends
    issues.extend(conn.validation.check(**kwargs))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 9, in check
    issues.extend(self._check_sql_mode(**kwargs))
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 13, in _check_sql_mode
    with self.connection.cursor() as cursor:
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 255, in cursor
    return self._cursor()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 232, in _cursor
    self.ensure_connection()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
    self.connect()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 236, in get_new_connection
    return Database.connect(**conn_params)
  File "/app/.heroku/python/lib/python3.6/site-packages/MySQLdb/__init__.py", line 86, in Connect
    return Connection(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

在哪里更改数据库配置。我以为heroku会自动做到这一点!


settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'redditdb',
        'HOST': 'localhost',
        'USER':'root',
        'PASSWORD': 'root'
    }
}

1 个答案:

答案 0 :(得分:1)

这些是您要在heroku上使用postgres所做的更改。

您必须安装两个python软件包

 pip install psycopg2   # postgres adapter for python
 pip install dj_database_url

记住要在您的requirements.txt

中进行更新
pip freeze > requirements.txt

最后在您的 settings.py 中,import dj_database_url并按如下所示更新您的DATABASES设置

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

现在将您的应用程序部署到heroku。 Heroku将自动为您添加一个postgresql插件。