40
MySQL Replication & Cluster Ariz C. Jacinto PLUG, Inc.

Mysql replication & cluster

  • Upload
    taejun

  • View
    2.816

  • Download
    4

Embed Size (px)

DESCRIPTION

Mysql replication & cluster

Citation preview

Page 1: Mysql replication & cluster

MySQL Replication & Cluster

Ariz C. JacintoPLUG, Inc.

Page 2: Mysql replication & cluster

MySQL Overview

Most popular open source database.

Part of the LAMP stack: GNU/Linux, Apache, MySQL, PHP/Perl/Python.

+ + +

Page 3: Mysql replication & cluster

MySQL Replication & Cluster, What for?

Scalability.

High-availabilty.

Page 4: Mysql replication & cluster

Scalability Defined

Scalability is a key success factor for business applications in a dynamicenvironment.

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.

Page 5: Mysql replication & cluster

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.

Page 6: Mysql replication & cluster

Horizontal Scaling (Clustering)

High-Perfomance Computing OpenMosix, etc.

Page 7: Mysql replication & cluster

Horizontal Scaling (Clustering)

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

farm

director

Page 8: Mysql replication & cluster

Horizontal Scaling (Clustering)

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

hot standby

Page 9: Mysql replication & cluster

How to Replicate Data on GNU/Linux?

Rsync

Page 10: Mysql replication & cluster

How to Replicate Data on GNU/Linux?

DRBD

raid1-over-network

Page 11: Mysql replication & cluster

How to Replicate Data on GNU/Linux?

(Database) SQL Replication

Page 12: Mysql replication & cluster

When is Database Replication Needed?

Case 1: Eliminating Cross-Host Query.

db_1 db_2db_1

Server 1 Server 2

on Server 2SELECT db1.tbl1.data, db2.tbl2.data WHERE ...

Page 13: Mysql replication & cluster

When is Database Replication Needed?

Case 2: Manage the Database Load.

db_1 db_1

Write Server( Insert / Update / Delete)

Read Server( Select )

Page 14: Mysql replication & cluster

When is Database Replication Needed?

Case 3: Hot-backup.

db_1 db_1

Production Server Backup Server

Page 15: Mysql replication & cluster

How MySQL Replication Works

binlog

Slave I/O Thread

MySQL Daemon

Slave Thread Relay

Log

binlog

Master Thread

MySQL Daemon

C l i e n t s

Page 16: Mysql replication & cluster

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.

Page 17: Mysql replication & cluster

Configuration of MySQL for Replication

Sample Setup

Database Server 1192.168.0.1

Database Server 2192.168.0.2

Page 18: Mysql replication & cluster

Configuration of Replication Master

/etc/my.cnf

[mysqld]log-binserver-id = 1

Page 19: Mysql replication & cluster

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

Replication Client User-Account Creation on Master

Page 20: Mysql replication & cluster

Configuration of Replication Slave

/etc/my.cnf

[mysqld]server-id = 2master-host = 192.168.0.1master-user = replicator

Page 21: Mysql replication & cluster

Start / Stop Replication

Start Replication Master & Slave(s)

shell> service mysqld start

Start / Stop Replication Client

mysql> start slavemysql> stop slave

Page 22: Mysql replication & cluster

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.

Page 23: Mysql replication & cluster

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 = 213slave-skip-errors = 1062

Replication With Existing Data

Page 24: Mysql replication & cluster

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.

Replication Error 1062: Duplicates

Page 25: Mysql replication & cluster

Filtering of Database & Tables to be Replicated

[mysqld]...replicate-db = db1replicate-table = db1.tbl1

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

Page 26: Mysql replication & cluster

Renaming of Database & Tables to be Replicated

[mysqld]

...replicate-rewrite-db = db1->db2

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

Page 27: Mysql replication & cluster

Dual Replication Function, Master & Slave

[mysqld]...log-slave-update

db_1 db_1db_2 db_2

Page 28: Mysql replication & cluster

Slave Require Multiple Replication Masters

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

Slaves

Master

Slave

Masters

Page 29: Mysql replication & cluster

Slave Require Multiple Replication Masters

*Real-time replication from two masters.

SlaveMaster Master(Slave)

db_1db_2

db_1db_2

db_1

Page 30: Mysql replication & cluster

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.

Page 31: Mysql replication & cluster

Load-Balancing

Master(Write)

Director

Slaves(Read-Only)

Director(backup)

clients

Virtual IP

Page 32: Mysql replication & cluster

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.3real = 192.168.0.1real = 192.168.0.2request = [customized mysql page]receive = [expected result]

Page 33: Mysql replication & cluster

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.

Page 34: Mysql replication & cluster

Using MySQL Cluster for HA (active/active)

db_1 db_1

Master( active )

Master( active )

Page 35: Mysql replication & cluster

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.

Page 36: Mysql replication & cluster

Using MySQL Cluster for HA (active/active)

ClusterManager

MySQL Farm( Read / Write )

clients

StorageEngine

Virtual IP

Page 37: Mysql replication & cluster

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

Page 38: Mysql replication & cluster

Configuring MySQL Cluster(Database Nodes)

/etc/my.cnf

[mysqld]ndbclusterndb-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

Page 39: Mysql replication & cluster

Checking MySQL Clusteron Management Server

shell> ndb_mgm

ndb_mgm> showConnected to Management ServerCluster Configuration[ndbd(NDB)] 2 node(s)id=2 @192.168.0.1id=3 @192.168.0.2

[ndb_mgmd(MGM)] 1 node(s)id=1 @192.168.0.250

Page 40: Mysql replication & cluster

MySQL Replication & Cluster

Ariz C. JacintoPLUG, Inc.