n3xt0r/laravel-mysql-sync

同步数据库从一个环境到另一个环境

1.2.3 2020-10-09 11:35 UTC

README

Build Status Latest Stable Version Test Coverage Maintainability License

此工具允许 Laravel/Lumen 开发者将配置的环境(如测试或生产系统)的 MySQL 数据库同步到本地环境。

要求

远程

- mysqldump
- openssh

本地

- mysql-client

安装

composer require --dev n3xt0r/laravel-mysql-sync

将新的提供者添加到 config/app.php 文件中的 providers 数组

    'providers' => [
        // ...
        \N3XT0R\MySqlSync\Providers\MySqlSyncServiceProvider::class,
        // ...
      ],

发布配置

php artisan vendor:publish --provider="N3XT0R\MySqlSync\Providers\MySqlSyncServiceProvider"

Lumen

手动将供应商目录中的配置添加到您的配置目录。不要忘记在 bootstrap/app.php 中注册它。

配置示例

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Remote Server Connections
    |--------------------------------------------------------------------------
    |
    | These are the servers that will be accessible via the SSH task runner
    | facilities of Laravel. This feature radically simplifies executing
    | tasks on your servers, such as deploying out these applications.
    |
    */
    'connections' => [
        'production' => [
            'host' => 'example.com',
            'username' => 'myUser',
            'password' => '',
            'key' => storage_path('id_rsa'),
            'keytext' => '',
            'keyphrase' => '',
            'agent' => '',
            'timeout' => 10,
        ],
    ],
    /*
    |--------------------------------------------------------------------------
    | Remote Server Databases
    |--------------------------------------------------------------------------
    |
    | These are the databases that will be accessible for syncing.
    |
    */
    'databases' => [
        'laravel' => [
            'connection' => 'production',
            'host' => 'mysql.example.com',
            'database' => 'myApp',
            'user' => 'root',
            'password' => 'myPassword',
        ],
        'secondOptionalDb' => [
            'connection' => 'production',
            'host' => 'mysql.example.com',
            'database' => 'customerDb',
            'user' => 'root',
            'password' => 'myPassword',
        ],
    ],
    'environments' => [
        'production' => [
            /**
            * be careful, this is the same order like on importing databases
            * when you have constraints between database, set them to correct order.
            */
            'databases' => [
                'laravel',
                'secondOptionalDb',
            ],
        ],
    ],
    /**
    * originally it should be the storage dir
    * but you could configure any other directory, too.
    */
    'storage' => storage_path(), 
];

执行同步操作

php artisan db:sync --stage=production