Jasper Cramwinckel
Jasper Cramwinckel

Reputation: 3

How to connect MySQL Cartridge to Django in OpenShift

I have installed the django 1.8.4/Python 3.3 app in OpenShift and I added a MySQL 5.5 cartridge. Now I want to let django use MySQL instead of sqlite3. To do this, I changed setup.py from

install_requires=[
    'Django==1.8.4'
],

to

install_requires=[
    'Django==1.8.4',
    'mysqlclient'
],

I changed wsgi/myproject/myproject/settings.py from

DATABASES = {
    'default': {
        # GETTING-STARTED: change 'db.sqlite3' to your sqlite3 database:
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(DATA_DIR, 'db.sqlite3'),
    }
}

to

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mysql_db_name', 
        'USER': 'mysql_username', 
        'PASSWORD': 'xxxxxx', 
        'HOST': os.environ['OPENSHIFT_DB_HOST'],
        'PORT': os.environ['OPENSHIFT_DB_PORT'],
    }
}

When I git push to OpenShift, I get the following error:

remote: Executing 'python /var/lib/openshift/5642f9ad7628e16a6300030a/app-root/runtime/repo//wsgi/myproject/manage.py migrate --noinput'       
remote: Traceback (most recent call last):       
remote:   File "/opt/rh/python33/root/usr/lib64/python3.3/os.py", line 673, in __getitem__       
remote:     value = self._data[self.encodekey(key)]       
remote: KeyError: b'OPENSHIFT_DB_HOST'       

What am I doing wrong?

Thanks,

Jasper

Upvotes: 0

Views: 473

Answers (1)

Jiri Fiala
Jiri Fiala

Reputation: 1400

As the error message says, the OPENSHIFT_DB_HOST does not seem to be the correct environment variable. Try using OPENSHIFT_MYSQL_DB_HOST (& include the DB type _MYSQL also for other variables).

You can actually use the variables for username and password too, details here: https://developers.openshift.com/en/managing-environment-variables.html#database-variables

Upvotes: 1

Related Questions