DBA > Articles

Creating and linking a MariaDB database

By:
To read more DBA articles, visit http://dba.fyicenter.com/article/

After deploying our PHP application we will create a MariaDB database and link it to our application in Dokku v0.4.5 running on DigitalOcean.

MariaDB is a drop-in replacement of MySQL, meaning if you have an application that is using MySQL you can export the database and import it into MariaDB and everything will still work.

The instructions in this article will be for the MariaDB plugin but they are basically the same for the other database plugins.

Create the database
Start by installing the plugin:
dokku plugin:install https://github.com/dokku/dokku-mariadb.git mariadb
Now create a database:
dokku mariadb:create db_name
Link the database service to the application:
okku mariadb:link db_name app_name
Connect to the database in your application

When linking a database to an application an environment variable is created named DATABASE_URL. This variable contains the necessary information to connect to the database.

The variable is in the following format:
service://user:pass@host:port/db_name

To connect to the database in PHP you can extract the information from this environment variable without having to store sensitive information in your repository:

$url = getenv('DATABASE_URL');
$components = parse_url($url);

if ($components) {
$host = $components['host'];
$username = $components['user'];
$password = $components['pass'];
$dbname = substr($components['path'], 1);
$port = $components['port'];
}

Now you can use those variables to connect to the database. Importing an existing database

If you need to import an existing database you can do so with the following command:

dokku mariadb:import db_name < file_name
You can use SFTP to transfer the file from your local machine.
For example I use FileZilla in Windows and in the Site Manager I just need to:
Select the SFTP protocol
Set the logon type to "Key file"
Set "root" as the user
Select the private key file

Full article...


Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/