2 min read

Connect Ghost to your managed DigitalOcean MySQL Database (DBaaS)

Digital Ocean provide a nice way to create and connect to managad databases including MySQL, Redis and PostgreSQL. However, it can sometimes be tricky setting up your configuration to connect your app to a managed database. In this tutorial, you will learn how to do just that with a hosted Ghost app.

I am going to assume you have already created a MySQL database through the Digital Ocean control panel. If you haven’t and you’re not sure how to do this, you can follow their instructions here.

Getting started

If you’re like me and you created a new droplet after creating your database, then you will need to head to the “databases” panel and add your droplet as a trusted source. Once added, click save and you’ll be good to connect from your new droplet.

Next, you will need to create a new database user and set the password encryption to “Legacy – Mysql 5.x”. If you don’t do this, then Ghost won’t be able to connect properly.

Create a new database with your chosen name and you’re ready to edit the configuration file.

Be aware

If you already have a database setup, please be sure to back it up and save the connection details before moving to the next section. You may also want to transfer the tables and data you already have over to your new managed database.

The configuration can be slightly tricky, especially with how new this can be for a lot of people. Next, you need to SSH into your server and edit the config file “config.production.json“. Inside the “database” parameter, delete what you have and add the following…

 "connection": {      "host": "private-db-mysql.db.ondigitalocean.com",      "port": 25060,      "user": "username",      "password": "password",      "database": "db-name",      "ssl": {        "ca": "-----BEGIN CERTIFICATE-----\nMIIFY... truncated ...pq8fa/a\n-----END CERTIFICATE-----\n\n"      }    }  },

Update the host, user, password and ca values with the details given from Digital Ocean. You will need to download the CA certificate from your database dashboard and copy/paste the text inside the file to the “ca” parameter in the code above.

Closing thoughts

Once you have finished and saved the file, you’re ready to restart your Ghost app and hopefully everything starts correctly. However, if it doesn’t restart correctly, please revert your changes and comment below or message me with any errors you have received.