Skip to content

Laravel Multiple Database Migration Issues – Complete Solution

By Jasper Frumau

When working with multiple databases in Laravel, you may encounter issues where additional databases are missing migration tables. This causes problems when running php artisan migrate and php artisan schema:dump for those databases.

Common Error Messages

You’ll typically see errors like:

Error Output:
================
mysqldump: Couldn't find table: "migrations"
at vendor/symfony/process/Process.php:267

Or when running schema dumps:

mysqldump: unknown variable 'column-statistics=0'

Complete Solution

Step 1: Install Migration Table for Additional Database

First, create the migrations table for your additional database:

php artisan migrate:install --database=mysql_publish_testing

This will output:

INFO  Migration table created successfully.

Step 2: Handle mysqldump Variable Error

If you encounter the column-statistics=0 error when running schema dumps, you have several options:

Option A: Update your database configuration

In your config/database.php, add the dump configuration to your database connection:

'mysql_publish_testing' => [
    'driver' => 'mysql',
    'host' => env('DB_PUBLISH_HOST', '127.0.0.1'),
    'port' => env('DB_PUBLISH_PORT', '3306'),
    'database' => env('DB_PUBLISH_DATABASE', 'forge'),
    'username' => env('DB_PUBLISH_USERNAME', 'forge'),
    'password' => env('DB_PUBLISH_PASSWORD', ''),
    // ... other config
    'dump' => [
        'dump_binary_path' => '/usr/bin/', // Path to mysqldump
        'use_single_transaction' => true,
        'timeout' => 60 * 5, // 5 minute timeout
        'exclude_tables' => [],
        'add_extra_option' => '--column-statistics=0',
    ],
],

Option B: Use environment-specific mysqldump options

If you’re using MySQL 8.0+ and getting the column-statistics error, you can either:

  1. Upgrade your mysqldump version to one that supports the option
  2. Remove the problematic option by updating Laravel’s dump configuration

Option C: Manual mysqldump command

You can also run the dump manually with proper options:

mysqldump --user=username --password=password --host=localhost --column-statistics=0 database_name > schema.sql

Step 3: Run Schema Dump

After resolving the mysqldump issue, you can successfully run:

php artisan schema:dump --database=mysql_publish_testing

Expected output:

INFO  Database schema dumped successfully.

Step 4: Run Migrations

Now you can run migrations on your additional database:

php artisan migrate --database=mysql_publish_testing

Prevention Tips

  1. Always install migration tables for new database connections before running migrations
  2. Test your mysqldump configuration in your local environment first
  3. Keep your MySQL tools updated to avoid version compatibility issues
  4. Document your database setup for team members

Alternative: Programmatic Solution

You can also create a custom Artisan command to automate this process:

// app/Console/Commands/SetupDatabase.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class SetupDatabase extends Command
{
    protected $signature = 'db:setup {connection}';
    protected $description = 'Setup a database connection with migrations table';

    public function handle()
    {
        $connection = $this->argument('connection');
        
        // Install migrations table
        Artisan::call('migrate:install', ['--database' => $connection]);
        $this->info("Migration table installed for {$connection}");
        
        // Run migrations
        Artisan::call('migrate', ['--database' => $connection]);
        $this->info("Migrations completed for {$connection}");
        
        // Dump schema
        try {
            Artisan::call('schema:dump', ['--database' => $connection]);
            $this->info("Schema dumped for {$connection}");
        } catch (\Exception $e) {
            $this->error("Schema dump failed: " . $e->getMessage());
            $this->info("Check your mysqldump configuration");
        }
    }
}

Then run:

php artisan db:setup mysql_publish_testing

This solution addresses both the missing migrations table and the mysqldump configuration issues you may encounter when working with multiple databases in Laravel.

Comments are closed for this post.