How should I reorganize my code (PHP and MySQL) to work more efficiently with the Master / Slave database configuration?

I developed a web application using PHP and MySQL, which all works from one server. When I come to scaling and need a separate database server and then end up needing master / slave configuration of database servers, how do I update the code to connect to the correct server? I have database connection data stored in a separate file, so you can easily update it. What happens when I have a master (where all records will go) and a slave (for all readings). What is the best way to optimize my PHP code, is there a good resource / examples on how to structure MySQL for master / slave servers?

amuses

+4
source share
1 answer

Personally, I would reorganize the code to disable access to the database directly from the main program and instead create a data access class that will be responsible for determining which actions were "read" and which actions were "written" and sending them to the appropriate server (s).

Thus, all the code that knows about slave / master configuration will be in one place, and you will not need to change your main program, except that it receives data from the Data Access class, just the opposite of the database.

+1
source

Source: https://habr.com/ru/post/1346231/


All Articles