Migrating Ghost from MySQL to SQLite

Migrating Ghost from MySQL to SQLite

In my first post, I said that I set up my Ghost blog with a MySQL database.

Why is that? Because ghost-cli wants you to use a MySQL database and I happened to have a MariaDB server on my VM and so I just added another database to it

SQLite is a better choice#

However, Ghost supports SQlite as a storage backend.
In fact, SQLite can handle more load than this blog could ever have, considering I use Nginx cache on my reverse proxy. In the end, the database is only used when I do stuff in the back-office, which is very light on the database.

Using SQLite requires less maintenance since you don't have a server running, and that also helps the RAM a lot. I can backup my Ghost blog by saving its folder and... that's all.

There is no reason to use MySQL in place of SQLite in my case. It is very unfortunate that Ghost forces the use of MYSQL.

Do we convert the database?#

So, if you're in the same case as me and want to make you Ghost website lighter, how do you proceed?

At first, I thought that I would have to dump my MySQL database and convert it to SQLite using a tool like mysql2sqlite. This feels kind of dirty though...

Export and import#

The cleanest solution is to export your data into an XML file in your dashboard, in the "Labs" tab:

screenshot_12-03-2018_22-31-40

Then, modify your config.production.json to use a SQLite database.

As an example, here is mine:

{
  "url": "https://stanislas.blog/",
  "server": {
    "port": 2368,
    "host": "127.0.0.1"
  },
  "database": {
    "client": "sqlite3",
    "connection": {
      "filename": "/var/lib/ghost/content/data/ghost.db"
    }
  },
...
}

Make sure Ghost has the permissions to write there:

chown -R yourghostuser: /var/lib/ghost/

Then restart the Ghost service. You should see the install screen, so follow it as if you were installing a new website.

Then, get back to your "Labs" tab and import your .xml export.

To clean things up, remove the default Ghost user and its posts and tags. You should also remove the Ghost user and database from MySQL, or even uninstall your MySQL server if you don't use it.

Now, we can see that our SQLite database is in our content/data folder:

root@lyra /d/ghost# du -hs content/data/ghost.db
584K	content/data/ghost.db

So here were are with our fresh and clean Ghost blog using an indeed lite SQLite database!

It's sad that Ghost doesn't allow use to use SQLite from the beginning. When ghost-cli didn't exist, it was the default, but now it's only in development mode.

Header image source

Stanislas
Author
Stanislas
I like building things with code and computers

Comments

8Atom feed
Markdown supported
  1. Anonymous

    nice experience ... and nice to know

  2. Mariano

    Thank you, I will be spending some time getting my site to work with SQLite, but I wish I had read this earlier cause I just finished tweaking your theme for today, which by the way, I did find it to be much better than the original Casper. I did just a few tweaks more, but I must say I am very pleased with your job. It is my current theme. I shall credit you once I am done re-arranging what's to do. Thank you and keep up the good work.

  3. Yovko Lambrev

    Actually, you can use SQLite from the beginning. Just install it using ghost install --db sqlite3

  4. Kafin

    Hi, i found your blog from google search. Can i What are you using / How you make simple comment feature like this in ghost?

    Thanks for writing this because I also run my own ghost blog kafinsalim.xyz and going to migrate to sqlite

  5. Anonymous

    THANK YOU for the article, I think its cool to run ghost from sqlite3. Does that make it portable? Like could one just copy the whole directory and make another site entirely as easily as one can usually do with sqlite3? Again, thanks so much for the article, it seem that Ghost . org does not want people knowing how easy sqlite 3 is so prople pay the absurd prices for their hosting-, and that is just evil of them to do. Its super easy to install with ghost install --db sqlite3

  6. htmlboss

    Man I wish I found this article before I upsized my lightsail instance for more RAM. Back to experimenting 😂

    Thanks!!