- /**
- * MySQL read/write separation
- * edit:bbs.it-home.org
- */
- Class db
- {
- Public function __construct ($sql)
- {
- $chestr = Strtolower (Trim ($sql));
- If the SQL statement has a SELECT keyword, connect to the read database, otherwise connect to the write database
- if (substr ($chestr, 0,6) = = ' SELECT ')
- {
- Echo ' I am using select DB.
';
- $link = mysql_connect ("127.0.0.1:3306", "Root", "") or Die ("Could Not connect:". Mysql_error ());
- mysql_select_db ("test");
- $result = mysql_query ($sql);
- while ($row = Mysql_fetch_array ($result, Mysql_num))
- {
- printf ("%s%s", $row [0], $row [1]);
- }
- echo Mysql_get_host_info ($link). Mysql_get_server_info ($link). Mysql_get_proto_info ($link). Mysql_get_client_info ( ).'
';
- }
- Else
- {
- Echo ' I am using Insert db:
';
- $link = mysql_connect ("127.0.0.2:3306", "Root", "") or Die ("Could Not connect:". Mysql_error ());
- mysql_select_db ("test");
- $result = mysql_query ($sql);
- echo @mysql_affected_rows ($result);
- echo Mysql_get_host_info ($link). Mysql_get_server_info ($link). Mysql_get_proto_info ($link). Mysql_get_client_info ( ).'
';
- }
- }
- }
- $d = new db ("Update ' users ' set ' select ' = ' fasdf ' where ' id ' = 1");
- $d 2 = new db ("SELECT * from ' users '");
Copy CodeDatabase, 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. |