Skip to content

next update #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,37 @@ Mysql replication events explained
Configuration
=========

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

'user' - your mysql user (mandatory)

'ip' - your mysql host ip (mandatory)
'ip' or 'host' - your mysql host/ip (mandatory)

'password' - your mysql password (mandatory)

'port' - your mysql host port
'port' - your mysql host port (default 3306)

'dbName' - db name you want to listen

'charset' - db connection charset
'charset' - db connection charset (default utf8)

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

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

'slaveId' - script slave id for identification
'slaveId' - script slave id for identification (SHOW SLAVE HOSTS)

'binLogFileName' - bin log file name to start from

'binLogPosition' - bin log position to start from

'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)

'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)
'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)

'tablesOnly' - array to only listen on given tables
'tablesOnly' - array to only listen on given tables (default all tables)

'databasesOnly' - array to only listen on given databases
'databasesOnly' - array to only listen on given databases (default all databases)

'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)


Examples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use MySQLReplication\Config\Config;
use MySQLReplication\Definitions\ConstCapabilityFlags;
use MySQLReplication\Definitions\ConstCommand;
use MySQLReplication\Gtid\GtidException;
use MySQLReplication\Gtid\GtidService;
use MySQLReplication\Repository\MySQLRepository;
use MySQLReplication\Repository\RepositoryInterface;

/**
* Class BinLogConnect
* Class BinLogSocketConnect
* @package MySQLReplication\BinLog
*/
class BinLogConnect
class BinLogSocketConnect implements BinLogSocketConnectInterface
{
/**
* @var resource
Expand All @@ -24,9 +25,9 @@ class BinLogConnect
*/
private $checkSum = false;
/**
* @var MySQLRepository
* @var RepositoryInterface
*/
private $mySQLRepository;
private $repository;
/**
* @var Config
*/
Expand All @@ -47,17 +48,17 @@ class BinLogConnect

/**
* @param Config $config
* @param MySQLRepository $mySQLRepository
* @param RepositoryInterface $repository
* @param BinLogAuth $packAuth
* @param GtidService $gtidService
*/
public function __construct(
Config $config,
MySQLRepository $mySQLRepository,
RepositoryInterface $repository,
BinLogAuth $packAuth,
GtidService $gtidService
) {
$this->mySQLRepository = $mySQLRepository;
$this->repository = $repository;
$this->config = $config;
$this->packAuth = $packAuth;
$this->gtidService = $gtidService;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function connectToStream()
{
if (false === ($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)))
{
throw new BinLogException('Unable to create a socket:' . socket_strerror(socket_last_error()), socket_last_error());
throw new BinLogException(BinLogException::UNABLE_TO_CREATE_SOCKET. socket_strerror(socket_last_error()), socket_last_error());
}
socket_set_block($this->socket);
socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1);
Expand All @@ -116,7 +117,7 @@ public function connectToStream()
private function serverInfo()
{
BinLogServerInfo::parsePackage($this->getPacket(false));
BinLogServerInfo::parseVersion($this->mySQLRepository->getVersion());
BinLogServerInfo::parseVersion($this->repository->getVersion());
}

/**
Expand All @@ -143,8 +144,8 @@ public function getPacket($checkForOkByte = true)
}

/**
* @param $length
* @return mixed
* @param int $length
* @return string
* @throws BinLogException
*/
private function readFromSocket($length)
Expand All @@ -158,14 +159,14 @@ private function readFromSocket($length)
// http://php.net/manual/pl/function.socket-recv.php#47182
if (0 === $received)
{
throw new BinLogException('Disconnected by remote side');
throw new BinLogException(BinLogException::DISCONNECTED_MESSAGE);
}

throw new BinLogException(socket_strerror(socket_last_error()), socket_last_error());
}

/**
* @param $packet
* @param string $packet
* @return array
* @throws BinLogException
*/
Expand Down Expand Up @@ -207,23 +208,24 @@ private function auth()
}

/**
* @param $data
* @param string $data
* @throws BinLogException
*/
private function writeToSocket($data)
{
if (false === socket_write($this->socket, $data, strlen($data)))
{
throw new BinLogException('Unable to write to socket: ' . socket_strerror(socket_last_error()), socket_last_error());
throw new BinLogException(BinLogException::UNABLE_TO_WRITE_SOCKET . socket_strerror(socket_last_error()), socket_last_error());
}
}

