我应该如何重构我的代码(PHP 和 MySQL)以更有效地使用主/从数据库配置?

发布于 2024-10-29 06:11:25 字数 246 浏览 13 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半枫 2024-11-05 06:11:25

就我个人而言,我会重构代码以禁止直接从核心程序访问数据库,而是创建一个“数据访问”类,该类负责确定哪些操作是“读取”以及哪些操作是“写入”并将它们发送到适当的服务器。

这样,所有了解从/主设置的代码都将位于一个位置,您不需要修改核心程序,除了让它从“数据访问”类获取数据,而不是从“数据访问”类获取数据。直接数据库。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文