Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

swapnilmanew's avatar

Laravel - How to sync offline database to online

0

I want to sync my local database to online database when i click on a button in blade page. I looked online for the solution but did not found anything that worked for me. database.php

'local' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('LOCAL_DB_HOST', '127.0.0.1'),
            'port' => env('LOCAL_DB_PORT', '3306'),
            'database' => env('LOCAL_DB_DATABASE', 'forge'),
            'username' => env('LOCAL_DB_USERNAME', 'forge'),
            'password' => env('LOCAL_DB_PASSWORD', ''),
            'unix_socket' => env('LOCAL_DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
        'server' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('SERVER_DB_HOST', '127.0.0.1'),
            'port' => env('SERVER_DB_PORT', '3306'),
            'database' => env('SERVER_DB_DATABASE', 'forge'),
            'username' => env('SERVER_DB_USERNAME', 'forge'),
            'password' => env('SERVER_DB_PASSWORD', ''),
            'unix_socket' => env('SERVER_DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

env file

SERVER_DB_CONNECTION=server
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=3306
SERVER_DB_DATABASE=sync_db
SERVER_DB_USERNAME=root
SERVER_DB_PASSWORD=

LOCAL_DB_CONNECTION=local
LOCAL_DB_HOST=127.0.0.1
LOCAL_DB_PORT=3306
LOCAL_DB_DATABASE=sync_db
LOCAL_DB_USERNAME=root
LOCAL_DB_PASSWORD=

0 likes
11 replies
MohamedTammam's avatar

The question should be, why would you do it? mostly you're doing something wrong.

MohamedTammam's avatar

@swapnilmanew I don't see a good reason to sync your upload your local database to the production with Laravel.

You can do as @sinnbeck , which doesn't include Laravel. Or export it from local then upload to production and then import it.

Sinnbeck's avatar

I would probably write a shell script that dumps the database locally using mysqldump and then imports it in the other database through ssh with mysql

Sinnbeck's avatar

@swapnilmanew Not that I know of. Break down the task. First figure out how to dump your local database to an sql file using mysqldump. Then google how to ssh into a server and run a command.

maruf3535's avatar

@Sinnbeck sir I have already ready my custom script and run the script with custom command.

But when I run this command with a job, its not working.

Can I know your workflow details?

Sinnbeck's avatar

@maruf3535 I dont actually do this myself. But I would suggest making a new thread with your code and then get some help

iljavinzenz's avatar

@swapnilmanew I'd really love to know what you are building. Local, staging and production databases are always separate. Otherwise it's mpg scalable for other/future team members?

lewinmuz's avatar

Trying to do the same.

Scenario: An online based Laravel System is also suppose to be accessed by users who are in a NO INTERNET ZONE, though sometimes they go online. So when they go online all the offline data should be synced with the online data.

An copy offline version of the app is locally hosted on a local server at a location.

Please or to participate in this conversation.