/**
* @throws BinLogException
* @throws GtidException
*/
private function getBinlogStream()
{
$this->checkSum = $this->mySQLRepository->isCheckSum();
$this->checkSum = $this->repository->isCheckSum();
if (true === $this->checkSum)
{
$this->execute('SET @master_binlog_checksum=@@global.binlog_checksum');
Expand Down Expand Up @@ -260,14 +262,23 @@ private function execute($sql)
*/
private function registerSlave()
{
$prelude = pack('l', 18) . chr(ConstCommand::COM_REGISTER_SLAVE);
$prelude .= pack('I', $this->config->getSlaveId());
$prelude .= chr(0);
$prelude .= chr(0);
$prelude .= chr(0);
$prelude .= pack('s', '');
$prelude .= pack('I', 0);
$prelude .= pack('I', 0);
$host = gethostname();
$hostLength = strlen($host);
$userLength = strlen($this->config->getUser());
$passLength = strlen($this->config->getPassword());

$prelude = pack('l', 18 + $hostLength + $userLength + $passLength);
$prelude .= chr(ConstCommand::COM_REGISTER_SLAVE);
$prelude .= pack('V', $this->config->getSlaveId());
$prelude .= pack('C', $hostLength);
$prelude .= $host;
$prelude .= pack('C', $userLength);
$prelude .= $this->config->getUser();
$prelude .= pack('C', $passLength);
$prelude .= $this->config->getPassword();
$prelude .= pack('v', $this->config->getPort());
$prelude .= pack('V', 0);
$prelude .= pack('V', 0);

$this->writeToSocket($prelude);
$this->getPacket();
Expand All @@ -276,6 +287,7 @@ private function registerSlave()
/**
* @see https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html
* @throws BinLogException
* @throws GtidException
*/
private function setBinLogDumpGtid()
{
Expand Down Expand Up @@ -316,7 +328,7 @@ private function setBinLogDump()

if (0 === $binFilePos || '' === $binFileName)
{
$master = $this->mySQLRepository->getMasterStatus();
$master = $this->repository->getMasterStatus();
$binFilePos = $master['Position'];
$binFileName = $master['File'];
}
Expand Down
41 changes: 41 additions & 0 deletions src/MySQLReplication/BinLog/BinLogSocketConnectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace MySQLReplication\BinLog;

use MySQLReplication\BinLog\Exception\BinLogException;


/**
* Class SocketConnect
* @package MySQLReplication\BinLog
*/
interface BinLogSocketConnectInterface
{
/**
* @return bool
*/
public function isConnected();

/**
* @return bool
*/
public function getCheckSum();

/**
* @throws BinLogException
*/
public function connectToStream();

/**
* @param bool $checkForOkByte
* @return string
* @throws BinLogException
*/
public function getPacket($checkForOkByte = true);

/**
* @param string $packet
* @return array
* @throws BinLogException
*/
public function isWriteSuccessful($packet);
}
4 changes: 4 additions & 0 deletions src/MySQLReplication/BinLog/Exception/BinLogException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
*/
class BinLogException extends MySQLReplicationException
{
const DISCONNECTED_MESSAGE = 'Disconnected by remote side';
const UNABLE_TO_WRITE_SOCKET = 'Unable to write to socket: ';
const UNABLE_TO_CREATE_SOCKET = 'Unable to create socket: ';

}
2 changes: 1 addition & 1 deletion src/MySQLReplication/BinaryDataReader/BinaryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function readUInt24()
}

/**
* @return int
* @return string
*/
public function readUInt64()
{
Expand Down
19 changes: 0 additions & 19 deletions src/MySQLReplication/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class Config
* @var string
*/
private $password;
/**
* @var string
*/
private $dbName;
/**
* @var string
*/
Expand Down Expand Up @@ -81,7 +77,6 @@ class Config
* @param string $host
* @param int $port
* @param string $password
* @param string $dbName
* @param string $charset
* @param string $gtid
* @param string $mariaGtid
Expand All @@ -99,7 +94,6 @@ public function __construct(
$host,
$port,
$password,
$dbName,
$charset,
$gtid,
$mariaGtid,
Expand All @@ -116,7 +110,6 @@ public function __construct(
$this->host = $host;
$this->port = $port;
$this->password = $password;
$this->dbName = $dbName;
$this->charset = $charset;
$this->gtid = $gtid;
$this->slaveId = $slaveId;
Expand Down Expand Up @@ -155,10 +148,6 @@ public function validate()
{
throw new ConfigException(ConfigException::PASSWORD_ERROR_MESSAGE, ConfigException::PASSWORD_ERROR_CODE);
}
if (!empty($this->dbName) && false === is_string($this->dbName))
{
throw new ConfigException(ConfigException::DB_NAME_ERROR_MESSAGE, ConfigException::DB_NAME_ERROR_CODE);
}
if (!empty($this->charset) && false === is_string($this->charset))
{
throw new ConfigException(ConfigException::CHARSET_ERROR_MESSAGE, ConfigException::CHARSET_ERROR_CODE);
Expand Down Expand Up @@ -227,14 +216,6 @@ public function getPassword()
return $this->password;
}

/**
* @return string
*/
public function getDbName()
{
return $this->dbName;
}

/**
* @return string
*/
Expand Down
18 changes: 1 addition & 17 deletions src/MySQLReplication/Config/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ class ConfigBuilder
* @var string
*/
private $password = '';
/**
* @var string
*/
private $dbName = '';
/**
* @var string
*/
Expand Down Expand Up @@ -117,17 +113,6 @@ public function withPassword($password)
return $this;
}

/**
* @param string $dbName
* @return ConfigBuilder
*/
public function withDbName($dbName)
{
$this->dbName = $dbName;

return $this;
}

/**
* @param string $charset
* @return ConfigBuilder
Expand Down Expand Up @@ -187,7 +172,7 @@ public function withBinLogPosition($binLogPosition)
* @param array $eventsOnly
* @return ConfigBuilder
*/
public function withEventsOnly($eventsOnly)
public function withEventsOnly(array $eventsOnly)
{
$this->eventsOnly = $eventsOnly;

Expand Down Expand Up @@ -256,7 +241,6 @@ public function build()
$this->host,
$this->port,
$this->password,
$this->dbName,
$this->charset,
$this->gtid,
$this->mariaDbGtid,
Expand Down
Loading