MySQL replication best practices 105-232-931

59
baruch osoveskiy [email protected]

Transcript of MySQL replication best practices 105-232-931

Page 1: MySQL replication best practices 105-232-931

baruch osoveskiy

[email protected]

Page 2: MySQL replication best practices 105-232-931

Baruch osoveskiy

Senior Consultant in dbaces.com

Linux-Unix SYSADMIN form 1997

DBA on Oracle and MySQL Since 2000

Working with unstructured Data (“big data”)

since 2000 like text and spatial

Page 3: MySQL replication best practices 105-232-931

Oracle and Microsoft Partners

Oracle Aces and Ace Directors

What we do◦ Remote DBA services

◦ Database consulting and projects

◦ Oracle identity and access management

◦ Training

Platforms◦ Oracle DB

◦ Microsoft SQL Server

◦ MySQL

◦ PostgreSQL

Page 4: MySQL replication best practices 105-232-931
Page 5: MySQL replication best practices 105-232-931

One webinar each week

Webinar recording will be available on site a few

days after webinar

Check our webinars schedule @

http://www.dbaces.com/training/upcoming-webinars

subscribe to get updates @ www.dbaces.com

Page 6: MySQL replication best practices 105-232-931

MySQL replication Introduction

New features in MySQL 5.6 replication

How to Create MySQL replication with hot-

backups

How To monitor MySQL replication

Page 7: MySQL replication best practices 105-232-931

Master SlaveNETWORK

Page 8: MySQL replication best practices 105-232-931

Master SlaveNETWORKSlave

SlaveSlave

Slave

Page 9: MySQL replication best practices 105-232-931

• High availability – can promote slave to master

• Scale Out – add multiple read slaves • Analytics – can run Analytics on live

data.• Backup – run cold backup on slave.• For DEV and Pre-Prod

Page 10: MySQL replication best practices 105-232-931

Replication Formats• SBR - Statement Based Replication

• Nondeterministic function problem • RBR - Row Based Replication

• High network usage • Higher IO usage

• Mixed Mode

Page 11: MySQL replication best practices 105-232-931

Not safe Nondeterministic functionFOUND_ROWS(), GET_LOCK(), IS_FREE_LOCK(), IS_USED_LOCK(), LOAD_FILE(), MASTER_POS_WAIT(), RELEASE_LOCK(), ROW_COUNT(), SESSION_USER(), SLEEP(), SYSDATE(), SYSTEM_USER(), USER(), UUID(), and UUID_SHORT().

Page 12: MySQL replication best practices 105-232-931

Where the Changes Stored • Binary Log – Master (blue-bin.000002) • Relay Log – Slave (white-relay-bin.000003)

Page 13: MySQL replication best practices 105-232-931

Replication Threads• Master Server

• Binlog Dump Thread• Slave Server

• IO Thread• SQL Thread

Page 14: MySQL replication best practices 105-232-931

Method of Sending the Data (Replication Type)• Asynchronous Replication• Semi-Synchronous Replication (5.5 )

Page 15: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

1

Binarylog

DML+

COMMIT

Binlog Dump Thread

Page 16: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

1

2

Binarylog

IO Thread

DML+

COMMIT

Page 17: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

3

Binarylog

RelayLog

IO Thread

DML+

COMMIT

Page 18: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

34

Binarylog

RelayLog

IO Thread

SQL Thread

DML+

COMMIT

Page 19: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

34

5

Binarylog

RelayLog

IO Thread

SQL Thread

DML+

COMMIT

Page 20: MySQL replication best practices 105-232-931

Pros• Fast• slave can be disconnectedCons• Potential loss of transactions and data integrity

Page 21: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

1

Binarylog

DML

Binlog Dump Thread

Page 22: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

1

Binarylog

DMLIO Thread

Page 23: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

1

Binarylog

DMLIO Thread

2

34

IO Thread

TRX1TRX2TRX3

.

RelayLog

Page 24: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

34

Binarylog

RelayLog

IO Thread

IO ThreadDML

Page 25: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

34

Binarylog

RelayLog

IO Thread

IO ThreadDML

COMMIT

Page 26: MySQL replication best practices 105-232-931

