承接 rkr/db-sync 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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());
	}
}

统计信息

  • 总下载量: 1.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固