rkr/db-sync
Composer 安装命令:
composer require rkr/db-sync
包简介
A sync-tool for mysql-like databases
README 文档
README
Overview
- Synchronize data from one PDO-Connection to another.
- No changed will be made to data that is equal on both connections.
- Every table in the database must have a primary key.
- Detection of new or missing rows in the destination database is based in primary-keys.
- Detection of changed rows is made by a some Hashing-Algorithm where all values of columns outside a primary key form a hash.
- Datasets will be synchronized in chunks, so the vertical size of a table should not matter.
Quick-Start
This project was started to have a tool to quickly pull data from specific tables into a local dev database. The goal of the project is not to have some kind of command line tool to synchronize databases. For that there are tools like pt-table-sync. Is it rather meant to address more complex scenarios that could hardly be modelled by a cli-interface.
Important
This project is not intended to sync two production servers. It is meant to sync some data from a staging or production server back to local dev machines.
Even though it is not necessary, since db-sync will not write into the source database in any circumstance, it is recommended to use a db-user on the source database, that is configured to be read-only. Just to be clear.
It is intended that only data gets synchronized. The table structure should be synched in the form of migration scripts and is outside the scope of this project.
The most basic script for a source mariadb 10.2.3+ to a destination mariadb 10.2.3+ could look like this:
<?php use Kir\DBSync\DBTable; use Kir\DBSync\PDOWrapper; use Kir\DBSync\DBSyncData; use Kir\DBSync\DBEngines\MariaDBEngine; use Logger\Loggers\ResourceLogger; use Logger\Formatters\TemplateFormatter; require 'vendor/autoload.php'; // Bring your own PSR-Logger. This is from logger/essentials (packagist) $logger = new TemplateFormatter(new ResourceLogger(STDOUT)); $pdoOptions = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_COMPRESS => true]; $sourcePDOConn = new PDO('mysql:host=127.0.0.1;port=3312;dbname=some_db_name;charset=utf8', 'readonly1', null, $pdoOptions); $sourceDBEngine = new MariaDBEngine(new PDOWrapper($sourcePDOConn)); $destPDOConn = new PDO('mysql:host=127.0.0.1;port=3306;dbname=some_db_name;charset=utf8', 'root', null, $pdoOptions); $destDBEngine = new MariaDBEngine(new PDOWrapper($destPDOConn)); $tables = $sourceDBEngine->getTableProvider()->getAllTables(); $tables = array_filter($tables, fn(DBTable $table) => strpos($table->name, 'shop__stats_') !== 0); $syncData = new DBSyncData($logger); foreach($tables as $table) { $logger->info($table->name); try { // Set the last flag to true to ignore duplicate-key insert errors $syncData->syncTwoTablesFromDifferentConnections($table, $sourceDBEngine, $destDBEngine, null, options: [ 'ignoreUniqueConstraintViolations' => true ]); } catch (PDOException $e) { $logger->error($e->getMessage()); } }
rkr/db-sync 适用场景与选型建议
rkr/db-sync 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 05 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rkr/db-sync 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rkr/db-sync 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-03