How should I refactor my code (PHP & MySQL) to work more efficiently with a Master / slave database configuration?
I have developed a web application using PHP and MySQL which has all been running from a single server. When I开发者_如何转开发 come to scale up and need a separate database server and then ultimately need a master / slave configuration of database servers how should I update my code to connect to the correct server? I have the database connection details stored on a separate file so can update this easily enough. What happens though when I have a master (where all writes will go) and slave (for all reads). What is the best way to optimise my PHP code, is there a good resource / examples of how to structure MySQL for master / slave servers?
cheers
Personally I would refactor the code to disable access the database directly from the core program, and instead crate a "Data Access" class, which would be responsible for determining which actions were "reads" and which actions were "writes" and sending them to the appropriate server(s).
This way, all the code that is aware of the slave/master setup would be in one place and you wouldn't need to modify your core program, except to have it get the data from the "Data Access" class as opposed from the database directly.
精彩评论