Skip to content

Commit 643a43c

Browse files
authored
Merge pull request #7 from krowinski/mariadb
Mariadb gtid support
2 parents bd72455 + 168bbc9 commit 643a43c

12 files changed

+200
-1
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ Based on a great work of creators:https://github.com/noplay/python-mysql-repli
1010
Installation
1111
=========
1212

13+
In you project
14+
1315
```sh
1416
composer require krowinski/php-mysql-replication
1517
```
1618

19+
or standalone
20+
21+
```sh
22+
git clone https://github.com/krowinski/php-mysql-replication.git
23+
```
24+
1725
MySQL server settings
1826
=========
1927

@@ -33,7 +41,7 @@ Mysql replication events explained
3341
Configuration
3442
=========
3543

36-
You can pass this params to ConfigService->makeConfigFromArray([])
44+
You can pass this array keys to ConfigService->makeConfigFromArray([])
3745

3846
'user' - your mysql user (mandatory)
3947

@@ -49,6 +57,8 @@ You can pass this params to ConfigService->makeConfigFromArray([])
4957

5058
'gtid' - GTID marker(s) to start from (format 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592)
5159

60+
'mariaDbGtid' - MariaDB GTID marker(s) to start from (format 1-1-3,0-1-88)
61+
5262
'slaveId' - script slave id for identification
5363

5464
'binLogFileName' - bin log file name to start from

example/dump_events.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'user' => 'root',
1313
'ip' => '127.0.0.1',
1414
'password' => 'root',
15+
//'mariaDbGtid' => '1-1-3,0-1-88',
1516
//'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
1617
])
1718
);

src/MySQLReplication/BinLog/BinLogConnect.php

+8
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ private function getBinlogStream()
234234
$binFilePos = $this->config->getBinLogPosition();
235235
$binFileName = $this->config->getBinLogFileName();
236236

237+
if ('' !== $this->config->getMariaDbGtid())
238+
{
239+
$this->execute("SET @mariadb_slave_capability = 4");
240+
$this->execute("SET @slave_connect_state = '" . $this->config->getMariaDbGtid() . "'");
241+
$this->execute("SET @slave_gtid_strict_mode = 0");
242+
$this->execute("SET @slave_gtid_ignore_duplicates = 0");
243+
}
244+
237245
if ('' === $binFilePos || '' === $binFileName)
238246
{
239247
$master = $this->mySQLRepository->getMasterStatus();

src/MySQLReplication/Config/Config.php

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class Config
6666
* @var array
6767
*/
6868
private $databasesOnly;
69+
/**
70+
* @var string
71+
*/
72+
private $mariaDbGtid;
6973

7074
/**
7175
* Config constructor.
@@ -76,6 +80,7 @@ class Config
7680
* @param string $dbName
7781
* @param string $charset
7882
* @param string $gtid
83+
* @param string $mariaGtid
7984
* @param int $slaveId
8085
* @param string $binLogFileName
8186
* @param $binLogPosition
@@ -92,6 +97,7 @@ public function __construct(
9297
$dbName,
9398
$charset,
9499
$gtid,
100+
$mariaGtid,
95101
$slaveId,
96102
$binLogFileName,
97103
$binLogPosition,
@@ -114,6 +120,7 @@ public function __construct(
114120
$this->eventsIgnore = $eventsIgnore;
115121
$this->tablesOnly = $tablesOnly;
116122
$this->databasesOnly = $databasesOnly;
123+
$this->mariaDbGtid = $mariaGtid;
117124
}
118125

119126
/**
@@ -167,6 +174,10 @@ public function validate()
167174
{
168175
throw new ConfigException(ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE, ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE);
169176
}
177+
if (!empty($this->mariaDbGtid) && false === is_string($this->mariaDbGtid))
178+
{
179+
throw new ConfigException(ConfigException::MARIADBGTID_ERROR_MESSAGE, ConfigException::MARIADBGTID_ERROR_CODE);
180+
}
170181
}
171182

172183
/**
@@ -225,6 +236,14 @@ public function getGtid()
225236
return $this->gtid;
226237
}
227238

239+
/**
240+
* @return string
241+
*/
242+
public function getMariaDbGtid()
243+
{
244+
return $this->mariaDbGtid;
245+
}
246+
228247
/**
229248
* @return int
230249
*/

src/MySQLReplication/Config/ConfigBuilder.php

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class ConfigBuilder
6464
* @var array
6565
*/
6666
private $databasesOnly = [];
67+
/**
68+
* @var string
69+
*/
70+
private $mariaDbGtid;
6771

6872
/**
6973
* @return Config
@@ -78,6 +82,7 @@ public function build()
7882
$this->dbName,
7983
$this->charset,
8084
$this->gtid,
85+
$this->mariaDbGtid,
8186
$this->slaveId,
8287
$this->binLogFileName,
8388
$this->binLogPosition,
@@ -199,4 +204,11 @@ public function withDatabasesOnly(array $databasesOnly)
199204
$this->databasesOnly = $databasesOnly;
200205
}
201206

207+
/**
208+
* @param string $mariaDbGtid
209+
*/
210+
public function withMariaDbGtid($mariaDbGtid)
211+
{
212+
$this->mariaDbGtid = $mariaDbGtid;
213+
}
202214
}

src/MySQLReplication/Config/ConfigService.php

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public function makeConfigFromArray(array $config)
7373
{
7474
$configBuilder->withDatabasesOnly($v);
7575
}
76+
if ('mariaDbGtid' === $k)
77+
{
78+
$configBuilder->withMariaDbGtid($v);
79+
}
7680
}
7781

7882
return $configBuilder->build();

src/MySQLReplication/Config/Exception/ConfigException.php

+2
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ class ConfigException extends MySQLReplicationException
3030
const BIN_LOG_FILE_NAME_ERROR_CODE = 9;
3131
const BIN_LOG_FILE_POSITION_ERROR_MESSAGE = 'Incorrect binlog position type';
3232
const BIN_LOG_FILE_POSITION_ERROR_CODE = 10;
33+
const MARIADBGTID_ERROR_MESSAGE = 'Maria gtid must be string';
34+
const MARIADBGTID_ERROR_CODE = 11;
3335
}

src/MySQLReplication/Definitions/ConstEventType.php

+3
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@ class ConstEventType
5858
const WRITE_ROWS_EVENT_V2 = 30;
5959
const UPDATE_ROWS_EVENT_V2 = 31;
6060
const DELETE_ROWS_EVENT_V2 = 32;
61+
62+
// mariadb
63+
const MARIA_GTID_EVENT = 162;
6164
}

src/MySQLReplication/Definitions/ConstEventsNames.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ class ConstEventsNames
1616
const DELETE = 'delete';
1717
const UPDATE = 'update';
1818
const WRITE = 'write';
19+
const MARIADB_GTID = 'mariadb gtid';
1920
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: fazi
5+
* Date: 25.08.2016
6+
* Time: 21:10
7+
*/
8+
9+
namespace MySQLReplication\Event\DTO;
10+
11+
use MySQLReplication\Definitions\ConstEventsNames;
12+
use MySQLReplication\Event\EventInfo;
13+
14+
/**
15+
* Class MariaGTIDLogDTO
16+
* @package MySQLReplication\Event\DTO
17+
*/
18+
class MariaDbGtidLogDTO extends EventDTO
19+
{
20+
/**
21+
* @var string
22+
*/
23+
private $type = ConstEventsNames::MARIADB_GTID;
24+
/**
25+
* @var int
26+
*/
27+
private $flag;
28+
/**
29+
* @var int
30+
*/
31+
private $domainId;
32+
/**
33+
* @var int
34+
*/
35+
private $sequenceNumber;
36+
37+
public function __construct(
38+
EventInfo $eventInfo,
39+
$flag,
40+
$domainId,
41+
$sequenceNumber
42+
) {
43+
parent::__construct($eventInfo);
44+
45+
$this->flag = $flag;
46+
$this->domainId = $domainId;
47+
$this->sequenceNumber = $sequenceNumber;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function __toString()
54+
{
55+
return PHP_EOL .
56+
'=== Event ' . $this->getType() . ' === ' . PHP_EOL .
57+
'Date: ' . $this->eventInfo->getDateTime() . PHP_EOL .
58+
'Log position: ' . $this->eventInfo->getPos() . PHP_EOL .
59+
'Event size: ' . $this->eventInfo->getSize() . PHP_EOL .
60+
'Flag: ' . var_export($this->flag, true) . PHP_EOL .
61+
'Domain Id: ' . $this->domainId . PHP_EOL .
62+
'Sequence Number: ' . $this->sequenceNumber . PHP_EOL;
63+
}
64+
65+
/**
66+
* @return string
67+
*/
68+
public function getType()
69+
{
70+
return $this->type;
71+
}
72+
73+
/**
74+
* Specify data which should be serialized to JSON
75+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
76+
* @return mixed data which can be serialized by <b>json_encode</b>,
77+
* which is a value of any type other than a resource.
78+
* @since 5.4.0
79+
*/
80+
public function jsonSerialize()
81+
{
82+
return get_object_vars($this);
83+
}
84+
85+
/**
86+
* @return int
87+
*/
88+
public function getFlag()
89+
{
90+
return $this->flag;
91+
}
92+
93+
/**
94+
* @return int
95+
*/
96+
public function getSequenceNumber()
97+
{
98+
return $this->sequenceNumber;
99+
}
100+
101+
/**
102+
* @return int
103+
*/
104+
public function getDomainId()
105+
{
106+
return $this->domainId;
107+
}
108+
}

src/MySQLReplication/Event/Event.php

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public function consume()
115115
return (new GtidEvent($eventInfo, $binaryDataReader))->makeGTIDLogDTO();
116116
} else if (ConstEventType::QUERY_EVENT === $eventInfo->getType()) {
117117
return (new QueryEvent($eventInfo, $binaryDataReader))->makeQueryDTO();
118+
} elseif (ConstEventType::MARIA_GTID_EVENT === $eventInfo->getType()) {
119+
return (new MariaDbGtidEvent($eventInfo, $binaryDataReader))->makeMariaDbGTIDLogDTO();
118120
}
119121

120122
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace MySQLReplication\Event;
4+
5+
use MySQLReplication\Event\DTO\MariaDbGtidLogDTO;
6+
7+
/**
8+
* Class MariaGtidEvent
9+
* @package MySQLReplication\Event
10+
*/
11+
class MariaDbGtidEvent extends EventCommon
12+
{
13+
/**
14+
* @return MariaDbGtidLogDTO
15+
*/
16+
public function makeMariaDbGTIDLogDTO()
17+
{
18+
$sequenceNumber = $this->binaryDataReader->readUInt64();
19+
$domainId = $this->binaryDataReader->readUInt32();
20+
$flag = $this->binaryDataReader->readUInt8();
21+
22+
return new MariaDbGtidLogDTO(
23+
$this->eventInfo,
24+
$flag,
25+
$domainId,
26+
$sequenceNumber
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)