PHP code example of yerofey / replicator

1. Go to this page and download the library: Download yerofey/replicator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yerofey / replicator example snippets




ad variables from ".env" file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

if (!isset($_ENV['DB_REPLICATION_IS_ENABLED']) || $_ENV['DB_REPLICATION_IS_ENABLED'] == false) {
  exit();
}

$config_db_map = [
  'primary' => [
    'hostname' => $_ENV['DB_PRIMARY_HOST'],
    'database' => $_ENV['DB_PRIMARY_NAME'],
    'username' => $_ENV['DB_PRIMARY_USER'],
    'password' => $_ENV['DB_PRIMARY_PASS'],
  ],
  'secondary' => [
    'hostname' => $_ENV['DB_SECONDARY_HOST'],
    'database' => $_ENV['DB_SECONDARY_NAME'],
    'username' => $_ENV['DB_SECONDARY_USER'],
    'password' => $_ENV['DB_SECONDARY_PASS'],
  ],
];




// one-time worker replication script

ini_set('memory_limit', '256M');
set_time_limit(300);

// your app root location
$app_dir = __DIR__ . '/..';

// log actions in "/replicator_log.txt"
$debug = true;
$log_file = $app_dir . '/replicator_log.txt';

// master table config key from "config.php"
$primary_db_key = 'primary';
// slave table config key from "config.php"
$secondary_db_key = 'secondary';

// specifed tables to watch OR use "*" to watch for all tables
$watch_tables = [
  'test_table',
];

// load Composer
 {
  exit($e->getMessage() . PHP_EOL);
}

// init Replicator
$replicator = new Replicator(
  $connections,
  $helper,
  $debug,
  $log_file
);

// run Replicator
try {
  $replicator->run($watch_tables);
} catch (ReplicatorException $e) {
  $replicator->saveLog($e->getMessage());
}




// daemon worker replication script

ini_set('memory_limit', '256M');
set_time_limit(0);

// your app root location
$app_dir = __DIR__ . '/..';

// log actions in "/replicator_log.txt"
$debug = true;
$log_file = $app_dir . '/replicator_log.txt';

// master table config key from "config.php"
$primary_db_key = 'primary';
// slave table config key from "config.php"
$secondary_db_key = 'secondary';

// specifed tables to watch OR use "*" to watch for all tables
$watch_tables = [
  'test_table',
];

// watch interval (sec)
$interval_seconds = 10;

// load Composer
init Replicator
$replicator = new Replicator(
  $connections,
  $helper,
  $debug,
  $log_file
);

// run Replicator
while (true) {
  $time_start = microtime(true);

  try {
    $replicator->run($watch_tables);
  } catch (ReplicatorException $e) {
    $replicator->saveLog($e->getMessage());
  }

  $sleep = 0;
  $runtime = microtime(true) - $time_start;
  if ($runtime < $interval_seconds) {
    $sleep = $interval_seconds - $runtime;
  }

  sleep($sleep);
}

bash
touch config.php