How to retrieve data from MYSQL

Hello Ghosters,

I am working together with a friend on developing a modification for my ghost website. We have hit a bit of a wall in our knowledge and wanted to ask the community for help.

Here is what we want to achieve:

  1. Our Ghost install is hosted on a Digital Ocean droplet using Nginx on an Ubuntu server with Node.js and MySQL included (as detailed here).
  2. We would like to use Node.js to connect to the MySQL server to retrieve data from within a handlebars template.

How do we achieve this?

From developer:

“I’m able to ‘npm install mysql’ in the terminal and register it in the theme’s package.json file, but once inside of the tag inside of the .hbs template, ‘var mysql = require(‘mysql’)’ doesn’t work, mysql returns as ‘undefined’. I’ve tried requiring it in a .js file, and importing then importing that .js file in the .hbs template, but no luck there either.”

How do we connect to, and retrieve data from our MySQL database with Ghost? Is there a way to do it within a handlebars template file, and if not, is there any way of importing it into the file?

1 Like

Can you explain what you’re trying to do? The Ghost templating language, by design, doesn’t allow you to execute arbitrary code, so it’s not possible to execute SQL queries in the template. Ghost does have some helpers (specifically the {{get}} helper) that can give you data, though

1 Like

Hi there, I’m the dev referred to in the previous post. A bit new to Ghost, but your explanation makes complete sense and affirms my assumptions. What we want to do is essentially query a database if a specific query string component is present in the URL (with some Javascript handling in-between of course), so in this example,, because foo is present, we want to trigger a database call. Unless I’m mistaken, we can’t use the Handlebars helper because there is no way of passing variables into the query, which in our case would be the parsed query string component (I’d love to be wrong on this point though!). I’m leaning towards the idea of using a headless CMS to store our data and using the fetch API to request the data into the template. We’re open to better ideas of course!

1 Like

Everything you’re saying is correct! Handlebars doesn’t give you request context so you wouldn’t be able to have the conditional logic either.

That being said, you don’t have to go full headless - there’s nothing preventing you from using the Fetch API in the client to update the dynamic content areas when using Ghost’s integrated front end.


Great, thanks so much for your help in clarifying. Yeah, I think your approach is the right way to go, no use in reinventing the wheel.