Here is how to run more than one database connection.
This article is about using databases with different data, not necessary for load balancing (or connection pooling) between databases.
Inside of your datbase configuration file – likely
app/config/database.php – you can define more than one database connection of any type. In fact, you can define as many connections as you’d like. For instance, if your application has to pull data from 2 MySQL databases, you can define them both separately:
We have our default connection still set to
mysql. This means that, unless we specify otherwise, the application will use thet
Now that we have a 2nd database connection setup – how do we use it in code?
It turns out there’s a few ways!
Within the Schema Builder, you can use the with any connection. To specify which connection to use, simply run the
Similar to Schema Builder, you can on the Query Builder:
One way is to set the variable in your model: