MySQLReplicationClient.pl - Replicates a remote host's binlogs to a local database
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
This script will:
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 | +----------------+ +------------------+
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:
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.
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
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
To stop MySQL's built-in replication from resuming on a restart, on the slave host:
RESET SLAVE CHANGE MASTER TO MASTER_HOST=''
To confirm that replication is working:
SELECT * FROM Replication.SourcePosition
./MySQLReplicationClient ... --getstats
The source host of the binlogs to replicate from.
The port of the replication server running on the source host.
The name of the binlog we want to replicate.
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.
The port of the local MySQL server.
The schema used for internal replication data such as the local binlog positions.
Credentials to the local MySQL server.
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.
The port of the replication relay server.
Logging is via syslog, using the 'daemon' facility (by default to /var/log/messages).
The syslog log level. Possible values are:
Since no log messages are at LOG_CRIT or above, this effectively turns off logging.
Log all errors.
Log all errors and query responses.
Displays statistics on how replication is proceeding.
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.