Pagoda builds TP6 distributed database: master-slave synchronization, read-write separation

Prerequisites for master-slave configuration
1. The MySQL version is consistent;
2. The data in MySQL is consistent;

Master: 192.168.16.135
Slave: 192.168.16.137

Build two servers in the virtual machine, install pagodas on both of them, and close the fire wall of the two servers

systemctl stop firewalld.service

Two virtual machine services need to ping each other
Insert picture description here
1: Configure the main server mysql

#[必须]启用二进制日志 
log-bin=mysql-bin 
#[必须]服务器唯一ID,默认是1,一般取IP最后一段 
server-id=135

Insert picture description here
Reboot. . Enter the mysql command line

##创建帐户'slave'使用 "%" 所有从服务的ip
grant replication slave on *.* to 'slave'@'%' identified by '123456';

##创建帐户'slave'使用从服务的ip
##grant replication slave on *.* to 'slave'@'192.168.16.137' identified by '123456';

Query user

select user, host, password, from mysql.user;

Insert picture description here
To query the status of the master (master server), file position is required

show master status;

Insert picture description here
2. Configure the slave (slave server) mysql

#[可选]启用二进制日志 
log-bin=mysql-bin 
#[必须]服务器唯一ID,默认是1,一般取IP最后一段 
server-id=137

Insert picture description here
Restart the mysql service...Enter the mysql command line to
start the replication function from the server

change master to master_host='192.168.16.135',master_user='slave',master_password='123456',master_log_file='mysql-bin.000019',master_log_pos=3887;
  • master_host=Master server IP master_user=The backup user name created on the master server
  • master_password=Backup user password
  • master_log_file=The value of the File column obtained by querying the status of the master (master server)
  • master_log_pos=Value of the Position column

Then start the copy function from the server

start slave

Check the status of the replication function from the server

show slave status\G

Insert picture description here

##正确 这两个一定要yes
Slave_IO_Running: Yes 
lave_SQL_Running: Yes

Slave_IO and Slave_SQL processes must be running normally, that is, YES state, otherwise they are all wrong states (such as: where connect and NO are both wrong).

At this point, the configuration is complete, and then create databases and tables on the master and slave servers, and the mysql of the slave server will also have databases and tables.

3: TP6 configures the distributed database
In database.php:
Insert picture description here
I have the same mysql username and password for both services.
If they are not the same, you can set the
Insert picture description here
test as follows:

public function add(){
    
    
        $data = [];
        for($i=0;$i<5;$i++){
    
    
            array_push($data,['name'=>'name_'.($i+1)]);
        }
        $res = Db::name("student")->insertAll($data);       //主库
        dump('插入成功条数:' . $res);
    }

    public function list(){
    
    
        $res = Db::table('student')->where('id','=',1)->find();  //从库
        dump($res);
        $row = Db::table('student')->where('id','=',$res['id'])->update(['name' => 'name_4']);  //主库
        echo $row;

        // 在配置中 开启自动主库读取
        //'read_master' => true,
        //$res2 = Db::table('student')->where('id','=',1)->find(); //主库

        
        $res2 = Db::table('student')->where('id','=',1)->master(true)->find(); //主库
        dump($res2);
    }

Insert picture description here

Insert picture description here
You can see that different servers are used for both reading and writing.

The above is the file configuration in the framework of database read-write separation, master-slave synchronization, database server file configuration and code examples.

Guess you like

Origin blog.csdn.net/hgb24660/article/details/115351386