Skip to content

Instantly share code, notes, and snippets.

@sasin91
Created June 11, 2018 13:08
Show Gist options
  • Save sasin91/04d1d0d3c670df646ccc146baab44cd3 to your computer and use it in GitHub Desktop.
Save sasin91/04d1d0d3c670df646ccc146baab44cd3 to your computer and use it in GitHub Desktop.
Defragment your MySQL database
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class DatabaseOptimizeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:optimize';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make sure the database is healthy.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$connection = config('database.connections.'.config('database.default'));
$process = new Process("mysqlcheck -h {$connection['host']} -o {$connection['database']} -u{$connection['username']} -p{$connection['password']}");
$process->run();
throw_unless($process->isSuccessful(), ProcessFailedException::class, $process);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment