yeora / db-syncer
DbSyncer
1.2.8
2022-09-03 19:25 UTC
Requires
- php: >=7.4
- ifsnop/mysqldump-php: ^2.9
- symfony/console: ^6.1
- symfony/yaml: ^6.1
Requires (Dev)
- phpstan/phpstan: ^1.8
README
简要描述
DbSyncer是一个旨在简化数据库之间同步的工具。作为中间件,DbSyncer使用ifsnop/mysqldump-php包。
DbSyncer提供了1对1同步数据库模式和数据库本身,以及可以在表和列级别进行配置的同步功能。
例如,使用DbSyncer,可以将生产数据库传输到本地开发数据库,无需进一步手动调整数据。
- 可以在预先处理中屏蔽或修改敏感客户数据。
- 使用固定值覆盖或替换配置,以便它们适合目标环境。
- 在表级别设置限制,以便只选择有限的数据量。
- 在表级别设置条件,以便只选择某些数据。
- ...
需求
- PHP >= 7.4.0
- MySQL >= 4.1.0
- PDO
安装 & 设置(快速入门)
DbSyncer的安装是通过Composer完成的
composer require yeora/db-syncer --dev
现在需要创建一个配置文件,其中包含访问数据等。对于简单的标准配置文件,可以使用以下命令:
vendor/bin/dbSyncer init
DbSyncer将在项目根目录中创建一个DbSyncer.yaml配置文件,如下所示:
--- syncFroms: - credentials: hostname: YOUR_DB_HOSTNAME port: YOUR_DB_PORT username: YOUR_DB_USERNAME password: YOUR_DB_PASSWORD database: YOUR_DB_DATABASENAME syncTos: - credentials: hostname: YOUR_DB_HOSTNAME port: YOUR_DB_PORT username: YOUR_DB_USERNAME password: YOUR_DB_PASSWORD database: YOUR_DB_DATABASENAME
在此简单示例中,仅预配置了syncFrom和syncTo。syncFrom包含从其中执行同步的数据库所需的所有信息。syncTo包含要执行同步的数据库所需的所有信息。可以配置任意数量的syncFroms和syncTos。
下一步是输入访问数据。密码和用户名密钥值也可以完全从配置文件中删除。凭证将在启动同步器时交互式获取。
在输入访问数据或删除密钥值后,可以启动实际的同步过程。为此,必须执行以下命令
vendor/bin/dbSyncer sync
如果只配置了一个syncFrom和一个syncTo,并且指定了访问数据,则同步将自动进行,无需进一步的用户操作。
如果配置了多个syncFrom,则将交互式选择一个。如果配置了多个syncTo,则将交互式选择一个或多个。
这样,数据库A到数据库B的完整同步设置就完成了。
带注释的完整配置
以下是一个包含所有配置选项的配置示例。
--- generalConfig: # General config - Can be overridden by the configuration under syncFroms.config compress: NONE # Compressmethod for the SQL dump (NONE,GZIP,BZIP2,GZIPSTREAM) no-data: false # false = data | true = no data add-drop-table: true # Should a "ADD DROP TABLE" statement be executed? true = yes | false = no single-transaction: true lock-tables: true add-locks: true extended-insert: true disable-foreign-keys-check: true skip-triggers: false add-drop-trigger: true # Should a "ADD DROP TRIGGER" statement be executed? true = yes | false = no databases: false # Should a "CREATE DATABASE" statement be executed? true = yes | false = no add-drop-database: true # Should a "DROP DATABASE" statement be executed? true = yes | false = no hex-blob: true # Dump binary strings (BINARY, VARBINARY, BLOB) in hexadecimal format? true = yes | false = no syncFroms: # One or as many as desired syncFrom hosts - credentials: hostname: DATABASE_HOSTNAME port: YOUR_DATABASE_PORT username: YOUR_DATABASE_USER # Optional field. But must be entered interactively if omitted. password: YOUR_DATABASE_PASSWORD # Optional field. But must be entered interactively if omitted. database: YOUR_DATABASE_NAME config: # SyncFrom specific config no-data: false # E. g. overwrites no-data config from generalConfig conditions: # Allows to create rules on table level which data will be selected city: Population > 500000 and Name like 'A%' # For the city table, only entries are selected that have a population above 500000 and where the name starts with A. limits: # Sets table limits. city: 10 # Only 10 entries are selected for the city table country: 20 # Only 10 entries are selected for the country table tables: # The Tables Key is used to define operations on the table level. user: # Table user columns: email: # Column email replace: # The Replace operation replaces the values that correspond to oldValue with the value that is in value. # oldValue can also be a regular expression. But then this must be written as /REGULAR EXPRESSION/. - oldValue: gmail value: gmx password: # Column password overwrite: # The overwrite operation overwrites all entries of the column with value - value: 'DEFAULT' # All passwords are set to DEFAULT firstname: # Column firstname prefix: # The prefix operation appends to the beginning of the original value the value in "value". - value: MYPREFIX surname: # The suffix operation appends to the original value the value in "value". suffix: - value: MYSUFFIX syncTos: # One or as many as desired syncTo hosts - credentials: hostname: DATABASE_HOSTNAME port: YOUR_DATABASE_PORT username: YOUR_DATABASE_USER # Optional field. But must be entered interactively if omitted. password: YOUR_DATABASE_PASSWORD # Optional field. But must be entered interactively if omitted. database: YOUR_DATABASE_NAME