Thinkphp + Mysql master-slave synchronization to achieve separate read and write

Thinkphp + Mysql master-slave synchronization to achieve separate read and write

Listing, thinkphp environment two mysql +

Separate read and write, master-slave synchronization Advantage

  • Real time disaster recovery, for failover;
  • Separate read and write, to provide tracking, load balancing;
  • Tracking data, to avoid traffic impact.

Separate read and write, a primary synchronization principle from

  • MySQL master server service will all write operations recorded in the binlog log and generate log dump thread will pass binlog log I / O threads from the server MySQL services.
  • Generates two threads from the server MySQL service, one I / O thread, the other is the SQL thread.
  • From library I / O request binlog thread to the log of the primary database, and the log file is written binlog relaylog (relay log) in.
  • SQL thread reads from the content database relaylog in and parsed into specific operation to achieve consistent master-slave operation, the purpose of the final data in accordance with two databases.

Thinkphp Configuration

  • Modify config / database.php
    // 启用分布式数据库
    'deploy'    =>  1,
    // 数据库类型
    'type'        => 'mysql',
    // 服务器地址
    'hostname'    => '192.168.1.1,192.168.1.2',
    // 数据库名
    'database'    => 'demo1,demo2',
    // 数据库用户名
    'username'    => 'root1,root2',
    // 数据库密码
    'password'    => 'pass1,pass2',
    // 数据库连接端口
    'hostport'    => '3306',
    //开启读写分离
    'rw_separate'     => true,
    

Mysql configuration

这里 'deploy'    =>  1,表示启用分布式数据库 'rw_separate'     => true,开启读写分离
demo1,demo2代表你两个数据库名,其他类似。
  • Mysql configuration master-slave synchronization

    • Mysql modify the configuration file, I use the pagoda installed (etc / my.cnf) under
    • log-bin=mysql-binturn on
    • server-id = 1This represents the main library and the library from either the same, whatever the value.
    • The above operation must be configured from the library main library
  • Main Library Configuration

    • Create a user, from each server will need to use an account name and password to connect to the master, can each create an account from the server, you can make all servers use the same account. Here is all the same ip segment only from the server to create a master-slave synchronization account. First landing mysql, and then create a user named master, the password is 123456 account, which can be use 192.168.1.1, and the account can only be master-slave synchronization
    • CREATE USER 'master'@'192.168.1.1' IDENTIFIED BY '123456';

GRANT REPLICATION SLAVE ON *.* TO 'master'@'1192.168.1.1' IDENTIFIED BY '123456' WITH GRANT OPTION;

  • From the Library Configuration

    • From the library on the server Mysql
      CHANGE MASTER TO
      MASTER_HOST='192.168.1.1',
      MASTER_USER='master',
      MASTER_PASSWORD='123456',
      MASTER_LOG_FILE='mysql-bin.000097',
      MASTER_LOG_POS=1507;

    • View LOG_FILE and POS mysql entered on the master server show master status\Gcan see, I do not directly copy the above configuration, if you have copied and implemented, then you can stop slaveclose the service, and then re-execute the new configuration, namely restart mysql can.

Released four original articles · won praise 0 · Views 68

Guess you like

Origin blog.csdn.net/qq_32450143/article/details/105047869