A classic example of PHP database read and write separation

Source: Internet
Author: User
Tags database load balancing mysql query php database
  1. /**
  2. * MySQL read/write separation
  3. * edit:bbs.it-home.org
  4. */
  5. Class db
  6. {
  7. Public function __construct ($sql)
  8. {
  9. $chestr = Strtolower (Trim ($sql));
  10. If the SQL statement has a SELECT keyword, connect to the read database, otherwise connect to the write database
  11. if (substr ($chestr, 0,6) = = ' SELECT ')
  12. {
  13. Echo ' I am using select DB.
    ';
  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. }
  34. $d = new db ("Update ' users ' set ' select ' = ' fasdf ' where ' id ' = 1");
  35. $d 2 = new db ("SELECT * from ' users '");
Copy Code

Database, read-write and share the relevant explanations.

Database reading and writing separation, can effectively reduce the pressure to reduce the database.

A primary database, multiple from the database schema, to achieve read and write separation, is a frequently used scenario. The primary database provides write operations, which provide read operations from the database. In fact, the pressure of the decentralized database, do not have to read and write separation, but the separation of read and write has its own many advantages. For example: 1, in favor of the database load balancing implementation. 2, facilitate the implementation of master-slave database hot backup. If you do not separate, then you need to consider the two-way hot backup strategy. This configuration is more complex, and the problem is also more troublesome. 3, to maintain the high availability of the database, especially in the database cluster environment. 4, finally, the realization of read and write separation, MySQL query speed and stability will be more confident.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.