By anoopmohan on
Hi,
How to set up master-slave database configuration with Drupal 8? Any advice would be highly appreciated.
Thanks
Hi,
How to set up master-slave database configuration with Drupal 8? Any advice would be highly appreciated.
Thanks
Comments
... Years later
Many years later and still no one has an answer to this question. I am in the process of trying to figure that out right now. I will post the answer here when I do.
Please do - it's something I
Please do - it's something I'd be interested in reading more about.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
This Worked for Me
Nice, thanks for the update.
Nice, thanks for the update.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Does this configuration
Does this configuration splits read and writes to DB ?
Surprising
The documentation actually says that the replica should be an array or replicas.
See https://stackoverflow.com/questions/49918991/use-mysql-master-slave-bala...
& https://www.drupal.org/docs/8/api/database-api/database-configuration
Wonder what you mean with it works. Can you elaborate on your usage?
@Bober No, AFAIK there is no functionality in Drupal using it. You would need to use the replica service. You might want to google on master/slave as well as primary/replica. You can also find some test in ./web/core/tests/Drupal/KernelTests/Core/Database testing the replica.
Have you changed anything else related to select query in sytem
@adrian1231 I have also added this configuration in my settings.php file but how to test it is working and how to check that all select core query is using my slave database not master database.
Actually I need to write query only with master database and all select query even related to my custom module or any default core module execute with only slave database so please tell me how to achieve this. I really need it.
Replicating Drupal Db
I'm doing it with Drupal 7 at the moment. In process of converting large custom Drupal 7 app to Drupal 9. I used the standard MySQL way of setting up master/slave DBs. It works in both directions:
my.cnf (master/slave server1)
my.cnf (master/slave server2)
Login to mysql on master/slave server1 and server2 and execute the following:
quit
Restart database
On master/slave server2 execute the following:
On master/slave server1 execute the following:
Simpler is always better.
What configuration required in Drupal 8 end
@donpwinston Thanks for the update.
Actually, I need to what configuration is required in Drupal 8 end, so that master/ slave DB can work means, all insert query will execute through master database and all select query will execute through slave end.
Drupal 8 Master Slave Database Configuration
In Drupal 8 Master-Slave database setup can be configured using the suggestion provided in link: https://www.drupal.org/docs/8/api/database-api/database-configuration. While checking setup in local environment, this approach is not working as per expectation. So there is an alternative snippet which actually worked and tested on Drupal version 8.8.0.
How you tested it?
@Simanta Can you tell me how to tested it over Drupal 8 means can you give me brief explain please i really need it.
Config the master/slave in Drupal 8 or Drupal 7
To apply the slave or replica server, we have to focus on views and db_static. In Views -> ADVANCED Tab -> Query settings -> "check" the Use Slave Server(D7) and Use Secondary Server(D8) checkbox to apply the slave/replica server.
For db_query():
Changes in settings.php file
Thanks
How Replica will work in Drupal 9?
I am trying to run the SELECT query on the replica database but not working. Please look at my configuration:
settings.php
In controller:
Working:
Not working:
Why above code is not working?
No longer supported
See https://www.drupal.org/node/2993033
Use Replica database by database.replica service
Thanks @mmjvb
Now it is working as below: