Skip to content

Commit 5291f69

Browse files
authored
next update (#19)
* Documentation update * MySQLRepository and BinLogSocketConnect extract to interfaces * BinLogSocketConnect exception set as const * register slave use now hostname and port to be correct display in "SHOW SLAVE HOSTS" * dbname removed from configuration as is no more in use * added foreign keys info to events
1 parent 9d13140 commit 5291f69

19 files changed

+1361
-1198
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,37 @@ Mysql replication events explained
4343
Configuration
4444
=========
4545

46-
You can pass this array keys to ConfigService->makeConfigFromArray([])
46+
You can pass this array keys to ConfigService->makeConfigFromArray([]) or use ConfigBuilder to generate config.
4747

4848
'user' - your mysql user (mandatory)
4949

50-
'ip' - your mysql host ip (mandatory)
50+
'ip' or 'host' - your mysql host/ip (mandatory)
5151

5252
'password' - your mysql password (mandatory)
5353

54-
'port' - your mysql host port
54+
'port' - your mysql host port (default 3306)
5555

56-
'dbName' - db name you want to listen
57-
58-
'charset' - db connection charset
56+
'charset' - db connection charset (default utf8)
5957

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

6260
'mariaDbGtid' - MariaDB GTID marker(s) to start from (format 1-1-3,0-1-88)
6361

64-
'slaveId' - script slave id for identification
62+
'slaveId' - script slave id for identification (SHOW SLAVE HOSTS)
6563

6664
'binLogFileName' - bin log file name to start from
6765

6866
'binLogPosition' - bin log position to start from
6967

7068
'eventsOnly' - array to listen on events (full list in [ConstEventType.php](https://github.com/krowinski/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
7169

72-
'eventsIgnore' - array yo ignore events (full list in [ConstEventType.php](https://github.com/krowinski/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
70+
'eventsIgnore' - array to ignore events (full list in [ConstEventType.php](https://github.com/krowinski/php-mysql-replication/blob/master/src/MySQLReplication/Definitions/ConstEventType.php) file)
7371

74-
'tablesOnly' - array to only listen on given tables
72+
'tablesOnly' - array to only listen on given tables (default all tables)
7573

76-
'databasesOnly' - array to only listen on given databases
74+
'databasesOnly' - array to only listen on given databases (default all databases)
75+
76+
'tableCacheSize' - some data are collected from information schema, this data is cached. This variable set cache for tables bigger takes more memory. (default 128 objects)
7777

7878

7979
Examples

src/MySQLReplication/BinLog/BinLogConnect.php renamed to src/MySQLReplication/BinLog/BinLogSocketConnect.php

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
use MySQLReplication\Config\Config;
77
use MySQLReplication\Definitions\ConstCapabilityFlags;
88
use MySQLReplication\Definitions\ConstCommand;
9+
use MySQLReplication\Gtid\GtidException;
910
use MySQLReplication\Gtid\GtidService;
10-
use MySQLReplication\Repository\MySQLRepository;
11+
use MySQLReplication\Repository\RepositoryInterface;
1112

1213
/**
13-
* Class BinLogConnect
14+
* Class BinLogSocketConnect
1415
* @package MySQLReplication\BinLog
1516
*/
16-
class BinLogConnect
17+
class BinLogSocketConnect implements BinLogSocketConnectInterface
1718
{
1819
/**
1920
* @var resource
@@ -24,9 +25,9 @@ class BinLogConnect
2425
*/
2526
private $checkSum = false;
2627
/**
27-
* @var MySQLRepository
28+
* @var RepositoryInterface
2829
*/
29-
private $mySQLRepository;
30+
private $repository;
3031
/**
3132
* @var Config
3233
*/
@@ -47,17 +48,17 @@ class BinLogConnect
4748

4849
/**
4950
* @param Config $config
50-
* @param MySQLRepository $mySQLRepository
51+
* @param RepositoryInterface $repository
5152
* @param BinLogAuth $packAuth
5253
* @param GtidService $gtidService
5354
*/
5455
public function __construct(
5556
Config $config,
56-
MySQLRepository $mySQLRepository,
57+
RepositoryInterface $repository,
5758
BinLogAuth $packAuth,
5859
GtidService $gtidService
5960
) {
60-
$this->mySQLRepository = $mySQLRepository;
61+
$this->repository = $repository;
6162
$this->config = $config;
6263
$this->packAuth = $packAuth;
6364
$this->gtidService = $gtidService;
@@ -95,7 +96,7 @@ public function connectToStream()
9596
{
9697
if (false === ($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
9798
{
98-
throw new BinLogException('Unable to create a socket:' . socket_strerror(socket_last_error()), socket_last_error());
99+
throw new BinLogException(BinLogException::UNABLE_TO_CREATE_SOCKET. socket_strerror(socket_last_error()), socket_last_error());
99100
}
100101
socket_set_block($this->socket);
101102
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
@@ -116,7 +117,7 @@ public function connectToStream()
116117
private function serverInfo()
117118
{
118119
BinLogServerInfo::parsePackage($this->getPacket(false));
119-
BinLogServerInfo::parseVersion($this->mySQLRepository->getVersion());
120+
BinLogServerInfo::parseVersion($this->repository->getVersion());
120121
}
121122

122123
/**
@@ -143,8 +144,8 @@ public function getPacket($checkForOkByte = true)
143144
}
144145

145146
/**
146-
* @param $length
147-
* @return mixed
147+
* @param int $length
148+
* @return string
148149
* @throws BinLogException
149150
*/
150151
private function readFromSocket($length)
@@ -158,14 +159,14 @@ private function readFromSocket($length)
158159
// http://php.net/manual/pl/function.socket-recv.php#47182
159160
if (0 === $received)
160161
{
161-
throw new BinLogException('Disconnected by remote side');
162+
throw new BinLogException(BinLogException::DISCONNECTED_MESSAGE);
162163
}
163164

164165
throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
165166
}
166167

167168
/**
168-
* @param $packet
169+
* @param string $packet
169170
* @return array
170171
* @throws BinLogException
171172
*/
@@ -207,23 +208,24 @@ private function auth()
207208
}
208209

209210
/**
210-
* @param $data
211+
* @param string $data
211212
* @throws BinLogException
212213
*/
213214
private function writeToSocket($data)
214215
{
215216
if (false === socket_write($this->socket, $data, strlen($data)))
216217
{
217-
throw new BinLogException('Unable to write to socket: ' . socket_strerror(socket_last_error()), socket_last_error());
218+
throw new BinLogException(BinLogException::UNABLE_TO_WRITE_SOCKET . socket_strerror(socket_last_error()), socket_last_error());
218219
}
219220
}
220221

221222
/**
222223
* @throws BinLogException
224+
* @throws GtidException
223225
*/
224226
private function getBinlogStream()
225227
{
226-
$this->checkSum = $this->mySQLRepository->isCheckSum();
228+
$this->checkSum = $this->repository->isCheckSum();
227229
if (true === $this->checkSum)
228230
{
229231
$this->execute('SET @master_binlog_checksum=@@global.binlog_checksum');
@@ -260,14 +262,23 @@ private function execute($sql)
260262
*/
261263
private function registerSlave()
262264
{
263-
$prelude = pack('l', 18) . chr(ConstCommand::COM_REGISTER_SLAVE);
264-
$prelude .= pack('I', $this->config->getSlaveId());
265-
$prelude .= chr(0);
266-
$prelude .= chr(0);
267-
$prelude .= chr(0);
268-
$prelude .= pack('s', '');
269-
$prelude .= pack('I', 0);
270-
$prelude .= pack('I', 0);
265+
$host = gethostname();
266+
$hostLength = strlen($host);
267+
$userLength = strlen($this->config->getUser());
268+
$passLength = strlen($this->config->getPassword());
269+
270+
$prelude = pack('l', 18 + $hostLength + $userLength + $passLength);
271+
$prelude .= chr(ConstCommand::COM_REGISTER_SLAVE);
272+
$prelude .= pack('V', $this->config->getSlaveId());
273+
$prelude .= pack('C', $hostLength);
274+
$prelude .= $host;
275+
$prelude .= pack('C', $userLength);
276+
$prelude .= $this->config->getUser();
277+
$prelude .= pack('C', $passLength);
278+
$prelude .= $this->config->getPassword();
279+
$prelude .= pack('v', $this->config->getPort());
280+
$prelude .= pack('V', 0);
281+
$prelude .= pack('V', 0);
271282

272283
$this->writeToSocket($prelude);
273284
$this->getPacket();
@@ -276,6 +287,7 @@ private function registerSlave()
276287
/**
277288
* @see https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html
278289
* @throws BinLogException
290+
* @throws GtidException
279291
*/
280292
private function setBinLogDumpGtid()
281293
{
@@ -316,7 +328,7 @@ private function setBinLogDump()
316328

317329
if (0 === $binFilePos || '' === $binFileName)
318330
{
319-
$master = $this->mySQLRepository->getMasterStatus();
331+
$master = $this->repository->getMasterStatus();
320332
$binFilePos = $master['Position'];
321333
$binFileName = $master['File'];
322334
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace MySQLReplication\BinLog;
3+
4+
use MySQLReplication\BinLog\Exception\BinLogException;
5+
6+
7+
/**
8+
* Class SocketConnect
9+
* @package MySQLReplication\BinLog
10+
*/
11+
interface BinLogSocketConnectInterface
12+
{
13+
/**
14+
* @return bool
15+
*/
16+
public function isConnected();
17+
18+
/**
19+
* @return bool
20+
*/
21+
public function getCheckSum();
22+
23+
/**
24+
* @throws BinLogException
25+
*/
26+
public function connectToStream();
27+
28+
/**
29+
* @param bool $checkForOkByte
30+
* @return string
31+
* @throws BinLogException
32+
*/
33+
public function getPacket($checkForOkByte = true);
34+
35+
/**
36+
* @param string $packet
37+
* @return array
38+
* @throws BinLogException
39+
*/
40+
public function isWriteSuccessful($packet);
41+
}

src/MySQLReplication/BinLog/Exception/BinLogException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
*/
1111
class BinLogException extends MySQLReplicationException
1212
{
13+
const DISCONNECTED_MESSAGE = 'Disconnected by remote side';
14+
const UNABLE_TO_WRITE_SOCKET = 'Unable to write to socket: ';
15+
const UNABLE_TO_CREATE_SOCKET = 'Unable to create socket: ';
16+
1317
}

src/MySQLReplication/BinaryDataReader/BinaryDataReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function readUInt24()
145145
}
146146

147147
/**
148-
* @return int
148+
* @return string
149149
*/
150150
public function readUInt64()
151151
{

src/MySQLReplication/Config/Config.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ class Config
2626
* @var string
2727
*/
2828
private $password;
29-
/**
30-
* @var string
31-
*/
32-
private $dbName;
3329
/**
3430
* @var string
3531
*/
@@ -81,7 +77,6 @@ class Config
8177
* @param string $host
8278
* @param int $port
8379
* @param string $password
84-
* @param string $dbName
8580
* @param string $charset
8681
* @param string $gtid
8782
* @param string $mariaGtid
@@ -99,7 +94,6 @@ public function __construct(
9994
$host,
10095
$port,
10196
$password,
102-
$dbName,
10397
$charset,
10498
$gtid,
10599
$mariaGtid,
@@ -116,7 +110,6 @@ public function __construct(
116110
$this->host = $host;
117111
$this->port = $port;
118112
$this->password = $password;
119-
$this->dbName = $dbName;
120113
$this->charset = $charset;
121114
$this->gtid = $gtid;
122115
$this->slaveId = $slaveId;
@@ -155,10 +148,6 @@ public function validate()
155148
{
156149
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE);
157150
}
158-
if (!empty($this->dbName) && false === is_string($this->dbName))
159-
{
160-
throw new ConfigException(ConfigException::DB_NAME_ERROR_MESSAGE, ConfigException::DB_NAME_ERROR_CODE);
161-
}
162151
if (!empty($this->charset) && false === is_string($this->charset))
163152
{
164153
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE);
@@ -227,14 +216,6 @@ public function getPassword()
227216
return $this->password;
228217
}
229218

230-
/**
231-
* @return string
232-
*/
233-
public function getDbName()
234-
{
235-
return $this->dbName;
236-
}
237-
238219
/**
239220
* @return string
240221
*/

src/MySQLReplication/Config/ConfigBuilder.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ class ConfigBuilder
2424
* @var string
2525
*/
2626
private $password = '';
27-
/**
28-
* @var string
29-
*/
30-
private $dbName = '';
3127
/**
3228
* @var string
3329
*/
@@ -117,17 +113,6 @@ public function withPassword($password)
117113
return $this;
118114
}
119115

120-
/**
121-
* @param string $dbName
122-
* @return ConfigBuilder
123-
*/
124-
public function withDbName($dbName)
125-
{
126-
$this->dbName = $dbName;
127-
128-
return $this;
129-
}
130-
131116
/**
132117
* @param string $charset
133118
* @return ConfigBuilder
@@ -187,7 +172,7 @@ public function withBinLogPosition($binLogPosition)
187172
* @param array $eventsOnly
188173
* @return ConfigBuilder
189174
*/
190-
public function withEventsOnly($eventsOnly)
175+
public function withEventsOnly(array $eventsOnly)
191176
{
192177
$this->eventsOnly = $eventsOnly;
193178

@@ -256,7 +241,6 @@ public function build()
256241
$this->host,
257242
$this->port,
258243
$this->password,
259-
$this->dbName,
260244
$this->charset,
261245
$this->gtid,
262246
$this->mariaDbGtid,

0 commit comments

Comments
 (0)