Master

TRX1TRX2TRX3

.

Slave

TRX1TRX2TRX3

.

1

2

34

Binarylog

RelayLog

IO Thread

IO ThreadDML

COMMIT

5SQL Thread

Page 27: MySQL replication best practices 105-232-931

Pros• data integrity

• Commit ensure the transaction will copy to slave

Cons• Performance Implications

• The transaction need to Wait for the slave to commit

• Problematic in high DML load

Page 28: MySQL replication best practices 105-232-931

What's New

• Multi threaded Slave• Relay log recovery • Binary Log Group Commit• Crash-safe Replication – Binlog Checksums• Global Transaction ID

You can read more in:http://dev.mysql.com/tech-resources/articles/whats-new-in-mysql-5.6.html

Page 29: MySQL replication best practices 105-232-931

Replication performance is improved by using multiple execution threads to apply replication events

slave-parallel-workers=2

You can read more in:http://dev.mysql.com/tech-resources/articles/whats-new-in-mysql-5.6.html

Page 30: MySQL replication best practices 105-232-931

• slave can automatically recover from a failure and resume replicating DML updates

• the slave can roll back replication to the last successfully committed transaction

relay-log-recovery =1

You can read more in:http://dev.mysql.com/tech-resources/articles/whats-new-in-mysql-5.6.html

And in Bug #13893363

Page 31: MySQL replication best practices 105-232-931

• Commit a group of transactions to the binary log.• Ensures durability with good performance.• Designed to handle seamlessly temporary peeks

on the workload.

enable by default in 5.6

You can read more in:http://dev.mysql.com/tech-resources/articles/whats-new-in-mysql-5.6.html

Page 32: MySQL replication best practices 105-232-931
Page 33: MySQL replication best practices 105-232-931

Master and slave can have different data • Non deterministic queries. • Wrong use of replication filters. • Write executed on slave outside the.

replication (slave is not read only)• Binlogs or relay log corruption.

Page 34: MySQL replication best practices 105-232-931

How to check ? Before 5.6 you can use only :• show slave status • pt-table-checksum

• utilty from precona that can check table checksum

Page 35: MySQL replication best practices 105-232-931

• First enable checksum on the master

binlog_checksum = CRC32;master_verify_checksum = on;

• Then start binlog verify on the slave

slave_sql_verify_checksum = on;

* From >= 5.6.6 enable by default

Page 36: MySQL replication best practices 105-232-931

mysql> show slave status

…Last_Error : 1538 Last_Error : relay log read error…… …..# mysqlbinlog --verify-binlog-checksum mysql_18676-relay-bin.000002

[...]ERROR: Error in Log_event::read_log_event(): 'Event crc check failed! Most likely there is event corruption.', data_len: 103, event_type: 2ERROR: Could not read entry at offset 283: Error in log format or read error.[...]

Page 37: MySQL replication best practices 105-232-931

Pros• Can help to find error in the bin log/relay log • Notify on a bin log corruptionCons• More CPU for the CRC • The MySQL will keep writing to the corrupt bin log

Page 38: MySQL replication best practices 105-232-931
Page 39: MySQL replication best practices 105-232-931

• Transactions are not guaranteed to execute in the order of the Binary logs

• The show slave status is not accurate

Page 40: MySQL replication best practices 105-232-931

Master

Slave1

Slave2

BinlogFile:mysql-bin.0010Position:1238574

BinlogFile:mysql-bin.0020Position:1043

BinlogFile:mysql-bin.0140Position:8373

Page 41: MySQL replication best practices 105-232-931

• The same event can have different Binary log file and position on different slaves

• Hard to find inconsistency in replication• Very problematic to reconfigure replication

Page 42: MySQL replication best practices 105-232-931

• Unique identifier of a transaction across all servers in replication topology

The GTID look like :

GTID = source_id:transaction_idWhere transaction_id = transaction_start-transaction_stop

eg :3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5

Page 43: MySQL replication best practices 105-232-931

Master

Slave1

Slave2

BinlogFile:mysql-bin.00103E11FA47-71CA-11E1-9E33-C80AA9429562:1-5

