简体   繁体   English

如何在Heroku上连接到MySQL?

[英]How can I connect to MySQL on Heroku?

I have a Django project that uses MySQL as its database. 我有一个使用MySQL作为其数据库的Django项目。 On my development machine the MySQL database runs on my local machine as shown here: 在我的开发机器上,MySQL数据库在我的本地机器上运行,如下所示:

DATABASES = {

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'xxx',
        'USER': 'root',
        'PASSWORD': 'xxxx',
        'HOST': 'localhost',
        #'PORT': '3306',
    }
}

I have successfully deployed the project to Heroku, but it keeps showing that 我已将项目成功部署到Heroku,但它不断显示

2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused) 2003,“无法连接到'localhost'上的MySQL服务器([Errno 111]连接被拒绝)

since the setting is the local MySQL and the views.py contain functions that import local database. 因为设置是本地MySQL,并且views.py包含导入本地数据库的函数。 I still cannot figure out how to have the website to connect to this database. 我仍然不知道如何将网站连接到该数据库。

Is there a way to make this work with Heroku, or do I need to find another host? 是否可以通过Heroku使其工作,还是需要寻找其他主机?

You can promote the settings.py to a package. 您可以将settings.py升级为软件包。 Create the settings folder like this one 创建这样的设置文件夹

settings/
├── __init__.py
├── base.py
├── dev.py
├── heroku.py

Keep the base.py as your settings.py and then, based on the env, changes what you need. 将base.py保留为settings.py,然后根据环境更改所需的内容。

Your heroku.py could be 您的heroku.py可能是

from .base import *

DATABASE = {
    # change to use the database on heroku
}

What is important is to tell the deploy version to use the new settings. 重要的是告诉部署版本使用新设置。 Check the wsgi.py 检查wsgi.py

import os

from django.core.wsgi import get_wsgi_application

# point to the new settings.dev as default, if the `DJANGO_SETTINGS_MODULE`
# is not set
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev")
application = get_wsgi_application()

Remember to change the DJANGO_SETTINGS_MODULE in heroku to settings.heroku 记住将heroku中的DJANGO_SETTINGS_MODULE更改为settings.heroku

A really useful way to do this is to use django-environ and set the configuration option on the heroku settings. 一种非常有用的方法是使用django-environ并在heroku设置上设置配置选项。

# settings.py
# Parse database connection url strings like psql://user:pass@127.0.0.1:8458/db
DATABASES = {
    # read os.environ['DATABASE_URL'] and raises ImproperlyConfigured exception if not found
    'default': env.db(),

}

and your environment variable like: 和您的环境变量,如:

DATABASE_URL=mysql://user:%23password@127.0.0.1:3306/dbname

Regarding your question, Heroku is a really good option to deploy your projects. 关于您的问题,Heroku是部署项目的一个很好的选择。

juanmhidalgo's answer is a good start, and can be generalized for arbitrary environment variables. juanmhidalgo的答案是一个好的开始,可以推广到任意环境变量。 But if you only care about the database variable there's another solution. 但是,如果您只关心数据库变量,那么还有另一种解决方案。

By default, Heroku provides a PostgreSQL database and sets your application's DATABASE_URL with a connection string that can be used to connect to it. 默认情况下,Heroku提供PostgreSQL数据库,并使用可用于连接到它的连接字符串设置应用程序的DATABASE_URL

As far as I know, the various MySQL addons also set this variable but it would be helpful to know which one you've selected so we can confirm. 据我所知,各种MySQL插件也都设置了该变量,但是知道您选择了哪个变量会有所帮助,以便我们进行确认。 It may need to be set manually, based on another environment variable. 它可能需要基于另一个环境变量手动设置。

Assuming that the DATABASE_URL environment variable is properly set you can use dj-database-url to set your database up directly, optionally providing a fallback to use when the variable isn't available (eg on your development box), in your settings.py file: 假设已正确设置DATABASE_URL环境变量,则可以使用dj-database-url直接设置数据库,还可以选择在settings.py提供备用,以在变量不可用时使用(例如,在开发箱中)。文件:

import dj_database_url

DATABASES['default'] = dj_database_url.config(
    default='mysql://root:<password>@localhost:3306/<database>',
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM