Introduction

CodeIgniter has made it very simple for us to use failover databases with our CI applications. In this tutorial we’ll learn how to set up a failover database for our application to ensure we don’t have any single points of failure.

Let’s dive into it.

Database Setup

Setting up a failover database in CI is simple and straightforward. First we’ll need to open our application/config/database.php file, then we’ll start adding our failover system settings at the bottom of the file.

Let’s say our database settings currently look like this:

This is a pretty typical setup for a CI web app, so we’ll use this setup as a basis for creating our failover settings. To get started with the failover, append the following to the bottom of the code above:

Now we’re ready to add in our failover settings array:

And that’s it, it’s really that simple. You can have as many failover arrays as you want actually. All you have to do is create another “failover” property within the first failover array to create a second failover array, like so:

And that’s it! Who knew it could be so simple to set up failover databases. CI makes it effortless. To learn more about the CI Database class, review their official documentation.