MySQL
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.
farm
director
Horizontal Scaling (Clustering)
High-Availability / Failover
LinuxHA (Heartbeat), etc.
hot standby
How to Replicate Data on
GNU/Linux?
Rsync
How to Replicate Data on
GNU/Linux?
DRBD
raid1-over-network
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 db1.tbl1.data, db2.tbl2.data WHERE ...
When is Database Replication
Needed?
Case 2:
Manage the Database Load.
db_1 db_1
Write Server Read Server
( Insert / Update / Delete) ( Select )
When is Database Replication
Needed?
Case 3:
Hot-backup.
db_1 db_1
Production Server Backup Server
How MySQL Replication Works
Clients
MySQL MySQL
Daemon binlog
Daemon
b
i
n
l Slave
Thread Relay
o
g
Master Slave I/O Log
Thread Thread
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
192.168.0.1 192.168.0.2
Configuration of Replication
Master
/etc/my.cnf
[mysqld]
log-bin
server-id = 1
Replication Client User-
Account Creation on Master
mysql> GRANT REPLICATION SLAVE
ON *.*
TO 'replicator'@'192.168.0.2';
Configuration of Replication
Slave
/etc/my.cnf
[mysqld]
server-id =2
master-host = 192.168.0.1
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 = “mysql-bin.013”
master-user = 213
slave-skip-errors = 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 = db1
replicate-table = 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_1
db_2 db_2
[mysqld]
...
log-slave-update
Slave Require Multiple
Replication Masters
Master Masters
Slaves Slave
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_1
db_2 db_2
Master Master Slave
(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.
Load-Balancing
Virtual IP
c
l
i
e
Director
n
t
s Master
(Write)
Director Slaves
(backup) (Read-Only)
Load-Balancing Using Linux
Director & Heartbeat
/etc/ha.d/haresources
192.168.0.3 ldirectord::mysql
/etc/ha.d/mysql.cf
...
virtual = 192.168.0.3
real = 192.168.0.1
real = 192.168.0.2
request = [customized mysql page]
receive = [expected result]
Using Heartbeat for MySQL HA
1. Configure Heartbeat.
/etc/ha.d/haresources
192.168.0.3 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 Master
( active ) ( 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 Cluster
Manager
MySQL Farm Storage
( Read / Write ) Engine
Configuring MySQL Cluster
(Management Server)
config.ini
...
[NDB_MGMD]
HostName = 192.168.0.250
[NDBD]
HostName = 192.168.0.1
[NDBD]
HostName = 192.168.0.2
..
Start the controller / management server:
shell> ndb_mgmd
Configuring MySQL Cluster
(Database Nodes)
/etc/my.cnf
[mysqld]
ndbcluster
ndb-connectstring = 192.168.0.250
[mysql_cluster]
ndb-connectstring = 192.168.0.250
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 @192.168.0.1
id=3 @192.168.0.2
[ndb_mgmd(MGM)] 1 node(s)
id=1 @192.168.0.250
MySQL
Replication & Cluster
Ariz C. Jacinto
PLUG, Inc.