BinlogFile:mysql-bin.00203E11FA47-71CA-11E1-9E33-C80AA9429562:1-5

BinlogFile:mysql-bin.01403E11FA47-71CA-11E1-9E33-C80AA9429562:1-5

Page 44: MySQL replication best practices 105-232-931

How to enable GUID on MySQL replication• Enable read only mode on Slaves

set global read_only=1• Compare the binlog position on master and slave

show slave status show master status

• Change configuration on all servers and restart bin-logguid_mode=ONEnforce-guid-consistency

• Initialize GUID on all servers change master to MASTER_AUTO_POSITION = 1;

• Disable read only mode on Slaves

Page 45: MySQL replication best practices 105-232-931
Page 46: MySQL replication best practices 105-232-931

Master Blue

192.168.1.110

SlaveWhite

192.168.1.111

Page 47: MySQL replication best practices 105-232-931

Master Blue

192.168.1.110

SlaveWhite

192.168.1.111

Data files from

Master Blue

TRX1TRX2TRX3

.

Binarylog

Page 48: MySQL replication best practices 105-232-931

Master Blue

192.168.1.110

SlaveWhite

192.168.1.111Replication

Page 49: MySQL replication best practices 105-232-931

blue.my.cnf

[mysqld]...server-id=1report-host=bluebinlog-format=MIXEDlog-binlog-slave-updates=truegtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1binlog-checksum=CRC32master-verify-checksum=1

Page 50: MySQL replication best practices 105-232-931

white.my.cnf[mysqld]...server-id=2report-host=whitebinlog-format=MIXEDlog-slave-updates=truelog-bingtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLErelay-log-info-repository=TABLEsync-master-info=1slave-parallel-workers=2binlog-checksum=CRC32master-verify-checksum=1slave-sql-verify-checksum=1binlog-rows-query-log_events=1

Page 51: MySQL replication best practices 105-232-931

On Master:

[mysqld]rpl_semi_sync_master_enabled=1rpl_semi_sync_master_timeout=1000 # 1 second

On each Slave:

[mysqld]rpl_semi_sync_slave_enabled=1

Page 52: MySQL replication best practices 105-232-931

we need to create the replication user on master

mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected] IDENTIFIED BY 'reppwd';FLUSH PRIVILEGES;

Page 53: MySQL replication best practices 105-232-931

create Hot backup with * Oracle Enterprise Backup

#mysqlbackup --port=3306 --protocol=tcp --user=root --password --backup-dir=/data/backup backup-and-apply-log

* Also can use Percona XtraBackup

Page 54: MySQL replication best practices 105-232-931

• copy the backup directory to the slave via scp or other method

on the master:#tar –cvfZ /data/backup.taz /data/backup#scp /data/backup.taz 192.168.1.111:/data/

on the slave:#tar –xvfZ /data/backup.taz

Page 55: MySQL replication best practices 105-232-931

• Start the Slave #service start mysql• Start the replication process that will start the

io_Thread and the sql_Threadmysql>CHANGE MASTER TO MASTER_HOST='192.168.1.110', MASTER_USER='replication', MASTER_PASSWORD='reppwd', MASTER_AUTO_POSITION=1;START SLAVE;

Page 56: MySQL replication best practices 105-232-931

show slave status\G*************************** 1. row********Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.1.110Master_User: replicationMaster_Log_File: blue-bin.000002Read_Master_Log_Pos: 936Relay_Log_File: white-relay-bin.000003Relay_Log_Pos: 1146Relay_Master_Log_File: blue-bin.000002Slave_IO_Running: YesSlave_SQL_Running: Yes

Page 57: MySQL replication best practices 105-232-931

show slave status\G*************************** 1. row********Last_Errno: 0Last_Error: Skip_Counter: 0Master_UUID: 2671c08b-7cf0-11e2-ac39-00163ebee7c2

Master_Info_File: mysql.slave_master_infoSQL_Delay: 0SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itRetrieved_Gtid_Set: 2671c08b-7cf0-11e2-ac39-00163ebee7c2:1-4Executed_Gtid_Set: 2671c08b-7cf0-11e2-ac39-00163ebee7c2:1-4

Page 58: MySQL replication best practices 105-232-931