inigopascall/clone-db 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

inigopascall/clone-db

Composer 安装命令:

composer require inigopascall/clone-db

包简介

Package for cloning medium-size databases by ordering tables by foreign-key dependencies and running batch inserts

README 文档

README

This package was created as a solution to the problem of cloning medium/large databases between connections, for example pulling in a live database (or a subet of one) to staging or development environments where there is a need to play with real data. Large sql dump files are often too big to import in one go and can hit issues with memory limits (packet size), especially on micro servers. While this is an old problem, there are still surprisingly few straightforward ways to go about it. This package is an attempt at an out-of-the-box solution in the form of a Laravel package which provides a new artisan command for cloning one database to another.

  • Attempts to preserve data integrity and avoid foreign-key constraint errors by automatically ordering tables by foreign key dependencies, creating the 'master' tables first, and dependent tables sequentially afterwards, ignoring new rows added to the latter tables since the script was initiated. (Note that mutually-dependent data sets are not supported).
  • Avoids php/mysql memory limits by 'chunking' data, i.e. selecting & inserting in configurable batch sizes.

Obviously, running this script is destructive. Please ensure backups of all relevant databases have been taken before starting.

Instructions

composer require inigopascall/clone-db

php artisan vendor:publish --provider="InigoPascall\CloneDB\Providers\CloneDBProvider"

1. Configure the config file as needed

app/config/clone-db.php

Sensible defaults have been pre-set, with examples given in the comments in this file of how to fine-tune things to suit your needs.

2. Make sure the source & target databases are configured correctly in your app's config

app/config/database.php

'my_source_db_connection' => [
	'driver' => 'mysql',
	'host' => env('SOURCE_DB_HOST', '127.0.0.1'),
	'port' => env('SOURCE_DB_PORT', '3306'),
	'database' => env('SOURCE_DB_DATABASE', 'forge'),
	'username' => env('SOURCE_DB_USERNAME', 'forge'),
	'password' => env('SOURCE_DB_PASSWORD', '')
],
'my_target_db_connection' => [
	'driver' => 'mysql',
	'host' => env('TARGET_DB_HOST', '127.0.0.1'),
	'port' => env('TARGET_DB_PORT', '3306'),
	'database' => env('TARGET_DB_DATABASE', 'forge'),
	'username' => env('TARGET_DB_USERNAME', 'forge'),
	'password' => env('TARGET_DB_PASSWORD', '')
]

.env

SOURCE_DB_HOST=localhost
SOURCE_DB_PORT=3306
SOURCE_DB_DATABASE=source_database_name
SOURCE_DB_USERNAME=mysqlusername
SOURCE_DB_PASSWORD=mysluserpass

TARGET_DB_HOST=localhost
TARGET_DB_PORT=3306
TARGET_DB_DATABASE=target_database_name
TARGET_DB_USERNAME=mysqlusername
TARGET_DB_PASSWORD=mysqluserpass

3. run the command with the source & target connection names as parameters. Check & confirm output.

php artisan clone:db my_source_db_connection my_target_db_connection

Cloning a remote database via SSH

SSH config for your server pointing to the private key location

~/.ssh/config

Host remoteservername
Hostname {server ip address}
User username
IdentityFile ~/.ssh/remoteservernameprivatekey

Open SSH connection with ssh remoteservername -N -L 13306:localhost:3306 username@{server ip address}

app/config/database.php entry for the connection would then be set with localhost and the local port number specified in the above command (13306):

'my_source_db_connection' => [
    'driver' => 'mysql',
    'host' => env('SOURCE_DB_HOST', '127.0.0.1'),
    'port' => env('SOURCE_DB_PORT', '13306'),
    'database' => env('SOURCE_DB_DATABASE', 'remotedb'),
    'username' => env('SOURCE_DB_USERNAME', 'remotedbuser'),
    'password' => env('SOURCE_DB_PASSWORD', 'remotedbpass')
]

inigopascall/clone-db 适用场景与选型建议

inigopascall/clone-db 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 81 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 inigopascall/clone-db 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 inigopascall/clone-db 我们能提供哪些服务?
定制开发 / 二次开发

基于 inigopascall/clone-db 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-11-01