NAME

MySQLReplicationClient.pl - Replicates a remote host's binlogs to a local database

SYNOPSIS

MySQLReplicationClient.pl --srchost <srchost> --srcport <srcport> --srcbinlog <srcbinlog> --desthost <desthost> --destport <destport> --destdb <destdb> --destuser <destuser> --destpass <destpass> --relayhost <relayhost> --relayport <relayport> --loglevel <loglevel> --getstats

DESCRIPTION

This script will:

  • Connect to the replication server running on the source host (or a relay)

  • Request binlog queries starting from the local binlog position

  • Read query responses and execute them on the local database

  • Update the local binlog position after each query has been executed

A running instance will replicate from a single source i.e.:

+----------------+       +-----------------+
| local database |<------| source database |
+----------------+       +-----------------+

To replicate from multiple sources, simply run it multiple times i.e.:

+----------------+       +------------------+
|                |<------| source database1 |
|                |       +------------------+
|                |
|                |       +------------------+
| local database |<------| source database2 |
|                |       +------------------+
|                |       
|                |       +------------------+
|                |<------| source database3 |
+----------------+       +------------------+

First Time Setup

Stop MySQL's Built-In Replication

MySQL's built-in replication must be stopped between the source host and the local database before continuing. Otherwise MySQL's built-in replication and this script will execute the same queries leading to incorrect data.

On the slave host:

SLAVE STOP

Create the Replication Schema

The replication schema is where internal replication data will be stored. Before running the replication client for the first time, this schema will need to be created on the local database:

SET SQL_LOG_BIN=0;

CREATE DATABASE IF NOT EXISTS Replication;
USE Replication;

CREATE TABLE IF NOT EXISTS SourcePosition (
  Host    VARCHAR(255) NOT NULL,
  Binlog  VARCHAR(255) NOT NULL,
  Log     MEDIUMINT    NOT NULL,
  Pos     INT          NOT NULL,
  PRIMARY KEY ( Host, Binlog )
) ENGINE=InnoDB;

By default the 'Replication' schema name is used.

Grant Permissions

Since all queries from the replication server will be executed on the local database, appropriate permissions must be granted to the provided credentials.

Note that the SUPER privilege will also need to be granted since it will be modifying the system variable SQL_LOG_BIN.

Setting the Local Binlog Position

Before running the replication client for the first time, it will need to know where to start replicating from:

SET SQL_LOG_BIN=0;

INSERT INTO Replication.SourcePosition VALUES
( srchost, srcbinlog, startlog, startpos );

WARNING: Is is essential that turning off binlogging here is done before the insert. Otherwise all replication clients of this host will see the insert and update their own internal binlog positions.

The value of startlog and startpos will depend on how the local MySQL database was created.

To pick up from where MySQL's built-in replication stopped, use the values from SHOW SLAVE STATUS:

> SHOW SLAVE STATUS\G
...	
Relay_Master_Log_File: binlog-filename.<startlog>
Exec_Master_Log_Pos: <startpos>

To start from a known query within a binlog on the source host:

  $ mysqlbinlog binlog-filename.<startlog> | less
  ...
  # at <startpos>
	QueryText

Turn off MySQL's Built-In Replication

To stop MySQL's built-in replication from resuming on a restart, on the slave host:

RESET SLAVE
CHANGE MASTER TO MASTER_HOST=''

Confirm it's working

To confirm that replication is working:

  • You can query the replication position directly:

    SELECT * FROM Replication.SourcePosition
  • Use the --getstats option along with all the required options:

    ./MySQLReplicationClient ... --getstats

OPTIONS

srchost (mandatory)

The source host of the binlogs to replicate from.

srcport (default: 2603)

The port of the replication server running on the source host.

srcbinlog (mandatory)

The name of the binlog we want to replicate.

desthost (default: localhost)

The host of the local MySQL server to replicate into.

This value is usually 'localhost' however MySQL can be configured to bind to a specific IP address instead of all interfaces.

destport (default: 3306)

The port of the local MySQL server.

destdb (default: Replication)

The schema used for internal replication data such as the local binlog positions.

destuser (mandatory)
destpass (mandatory)

Credentials to the local MySQL server.

relayhost

If not specified, connect directly to the source host to replicate from.

If specified, connect instead to a replication relay host to replicate from.

A replication relay acts as a proxy cache for multiple replication clients.

relayport (default: 2600)

The port of the replication relay server.

loglevel (default: LOG_ERR)

Logging is via syslog, using the 'daemon' facility (by default to /var/log/messages).

The syslog log level. Possible values are:

  • LOG_CRIT

    Since no log messages are at LOG_CRIT or above, this effectively turns off logging.

  • LOG_ERR

    Log all errors.

  • LOG_DEBUG

    Log all errors and query responses.

getstats

Displays statistics on how replication is proceeding.

BUGS

  • Currently, row-based replication and LOAD DATA INFILE and related queries aren't supported. If encountered, replication will stop.

  • Since all queries coming over the wire will be in plain text, it is important that traffic be routed through a secure tunnel.

  • The relay isn't complete yet (but it's coming soon)

AUTHOR

Alfie John, alfiej@opera.com

LICENSE AND COPYRIGHT

Copyright (c) 2011, Opera Software Australia Pty. Ltd. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
  may be used to endorse or promote products derived from this software
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.