MariaDB (RDMS)#
Dokku has official support for mariadb, a good and mature open source relational database and drop-in replacement for MySQL. The dokku plugin for this is well documented. It can be used to launch & manage databases and link them to apps.
Creating a database for your app#
# create the database
dokku mariadb:create <db-name>
# connect to the database
dokku mariadb:connect <db-name>
Provisioning the database from an existing database#
# dump data from local database
# (NOTE: mysqldump likely requires additional parameters like the hostname, username & password to local db)
mysqldump db_name > db.dump
# send dump to dokku database
dokku mariadb:import <db-name> < db.dump
Linking the database to your app#
# assuming the db and app are created
dokku mariadb:link <db-name> <app-name>
This will set an environment variable: DATABASE_URL
which contains a connection URI of the form mysql://<username>:<auto-generated-password>@<service-name>:3306/<db-name>
.
Importantly, your app should receive this variable and use it to configure the database. For example, in python:
import os
import mysql.connector
DATABASE_URL = os.environ.get('DATABASE_URL')
assert DATABASE_URL is not None, 'Missing DATABASE_URL environment variable to connect to the database'
# connect to db
conn = mysql.connector.connect(DATABASE_URL)
# ... use conn in your app to build queries ...
conn.close()
Exporting from dokku mariadb#
# data in a dokku database can also be exported similarly to mysqldump
dokku mariadb:export <db-name> > db.dump