MySQL 複製協定的純 PHP 實作。這允許您接收插入、更新、刪除等事件及其資料和原始 SQL 查詢。
創作者為基礎的偉大作品:https://github.com/noplay/python-mysql-replication 和 https://github.com/fengxiangyun/mysql-replication
在你的專案中
composer require krowinski/php-mysql-replication
或獨立的
git clone https://github.com/krowinski/php-mysql-replication.git
composer install -o
PHP
資料庫管理系統
在 MySQL 伺服器設定檔中,您需要啟用複製:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
binlog-format = row #Very important if you want to receive write, update and delete row events
Mysql 複製事件解釋 https://dev.mysql.com/doc/internals/en/event-meanings.html
Mysql使用者權限:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host';
GRANT SELECT ON `dbName`.* TO 'user'@'host';
使用 ConfigBuilder 或 ConfigFactory 建立配置。可用選項:
'user' - 你的 mysql 使用者(強制)
'ip' 或 'host' - 你的 mysql 主機/IP(必填)
'password' - 你的 mysql 密碼(必填)
'port' - 你的 mysql 主機連接埠(預設 3306)
'charset' - 資料庫連接字元集(預設utf8)
'gtid' - GTID 標記(格式 9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592)
'mariaDbGtid' - MariaDB GTID 標籤(格式 1-1-3,0-1-88)
'slaveId' - 用於識別的腳本從機 ID(預設值:666)(顯示從機主機)
'binLogFileName' - 開始的 bin 日誌檔名
'binLogPosition' - bin 日誌位置開始
'eventsOnly' - 監聽事件的陣列(ConstEventType.php 檔案中的完整清單)
'eventsIgnore' - 忽略事件的陣列(ConstEventType.php 檔案中的完整清單)
'tablesOnly' - 僅監聽給定表的陣列(預設所有表)
'databasesOnly' - 僅偵聽給定資料庫的陣列(預設所有資料庫)
'tableCacheSize' - 從資訊模式收集一些數據,這些數據被緩存。
'custom' - 如果某些參數必須在擴展/實現自己的類別中設置
'heartbeatPeriod' - 設定複製心跳之間的時間間隔(以秒為單位)。每當主伺服器的二進位日誌有事件更新時,下一次心跳的等待時間就會重設。間隔是一個十進制值,範圍為 0 到 4294967 秒,分辨率為毫秒;最小的非零值為 0.001。只有當二進位日誌檔案中沒有超過間隔時間的未傳送事件時,主伺服器才會發送心跳。
'saveUuid' - 設定從屬 uuid 用於識別(預設值:0015d2b6-8a06-4e5e-8c07-206ef3fbd274)
紅寶石:https://github.com/y310/kodama
Java:https://github.com/shyiko/mysql-binlog-connector-java
去:https://github.com/siddontang/go-mysql
Python:https://github.com/noplay/python-mysql-replication
.NET:https://github.com/rusuly/MySqlCdc
所有範例都可以在範例目錄中找到
此範例將所有複製事件轉儲到控制台:
請記得更改您的使用者、主機和密碼的配置。
使用者應具有複製權限 [ REPLICATION CLIENT, SELECT]
php example/dump_events.php
對於測試 SQL 事件:
CREATE DATABASE php_mysql_replication ;
use php_mysql_replication;
CREATE TABLE test4 (id int NOT NULL AUTO_INCREMENT, data VARCHAR ( 255 ), data2 VARCHAR ( 255 ), PRIMARY KEY (id));
INSERT INTO test4 (data,data2) VALUES ( " Hello " , " World " );
UPDATE test4 SET data = " World " , data2 = " Hello " WHERE id = 1 ;
DELETE FROM test4 WHERE id = 1 ;
輸出將與此類似(取決於配置,例如 GTID 關閉/開啟):
=== Event format description ===
Date: 2017-07-06T13:31:11+00:00
Log position: 0
Event size: 116
Memory usage 2.4 MB
=== Event gtid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803092
Event size: 48
Commit: true
GTID NEXT: 3403c535-624f-11e7-9940-0800275713ee:13675
Memory usage 2.42 MB
=== Event query ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803237
Event size: 145
Database: php_mysql_replication
Execution time: 0
Query: CREATE DATABASE php_mysql_replication
Memory usage 2.45 MB
=== Event gtid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803285
Event size: 48
Commit: true
GTID NEXT: 3403c535-624f-11e7-9940-0800275713ee:13676
Memory usage 2.45 MB
=== Event query ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803500
Event size: 215
Database: php_mysql_replication
Execution time: 0
Query: CREATE TABLE test4 (id int NOT NULL AUTO_INCREMENT, data VARCHAR(255), data2 VARCHAR(255), PRIMARY KEY(id))
Memory usage 2.45 MB
=== Event gtid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803548
Event size: 48
Commit: true
GTID NEXT: 3403c535-624f-11e7-9940-0800275713ee:13677
Memory usage 2.45 MB
=== Event query ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803637
Event size: 89
Database: php_mysql_replication
Execution time: 0
Query: BEGIN
Memory usage 2.45 MB
=== Event tableMap ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803708
Event size: 71
Table: test4
Database: php_mysql_replication
Table Id: 866
Columns amount: 3
Memory usage 2.71 MB
=== Event write ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803762
Event size: 54
Table: test4
Affected columns: 3
Changed rows: 1
Values: Array
(
[0] => Array
(
[id] => 1
[data] => Hello
[data2] => World
)
)
Memory usage 2.74 MB
=== Event xid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803793
Event size: 31
Transaction ID: 662802
Memory usage 2.75 MB
=== Event gtid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803841
Event size: 48
Commit: true
GTID NEXT: 3403c535-624f-11e7-9940-0800275713ee:13678
Memory usage 2.75 MB
=== Event query ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57803930
Event size: 89
Database: php_mysql_replication
Execution time: 0
Query: BEGIN
Memory usage 2.76 MB
=== Event tableMap ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804001
Event size: 71
Table: test4
Database: php_mysql_replication
Table Id: 866
Columns amount: 3
Memory usage 2.75 MB
=== Event update ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804075
Event size: 74
Table: test4
Affected columns: 3
Changed rows: 1
Values: Array
(
[0] => Array
(
[before] => Array
(
[id] => 1
[data] => Hello
[data2] => World
)
[after] => Array
(
[id] => 1
[data] => World
[data2] => Hello
)
)
)
Memory usage 2.76 MB
=== Event xid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804106
Event size: 31
Transaction ID: 662803
Memory usage 2.76 MB
=== Event gtid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804154
Event size: 48
Commit: true
GTID NEXT: 3403c535-624f-11e7-9940-0800275713ee:13679
Memory usage 2.76 MB
=== Event query ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804243
Event size: 89
Database: php_mysql_replication
Execution time: 0
Query: BEGIN
Memory usage 2.76 MB
=== Event tableMap ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804314
Event size: 71
Table: test4
Database: php_mysql_replication
Table Id: 866
Columns amount: 3
Memory usage 2.76 MB
=== Event delete ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804368
Event size: 54
Table: test4
Affected columns: 3
Changed rows: 1
Values: Array
(
[0] => Array
(
[id] => 1
[data] => World
[data2] => Hello
)
)
Memory usage 2.77 MB
=== Event xid ===
Date: 2017-07-06T15:23:44+00:00
Log position: 57804399
Event size: 31
Transaction ID: 662804
Memory usage 2.77 MB
在虛擬機器上測試
Debian 8.7
PHP 5.6.30
Percona 5.6.35
inxi
CPU(s)~4 Single core Intel Core i5-2500Ks (-SMP-) clocked at 5901 Mhz Kernel~3.16.0-4-amd64 x86_64 Up~1 day Mem~1340.3/1996.9MB HDD~41.9GB(27.7% used) Procs~122 Client~Shell inxi~2.1.28
php example/benchmark.php
Start insert data
7442 event by seconds (1000 total)
7679 event by seconds (2000 total)
7914 event by seconds (3000 total)
7904 event by seconds (4000 total)
7965 event by seconds (5000 total)
8006 event by seconds (6000 total)
8048 event by seconds (7000 total)
8038 event by seconds (8000 total)
8040 event by seconds (9000 total)
8055 event by seconds (10000 total)
8058 event by seconds (11000 total)
8071 event by seconds (12000 total)
首先,MYSQL 不會給你非同步呼叫。您通常需要在應用程式中對此進行程式設計(透過事件分派並新增至某些佇列系統,如果您的資料庫有許多入口點,例如Web、後端其他微服務,則會在所有這些微服務中新增處理並不總是便宜的。引擎複製、即時分析和審計。
首先,您需要知道可能會發生很多事件,例如,如果您更新表“bar”中的 1 000 000 筆記錄,並且您需要從表“foo”中插入一條記錄,那麼所有事件都必須由腳本處理,並且您需要等待數據。這是正常現象,也是。您可以使用配置選項來加快速度。另外,如果腳本崩潰,您需要不時從 binlog(或 gtid)中儲存位置,以便在再次執行此腳本時從該位置開始,以避免重複。
就像我在1點使用佇列系統(如rabbitmq、redis或kafka)中提到的那樣,它們將使您能夠在多個腳本中處理資料。
創建一個問題,我會在空閒時間嘗試解決它:)
它像從屬模式下的任何其他 MYSQL 一樣工作,並且提供相同的開銷。
解決這個問題的最佳方法是將資料庫配置net_read_timeout
和net_write_timeout
增加到 3600。
在 my.conf binlog_row_image=full
中設定以修復僅接收部分更新的問題。
在 my.conf 中設定log_slave_updates=on
來修復此問題 (#71)(#66)
預設 MYSQL 設定產生一大塊流,這需要更多 RAM/CPU,您可以使用變數binlog_row_event_max_size
更改此設定以獲得較小的流[https://dev.mysql.com/doc/refman/8.0/en/ replication-options-binary- log.html#sysvar_binlog_row_event_max_size] 分割成更小的區塊