我应该如何重构我的代码(PHP 和 MySQL)以更有效地使用主/从数据库配置?
我使用 PHP 和 MySQL 开发了一个 Web 应用程序,该应用程序全部在单个服务器上运行。当我扩大规模并需要一个单独的数据库服务器,然后最终需要数据库服务器的主/从配置时,我应该如何更新我的代码以连接到正确的服务器?我将数据库连接详细信息存储在单独的文件中,因此可以轻松更新。当我有一个主设备(所有写入都将进行)和从设备(用于所有读取)时会发生什么。优化 PHP 代码的最佳方法是什么?是否有关于如何为主/从服务器构建 MySQL 的良好资源/示例?
干杯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我个人而言,我会重构代码以禁止直接从核心程序访问数据库,而是创建一个“数据访问”类,该类负责确定哪些操作是“读取”以及哪些操作是“写入”并将它们发送到适当的服务器。
这样,所有了解从/主设置的代码都将位于一个位置,您不需要修改核心程序,除了让它从“数据访问”类获取数据,而不是从“数据访问”类获取数据。直接数据库。
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.