Classic example of separation of read and write in PHP database

WBOY
Release: 2016-07-25 08:56:01
Original
1597 people have browsed it
  1. /**
  2. * mysql read and write separation
  3. * edit: bbs.it-home.org
  4. */
  5. class db
  6. {
  7. public function __construct($sql)
  8. {
  9. $chesr = strtolower(trim($sql));
  10. //Judge sql If the statement has the select keyword, it will connect to the reading database, otherwise it will connect to the writing database
  11. if(substr($chestr,0,6)=='select')
  12. {
  13. echo 'I am using select db..< br>';
  14. $link = mysql_connect("127.0.0.1:3306", "root", "") or die("Could not connect: " . mysql_error());
  15. mysql_select_db("test");
  16. $ result = mysql_query($sql);
  17. while ($row = mysql_fetch_array($result, MYSQL_NUM))
  18. {
  19. printf("%s %s", $row[0],$row[1]);
  20. }
  21. echo mysql_get_host_info($link).mysql_get_server_info($link).mysql_get_proto_info($link).mysql_get_client_info().'
    ';
  22. }
  23. else
  24. {
  25. echo 'I am using insert db..
    ';
  26. $link = mysql_connect("127.0.0.2:3306","root","") or die("Could not connect: " . mysql_error());
  27. mysql_select_db("test");
  28. $result = mysql_query($sql);
  29. echo @mysql_affected_rows($result);
  30. echo mysql_get_host_info($link).mysql_get_server_info($link).mysql_get_proto_info($link).mysql_get_client_info().'
    '; }
  31. }
  32. }
  33. $d = new db(" update `users` set `select`='fasdf' where `id` =1");
  34. $d2 = new db(" SELECT * from `users`");
Copy the code
Attached is the explanation of database reading and writing sharing.

The separation of reading and writing in the database can effectively reduce the pressure on the database.

The architecture of one master database and multiple slave databases to achieve separation of reading and writing is a commonly used solution. The master database provides write operations, and the slave database provides read operations. In fact, dispersing the pressure on the database does not necessarily require separation of reading and writing, but separation of reading and writing still has many benefits of its own. for example: 1. Conducive to the implementation of load balancing of the database. 2. It is conducive to realizing hot backup of master-slave database. If it is not separated, then you need to consider a two-way hot backup strategy. The configuration of this is more complicated, and it is also more troublesome when problems occur. 3. Helps maintain high availability of the database, especially in a database cluster environment. 4. Finally, after realizing the separation of reading and writing, you will have more confidence in the query speed and stability of mysql.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template