Home > Backend Development > PHP Tutorial > Detailed practical explanation of reading and writing separation using PHP+MYSQL

Detailed practical explanation of reading and writing separation using PHP+MYSQL

墨辰丷
Release: 2023-03-27 20:36:01
Original
2221 people have browsed it

This article mainly introduces the implementation of read-write separation in PHP MYSQL. It analyzes the techniques of read-write separation with examples, thereby improving the load capacity of the database. It has certain reference value. Those who are interested can learn more.

1. Introduction

I have written 2 articles before, namely:

The principle of Mysql master-slave synchronization

Myql master-slave synchronization practice

Based on this, we will implement a simple PHP Mysql read-write separation to improve the load capacity of the database.

2. Code practice

<?php
class Db
{
  private $res;
  function __construct($sql)
  {
    $querystr = strtolower(trim(substr($sql,0,6)));
    //如果是select,就连接slave服务器
    if($querystr == &#39;select&#39;)
    {
      $res=$this->slave_select($sql);
      $this->res=$res;
    }
    //如果不是select,就连接master服务器
    else
    {
      $res=$this->master_change($sql);
      $this->res=$res;
    }
  }

  /**
   * slave从库返回sql查询结果
   * @param $sql
   * @return array
   */
  private function slave_select($sql){
    //该处只是随机获取slave节点的ip,当然,还可以采用其他算法获取slave_ip
    $slave_server=$this->get_slave_ip();
    $dsn="mysql:host=$slave_server;dbname=test";
    $user=&#39;root&#39;;
    $pass=&#39;123456&#39;;
    $dbh=new PDO($dsn, $user, $pass);
    return $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  }

  /**master主库返回sql执行结果
   * @param $sql
   * @return int
   */
  private function master_change($sql){
    $master_server=&#39;192.168.33.22&#39;;
    $dsn="mysql:host=$master_server;dbname=test";
    $user=&#39;root&#39;;
    $pass=&#39;123456&#39;;
    $dbh=new PDO($dsn, $user, $pass);
    return $dbh->exec($sql);
  }

  /**
   * 随机获取slave-ip
   * @return mixed
   */
  private function get_slave_ip(){
    $slave_ips=[&#39;192.168.33.33&#39;,&#39;192.168.33.44&#39;];
    $count=count($slave_ips)-1;
    $random_key=mt_rand(0,$count);
    return $slave_ips[$random_key];
  }

  /**       
   * 获取结果
   * @return int
   */
  public function get_res(){
    return $this->res;
  }
}

$sql1 = "select * from t1";
$sql2 = "insert into t1 (name) values (&#39;haha&#39;)";
$sql3 = "delete from t1 where id=1";
$sql4 = "update t1 set name=&#39;Jerry&#39; where id=2";

$db = new Db($sql1);
//$db = new Db($sql2);
//$db = new Db($sql3);
//$db = new Db($sql4);

var_dump($db->get_res());
Copy after login

The above is the entire content of this article, I hope it will help everyone learn Helps.


Related recommendations:

php method to connect to mysqldatabase

Use the $wpdb class of wordpress to readmysqlHow to solve the problems that occur when doing ajax in the database

php mysql# Detailed explanation of the usage of ##_list_dbs() function

The above is the detailed content of Detailed practical explanation of reading and writing separation using PHP+MYSQL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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