Replication & Cluster

Ariz C. Jacinto
PLUG, Inc.

MySQL Overview

Most popular open source database. Part of the LAMP stack: GNU/Linux, Apache, MySQL, PHP/Perl/Python.
+ + +

MySQL Replication & Cluster, What for?

Scalability. High-availabilty.

Scalability Defined

Scalability is a key success factor for business applications in a dynamic environment. A system is said to scale if it is suitably efficient and practical when applied to large situations. Meaning, if the design fails when the quantity increases then it does not scale.

Types of Scaling
Vertical - Increase of processors, memory, etc. within one operating environment.

Horizontal – Increase of machines working in parallel for the same task (Clustering). Each system has its own operating system and one or more processors controlled by each system image.

Horizontal Scaling (Clustering)

High-Perfomance Computing OpenMosix, etc.

Horizontal Scaling (Clustering)

Load–Balancer DNS Round-Robin, LVS Director, etc.



Horizontal Scaling (Clustering)

High-Availability / Failover LinuxHA (Heartbeat), etc.

hot standby

How to Replicate Data on GNU/Linux?


How to Replicate Data on GNU/Linux?



How to Replicate Data on GNU/Linux?

(Database) SQL Replication

When is Database Replication Needed?
Case 1: Eliminating Cross-Host Query.
db_1 db_1 db_2

Server 1

Server 2

on Server 2 SELECT, WHERE ...

When is Database Replication Needed?
Case 2: Manage the Database Load.
db_1 db_1

Write Server ( Insert / Update / Delete)

Read Server ( Select )

When is Database Replication Needed?
Case 3: Hot-backup.
db_1 db_1

Production Server

Backup Server

How MySQL Replication Works

b i n l o g

MySQL Daemon

MySQL Daemon


Slave Thread Master Thread Slave I/O Thread

Relay Log

Configuration of MySQL for Replication

Download the latest stable MySQL 4.0.x version. Determine the Master and Slave(s). Edit /etc/my.cnf Creation of replication user-account on Replication Master.

Configuration of MySQL for Replication
Sample Setup

Database Server 1

Database Server 2

Configuration of Replication Master
/etc/my.cnf [mysqld] log-bin server-id = 1

Replication Client UserAccount Creation on Master

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replicator'@'';

Configuration of Replication Slave
/etc/my.cnf [mysqld] server-id =2 master-host = master-user = replicator

Start / Stop Replication
Start Replication Master & Slave(s)
shell> service mysqld start

Start / Stop Replication Client
mysql> start slave mysql> stop slave

Replication With Existing Data
1. Record the bin log name & position on the master.
mysql > SHOW MASTER STATUS; File Position mysql-bin.013 213

2. Dump the data from the master into an SQL file.

Replication With Existing Data
3. Copy & dump the SQL file to the slave.
mysql> source /path/to/file.sql

4. Alter the slave's replication starting point using the recorded bin log info.
... master_log_file master-user slave-skip-errors = “mysql-bin.013” = 213 = 1062

Replication Error 1062: Duplicates
SQL updates might occur on the Master in-between the recording of the binlog info and the dumping of data into the slave.

Thus, data already exists on the slave, by the time replication is being started.

Filtering of Database & Tables to be Replicated
[mysqld] ... replicate-db replicate-table

= db1 = db1.tbl1

replicate-ignore-db = db2 replicate-ignore-table = db1.tbl2

Renaming of Database & Tables to be Replicated
[mysqld] ... replicate-rewrite-db = db1->db2

replicate-rewrite-table = db1.tbl1->db1.tbl2

Dual Replication Function, Master & Slave
db_1 db_2 db_1 db_2

[mysqld] ... log-slave-update

Slave Require Multiple Replication Masters
Master Masters



Currently not supported, but replication from two masters is possible in a round-robin fashion using a script.

Slave Require Multiple Replication Masters
db_1 db_1 db_2 db_1 db_2


Master (Slave)


*Real-time replication from two masters.

Monitoring MySQL Replication
Check if replication threads are running. mysql> show slave status\G; Data Integrity - Row per Row Matching. - MD5 Checksum. - Routine Re-Dumping of Data.

Virtual IP

c l i e n t s


Master (Write) Director (backup) Slaves (Read-Only)

Load-Balancing Using Linux Director & Heartbeat
/etc/ha.d/haresources ldirectord::mysql /etc/ha.d/ ... virtual = real = real = request = [customized mysql page] receive = [expected result]

Using Heartbeat for MySQL HA
1. Configure Heartbeat. /etc/ha.d/haresources mysqld 2. Bind the MySQL service on the backup node to the virtual ip address. 3. Create a script to handle the re-sync of master from slave before it assumes again the role of the main server.

Using MySQL Cluster for HA (active/active)
db_1 db_1

Master ( active )

Master ( active )

Using MySQL Cluster for HA

Clustering of databases in a fault-tolerant system.

Standard MySQL server with an in-memory clustered storage engine called NDB.

Data stored in NDB, are immediately available on all data nodes.

Using MySQL Cluster for HA (active/active)
Virtual IP

c l i e n t s
MySQL Farm ( Read / Write ) Storage Engine

Cluster Manager

Configuring MySQL Cluster (Management Server)
config.ini ... [NDB_MGMD] HostName = [NDBD] HostName = [NDBD] HostName = .. Start the controller / management server: shell> ndb_mgmd

Configuring MySQL Cluster (Database Nodes)
/etc/my.cnf [mysqld] ndbcluster ndb-connectstring = [mysql_cluster] ndb-connectstring = Start storage engine / mysql daemon shell> ndbd --initial shell> service mysql start

Checking MySQL Cluster on Management Server
shell> ndb_mgm ndb_mgm> show Connected to Management Server Cluster Configuration [ndbd(NDB)] 2 node(s) id=2 @ id=3 @ [ndb_mgmd(MGM)] 1 node(s) id=1 @

Replication & Cluster

Ariz C. Jacinto
PLUG, Inc.

Sign up to vote on this title
UsefulNot useful