我的数据库驱动程序类应该支持复制 (PHP) 吗?
我目前正在为数据库引擎编写 PHP 应用程序和驱动程序(类)。 我想知道是否需要写一个复制支持(主从)? 我对此有点陌生,那么,如果我想支持负载平衡/复制,我的项目或类应该担心哪些事情? 哦,这是关于 MySQL 的。
I'm currently writing a PHP application and drivers (classes) for the database engines. I was wondering if I need to write a replication support (master-slave)? I'm a bit new to this, so, what kind of things should my project or classes worry about if I want to support load balancing/replication? Oh and this is about MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们使用主从数据库的方式是使用主数据库进行所有“活动使用”,使用从数据库进行所有报告(数据是否仍然稍微“赶上”并不重要)。 根据您的需要,您可以在主机上进行所有数据操作,并在从机上进行所有数据读取。 当您有阻塞插入或更新时,这尤其有帮助。 (注意:还要尽可能考虑“插入延迟”MySQL 语法,这也有助于避免阻塞。)
就 PHP 对此的支持而言,您真正需要的是保持对多个(两个)数据库连接的干净处理,并使用根据需要设置主(读/写)或从(只读)数据库连接。
The way we use our master-slave db, is to use the master for all "active usage", and the slave for all reporting (where it doesn't matter if the data is still "catching up" slightly). Depending on your needs, you could have -all- data manipulation occur on the master, and -all- data reading occur on the slave. This especially helps when you have blocking inserts or updates. (Note: Also consider the "insert delayed" MySQL syntax where possible, which helps avoid blocking too.)
As far as the PHP support for this, all you really need is to keep clean handling for multiple (two) database connections, and use the master (read/write) or slave (ONLY READ) db connection as desired.
如果您认为将使用从属设备进行读取,使用主设备进行写入,那么您的类需要同时支持至少多个连接。
我将向您展示我使用的 API,如果您选择这种方式,我可以向您发送课程。
ShusterDb::getInstance('read')->select($sql); //确保这是方法中的 SELECT。
ShusterDb::getInstance('write')->标量($sql);
If you think you will use the slaves to read and the master to write, then your Class needs to support at least several connections at once.
I will show you the API I used, If you choose that way, I can send you the class.
ShusterDb::getInstance('read')->select($sql); //makes sure this is a SELECT in the method.
ShusterDb::getInstance('write')->scalar($sql);
Itay,如果您愿意发送您的课程,我将有兴趣查看/可能使用它。
Itay, if you are open to sending your class, I would be interested in seeing / possibly using it.