1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace example; |
5
|
|
|
|
6
|
|
|
error_reporting(E_ALL); |
7
|
|
|
date_default_timezone_set('UTC'); |
8
|
|
|
include __DIR__ . '/../vendor/autoload.php'; |
9
|
|
|
|
10
|
|
|
use Monolog\Handler\StreamHandler; |
11
|
|
|
use Monolog\Logger; |
12
|
|
|
use MySQLReplication\BinLog\BinLogCurrent; |
13
|
|
|
use MySQLReplication\Config\ConfigBuilder; |
14
|
|
|
use MySQLReplication\Event\DTO\EventDTO; |
15
|
|
|
use MySQLReplication\Event\EventSubscribers; |
16
|
|
|
use MySQLReplication\MySQLReplicationFactory; |
17
|
|
|
|
18
|
|
|
$logger = new Logger("app", [new StreamHandler(STDOUT)]); |
19
|
|
|
BinLogBootstrap::clear(); |
20
|
|
|
/** |
21
|
|
|
* Your db configuration @see ConfigBuilder for more options |
22
|
|
|
*/ |
23
|
|
|
$binLogStream = new MySQLReplicationFactory( |
24
|
|
|
BinLogBootstrap::startFromPosition(new ConfigBuilder()) |
25
|
|
|
->withUser('root') |
26
|
|
|
->withHost('127.0.0.1') |
27
|
|
|
->withPort(3306) |
28
|
|
|
->withPassword('root') |
29
|
|
|
->withRetry(3) |
30
|
|
|
->build(), |
31
|
|
|
null, null, null, null, |
32
|
|
|
$logger |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class BenchmarkEventSubscribers |
37
|
|
|
* @package example |
38
|
|
|
*/ |
39
|
|
|
class MyEventSubscribers extends EventSubscribers |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @param EventDTO $event (your own handler more in EventSubscribers class ) |
43
|
|
|
*/ |
44
|
|
|
public function allEvents(EventDTO $event): void |
45
|
|
|
{ |
46
|
|
|
// all events got __toString() implementation |
47
|
|
|
echo $event; |
48
|
|
|
|
49
|
|
|
// all events got JsonSerializable implementation |
50
|
|
|
//echo json_encode($event, JSON_PRETTY_PRINT); |
51
|
|
|
|
52
|
|
|
echo 'Memory usage ' . round(memory_get_usage() / 1048576, 2) . ' MB' . PHP_EOL; |
53
|
|
|
|
54
|
|
|
// save event for resuming it later |
55
|
|
|
BinLogBootstrap::save($event->getEventInfo()->getBinLogCurrent()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Class SaveBinLogPos |
61
|
|
|
* @package example |
62
|
|
|
*/ |
63
|
|
|
class BinLogBootstrap |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private static $fileAndPath; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
private static function getFileAndPath(): string |
74
|
|
|
{ |
75
|
|
|
if (null === self::$fileAndPath) { |
|
|
|
|
76
|
|
|
self::$fileAndPath = '/tmp/bin-log-replicator-last-position'; |
77
|
|
|
} |
78
|
|
|
return self::$fileAndPath; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function clear() { |
82
|
|
|
file_exists(self::getFileAndPath()) && unlink(self::getFileAndPath()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param BinLogCurrent $binLogCurrent |
87
|
|
|
*/ |
88
|
|
|
public static function save(BinLogCurrent $binLogCurrent): void |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
echo 'saving file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL; |
92
|
|
|
|
93
|
|
|
// can be redis/nosql/file - something fast! |
94
|
|
|
// to speed up you can save every xxx time |
95
|
|
|
// you can also use signal handler for ctrl + c exiting script to wait for last event |
96
|
|
|
file_put_contents(self::getFileAndPath(), serialize($binLogCurrent)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param ConfigBuilder $builder |
101
|
|
|
* @return ConfigBuilder |
102
|
|
|
*/ |
103
|
|
|
public static function startFromPosition(ConfigBuilder $builder): ConfigBuilder |
104
|
|
|
{ |
105
|
|
|
if (!is_file(self::getFileAndPath())) { |
106
|
|
|
return $builder; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** @var BinLogCurrent $binLogCurrent */ |
110
|
|
|
$binLogCurrent = unserialize(file_get_contents(self::getFileAndPath())); |
111
|
|
|
|
112
|
|
|
echo 'starting from file:' . $binLogCurrent->getBinFileName() . ', position:' . $binLogCurrent->getBinLogPosition() . ' bin log position' . PHP_EOL; |
113
|
|
|
|
114
|
|
|
return $builder |
115
|
|
|
->withBinLogFileName($binLogCurrent->getBinFileName()) |
116
|
|
|
->withBinLogPosition($binLogCurrent->getBinLogPosition()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// register your events handler here |
121
|
|
|
$binLogStream->registerSubscriber(new MyEventSubscribers()); |
122
|
|
|
|
123
|
|
|
// start consuming events |
124
|
|
|
$binLogStream->run(); |
125
|
|
|
|
126
|
|
|
|