php-mysql-replication学习笔记

发表于 2019-03-12 16:28:16
阅读 346

安装

安装包

github安装包地址

https://github.com/krowinski/php-mysql-replication

https://github.com/doctrine/dbal

https://github.com/doctrine/event-manager

https://github.com/php-fig/simple-cache

https://github.com/symfony/symfony

依赖扩展

依赖php扩展sockets、bcmath,请根据自己的环境情况进行安装配置

安装 sockets 扩展

这是在 php-7.4.6 下的安装命令

cd php-7.4.6/ext/sockets/
/tongfu.net/env/php-7.4.6/bin/phpize
./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config
make && make install
cd ../../../

安装 bcmath 扩展

这是在 php-7.4.6 下的安装命令

cd php-7.4.6/ext/bcmath/
/tongfu.net/env/php-7.4.6/bin/phpize
./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config
make && make install
cd ../../../

下载

通过github下载上面的几个软件包

php-mysql-replication-master.zip

dbal-master.zip

event-manager-master.zip

simple-cache-master.zip

symfony-master.zip

目录组织

php-mysql-replication-master/src/MySQLReplication/
dbal-master/lib/Doctrine/
event-manager-master/lib/Doctrine/
simple-cache-master/src/ -> simple-cache-master/src/Psr/SimpleCache/
symfony-master/src/Symfony/

使用

autoload

建立autoload.php文件,写入如下代码

function php_replication_mysql_autoload($class){
    include_once('/tongfu.net/web/develop/TFWEB/Extends/Driver/'. str_replace("\\", "/", $class). '.php');
}

spl_autoload_register('php_replication_mysql_autoload');

dump_events

设置参数,注意这里的账号必须是root账号(就是授权 *.* 的权限的账号)

namespace example;

include __DIR__ . '/autoload.php';

use MySQLReplication\Config\ConfigBuilder;
use MySQLReplication\Event\DTO\EventDTO;
use MySQLReplication\Event\EventSubscribers;
use MySQLReplication\MySQLReplicationFactory;
use Symfony\Component\Config\Definition\Exception\Exception;
use MySQLReplication\Config\Config;

$binLogStream = new MySQLReplicationFactory(
    (new ConfigBuilder())
        ->withUser('root')
        ->withHost('10.16.1.101')
        ->withPort(3306)
        ->withPassword('abcdef')
        ->build()
);

class MyEventSubscribers extends EventSubscribers
{
    public function allEvents(EventDTO $event)
    {
        echo json_encode($event, JSON_PRETTY_PRINT);
    }
}

通过控制台方式执行

[root@tongfunet] /usr/bin/php /tongfu.net/web/develop/TFHome/extends/test/php_replication_mysql/dump_events.php

=== Event format description === 
Date: 2019-03-13T10:04:52+00:00
Log position: 0
Event size: 119
Memory usage 1.69 MB

这个时候我们建立一个数据表

再写入一条数据

再修改这条数据

MySQL [test]> create table users(id int auto_increment, user varchar(45) null, pwd varchar(45) null, logDT datetime null, primary key(id));
Query OK, 0 rows affected (0.01 sec)

MySQL [test]> insert into users values (null, 'user1', md5('123456'), now());
Query OK, 1 row affected (0.01 sec)

MySQL [test]> update users set logDT = now() where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

接着控制台就会打印信息

=== Event query === 
Date: 2019-03-13T11:44:00+00:00
Log position: 28361
Event size: 210
Database: tfapi_utf8
Execution time: 0
Query: create table users(id int auto_increment, user varchar(45) null, pwd varchar(45) null, logDT datetime null, primary key(id))

=== Event query === 
Date: 2019-03-13T11:45:14+00:00
Log position: 28512
Event size: 86
Database: tfapi_utf8
Execution time: 0
Query: BEGIN

=== Event tableMap === 
Date: 2019-03-13T11:45:14+00:00
Log position: 28574
Event size: 62
Table: users
Database: tfapi_utf8
Table Id: 193
Columns amount: 4

=== Event write === 
Date: 2019-03-13T11:45:14+00:00
Log position: 28658
Event size: 84
Table: users
Affected columns: 4
Changed rows: 1
Values: Array
(
    [0] => Array
        (
            [id] => 1
            [user] => user1
            [pwd] => e10adc3949ba59abbe56e057f20f883e
            [logDT] => 2019-03-13 19:45:14
        )

)


=== Event xid === 
Date: 2019-03-13T11:45:14+00:00
Log position: 28689
Event size: 31
Transaction ID: 1088

=== Event query === 
Date: 2019-03-13T11:45:39+00:00
Log position: 28840
Event size: 86
Database: tfapi_utf8
Execution time: 0
Query: BEGIN

=== Event tableMap === 
Date: 2019-03-13T11:45:39+00:00
Log position: 28902
Event size: 62
Table: users
Database: tfapi_utf8
Table Id: 193
Columns amount: 4

=== Event update === 
Date: 2019-03-13T11:45:39+00:00
Log position: 29036
Event size: 134
Table: users
Affected columns: 4
Changed rows: 1
Values: Array
(
    [0] => Array
        (
            [before] => Array
                (
                    [id] => 1
                    [user] => user1
                    [pwd] => e10adc3949ba59abbe56e057f20f883e
                    [logDT] => 2019-03-13 19:45:14
                )

            [after] => Array
                (
                    [id] => 1
                    [user] => user1
                    [pwd] => e10adc3949ba59abbe56e057f20f883e
                    [logDT] => 2019-03-13 19:45:39
                )

        )

)


=== Event xid === 
Date: 2019-03-13T11:45:39+00:00
Log position: 29067
Event size: 31
Transaction ID: 1091

应用

介绍

我们可以通过监听一个mysql的binlog的数据变化来捕获数据的更新变化来做很多事情

我们可以通过这个来实现mysql到redis的自动写入

我们还可以通过这个来实现mysql数据的采集再分析再加工的统计需求

常见问题

dump_events数据为空

问题:

发现dump_events无法正常捕获解析mysql的binlog增量数据,一般情况下是授权账号权限有问题

解决:

解决步骤如下:

1、先授权最高权限给 dump_events

grant all on *.* to php_mysql_replication_tfams@localhost identified by 'abcdef'

2、再启动 dump_events,这时会发现数据是正常的。记录下上次的 replication state(file, position)以备下次启动时候使用

3、再授权复制权限给 dump_events

grant replication slave, replication client on *.* to php_mysql_replication_tfams@localhost identified by 'abcdef'

4、再重新启动 dump_events,这时会发现数据依然是正常的