{"id":532,"date":"2014-12-23T15:51:14","date_gmt":"2014-12-23T13:51:14","guid":{"rendered":"http:\/\/misto.ch\/?p=532"},"modified":"2014-12-23T15:51:14","modified_gmt":"2014-12-23T13:51:14","slug":"add-database-to-your-paas","status":"publish","type":"post","link":"https:\/\/misto.ch\/de\/2014\/12\/23\/add-database-to-your-paas\/","title":{"rendered":"Add a Database to your own PaaS"},"content":{"rendered":"<p>This is the second part of our series on how to be your own PaaS provider using Dokku and RunAbove. The <a href=\"\/own-paas-with-runabove-and-dokku\/\">first part covered the basic installation<\/a> and enabled us to deploy simple Play applications. In this part, we&#8217;ll deploy a more interesting application that uses a database.<br \/>\n<!--more--><\/p>\n<p>Adding support for databases and other additional services is handled by <a href=\"http:\/\/progrium.viewdocs.io\/dokku\/plugins\">Dokku plugins<\/a>. Our goal is to add a MariaDB database instance and to deploy a Play application that uses it. So first we need to install <a href=\"https:\/\/github.com\/Kloadut\/dokku-md-plugin\">one of the MariaDB plug-ins<\/a>.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% ssh admin@ownpaas.tk\ncd \/var\/lib\/dokku\/plugins\nsudo git clone --recursive https:\/\/github.com\/Kloadut\/dokku-md-plugin mariadb\ncd mariadb\/dockerfiles\nsudo git checkout master\nsudo dokku plugins-install\n<\/pre>\n<p>Everything is well if the last command finishes with no output. You can verify that the plugin got installed by running dokku help:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nadmin@my-own-paas:~$ dokku help\n...\n    mariadb:console &lt;app&gt;     Open mysql-console to MariaDB container\n    mariadb:create &lt;app&gt;      Create a MariaDB container\n    mariadb:delete &lt;app&gt;      Delete specified MariaDB container\n    mariadb:dump &lt;app&gt; &lt;file&gt; Dump default db database into file\n    mariadb:dumpraw &lt;app&gt;     Dump default db database to std out \n    mariadb:info &lt;app&gt;        Display database informations\n    mariadb:link &lt;app&gt; &lt;db&gt;   Link an app to a MariaDB database\n    mariadb:logs &lt;app&gt;        Display last logs from MariaDB container\n<\/pre>\n<p>So let&#8217;s create a database! <\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nadmin@my-own-paas:~$ dokku mariadb:create computers\n\n-----&gt; MariaDB container created: mariadb\/computers\n\n       Host: 172.17.42.1\n       Port: 49154\n       User: 'root'\n       Password: 'XXX'\n       Database: 'db'\n<\/pre>\n<p>You don&#8217;t have to write the configuration down, we will later inject them into our application container as environment variables. First, create a new application and do the usual dance to deploy it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% activator new computer-database computer-database-scala\n% cd computer-database\/\n% git init &amp;&amp; git add . &amp;&amp; git commit -m &quot;init&quot;\n% git remote add dokku dokku@ownpaas.tk:computer-database\n% git push dokku HEAD \n...\n=====&gt; Application deployed:\n       http:\/\/computer-database.ownpaas.tk\n<\/pre>\n<p>Don&#8217;t try to access the application, it won&#8217;t work. Why?<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nadmin@my-own-paas:~$ dokku apps\n=== My Apps\ncomputer-database\nplay-scala\nadmin@my-own-paas:~$ dokku logs computer-database\nPicked up JAVA_TOOL_OPTIONS:  -Djava.rmi.server.useCodebaseOnly=true\n@6ki18l5gj: Database 'default' needs evolution!\n...\n&#x5B;warn] play - Run with -DapplyEvolutions.default=true if you want to run them automatically (be careful)\n<\/pre>\n<p>Not only does it need evolution, we also need to configure it to use our MariaDB instead of the H2 in-memory database it is currently configured to use. We can pass the evolutions-option using an environment variable:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nadmin@my-own-paas:~$ dokku config:set computer-database &quot;JAVA_OPTS=-DapplyEvolutions.default=true&quot;\n<\/pre>\n<p>Now, let&#8217;s link the application container to the database container:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nadmin@my-own-paas:~$ dokku mariadb:link computer-database computers\n...\n-----&gt; Setting config vars and restarting computer-database\n       DB_HOST:     172.17.42.1$\n       DB_PASSWORD: 18BZYCAANmepCbPD$\n       DB_NAME:     db$\n       DB_PORT:     49154$\n       DB_USER:     root\n...\n-----&gt; computer-database linked to mariadb\/computers database\n<\/pre>\n<p>We can use those environment variables in the application configuration, so edit conf\/application.conf and change the database section to this:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ndb.default.driver=com.mysql.jdbc.Driver\ndb.default.url=&quot;jdbc:mysql:\/\/&quot;${DB_HOST}&quot;:&quot;${DB_PORT}&quot;\/&quot;${DB_NAME}\ndb.default.user=${DB_USER}\ndb.default.password=${DB_PASSWORD}\n<\/pre>\n<p>Also don&#8217;t forget to update the build configuration to add the JDBC MySQL connector:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nlibraryDependencies ++= Seq(\n  ...\n  &quot;mysql&quot; % &quot;mysql-connector-java&quot; % &quot;5.1.30&quot;\n)\n<\/pre>\n<p>Ideally, we would be done now, but the SQL script in the evolutions aren&#8217;t valid for MySQL\/MariaDB, so we need to patch the sample application. I&#8217;ve prepared a patch that you can apply to your application (<a href=\"https:\/\/github.com\/opensas\/openshift-play2-computerdb\">based on this fork<\/a>):<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% curl http:\/\/misto.ch\/wp-content\/mysql-compatible-evolutions.patch | git apply -\n<\/pre>\n<p>Add and commit to the repository to deploy:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n% git commit -a -m &quot;db config&quot;\n% git push dokku HEAD\n<\/pre>\n<p>Now it&#8217;s time to visit your application! This concludes this series. Let me know in the comments if you have any suggestions for further articles, providers to evaluate or things to deploy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the second part of our series on how to be your own PaaS provider using Dokku and RunAbove. The first part covered the basic installation and enabled us to deploy simple Play applications. In this part, we&#8217;ll deploy a more interesting application that uses a database. Adding support for databases and other additional [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[29,35,36,38],"class_list":["post-532","post","type-post","status-publish","format-standard","hentry","category-infoq","category-scala","tag-database","tag-paas","tag-play","tag-scala"],"_links":{"self":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts\/532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":0,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"wp:attachment":[{"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/misto.ch\/de\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}