How to code PHP MySQL class for master and slave setup?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 13:01:28

What you want to use (and research) is MySQL Replication. This is handled completely independent of your code. You work with the database the same as if there were 1 or 100 servers.

you sound like you are wanting to improve performance/balance load

yes you need to do any destructive changes to the master database. the slaves can only be used for readonly. you would also need to be careful that you don't write to the master and read from the slave instantaneously, otherwise the data may not have been replicated to the slave yet. so any instantaneous reads would still need to come from the master.

i wouldn't suggest just randomly selecting a slave. you could do this by geographical region if they are spread out, or if you are running in a cluster you can use a proxy to do the load balancing for you..

here is some more info that may help

http://agiletesting.blogspot.com/2009/04/mysql-load-balancing-and-read-write.html

You should consider using mysqlnd_ms - PHP's replication and load balancing plugin.

I think this is better solution, especially for a production environment, since it's native to PHP and MySQL Proxy is still in Alpha release.

Useful links:

The master/slave set up should be handled automatically by the MySQL server, so you should not need any special code for this configuration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!