SlideShare a Scribd company logo
MySQL Replication Basics
Ohio Linux Fest 2016
Dave Stokes
david.stokes@oracle.com
@stokes
slideshare.net/davidmstokes
1
Safe Harbor 2
THE FOLLOWING IS INTENDED TO OUTLINE OUR
GENERAL PRODUCT DIRECTION. IT IS INTENDED FOR
INFORMATION PURPOSES ONLY, AND MAY NOT BE
INCORPORATED INTO ANY CONTRACT. IT IS NOT A
COMMITMENT TO DELIVER ANY MATERIAL, CODE, OR
FUNCTIONALITY, AND SHOULD NOT BE RELIED UPON IN
MAKING PURCHASING DECISIONS. THE DEVELOPMENT,
RELEASE, AND TIMING OF ANY FEATURES OR
FUNCTIONALITY DESCRIBED FOR ORACLE'S
PRODUCTS REMAINS AT THE SOLE DISCRETION OF
ORACLE.
Quick MySQL Catch Up
21 Years Old
MySQL has been part of
Oracle’s family of databases for
six years.
MySQL 8
MySQl 5.7 is the current release
but the next version will be
MySQL 8. Big feature is real
time data dictionary
Group Replication
Active master-master
replication.
JSON
A new native JSON datatype to
store documents in a column of
a table
Document Store
Programmers not know SQL
but need a database? X Devapi
allows them to use RDMS from
language of choice
Encryption
Use Oracle Key Vault to encrypt
your data at rest.
3
Replication
Past
Present
Future
4
Basic Premise Behind
Replication
1. Copy all the data off one server and on to another.
2. Setup Replication.
3. Log any changes on the first server to a log file, apply that
logfile to the second server.
5
Imagine Your Data as a Painting:
Make copy of Mona Lisa
6
Make copy of changes to Mona Lisa
7
Keep making copy of changes to Mona Lisa
8
Architecture
Binary Log on master
Slave Attaches
I/O Thread Grabs Data
Data copied to slave
Applier thread changes slave’s data
9
Graphic overview of replication
10
But what are we
replicating?
11
Replicating changes to tables and data
SBR: When using statement-based binary logging, the
master writes SQL statements to the binary log.
Replication of the master to the slave works by
executing the SQL statements on the slave. This is
called statement-based replication (often abbreviated
as SBR), which corresponds to the standard MySQL
statement-based binary logging format. Replication
capabilities in MySQL version 5.1.4 and earlier used
this format exclusively.
Structured Query Language (SQL)
RBR: When using row-based logging, the master
writes events to the binary log that indicate how
individual table rows are changed. Replication of the
master to the slave works by copying the events
representing the changes to the table rows to the
slave. This is called row-based replication (often
abbreviated as RBR).
The Delta
12
OrBoth
MBR: You can also configure MySQL to use a mix of both
statement-based and row-based logging, depending on which is
most appropriate for the change to be logged. This is called
mixed-format logging. When using mixed-format logging, a
statement-based log is used by default. Depending on certain
statements, and also the storage engine being used, the log is
automatically switched to row-based in particular cases.
Replication using the mixed format is often referred to as
mixed-based replication or mixed-format replication.
13
Notes for the future
With MySQL.5.6 RBR started sending over only the
primary key and the changed data (not sending
unchanged data) which can drastically cut the amount
of data sent to slave servers. This can be huge!!
Many future products will work better with RBR as it
more deterministic. So plan accordingly.
14
Threading
Before 5.6 MySQL Replication is SINGLE threaded – Airline
boarding example
MySQL 5.6 is multi-threaded at the database level
MySQL 5.7 is multi-threaded at the table level
15
Synchronicity
16
Async and Semi Synchronous
Semi Synchronous replication -- a commit
performed on the master blocks before
returning to the session that performed the
transaction until at least one slave
acknowledges that it has received and
logged the events for the transaction (Note
not written to table, just recorded for
future).
Asynchronous replication -- slave servers
retrieve data, master unaware of slave’s
consumption.
17
18
Topologies
Topologies -- Before 5.7
19
Common - Read / Write Split
20
Multi-source Replication -- MySQL 5.7
21
Group Replcation -- Labs.MySQL.Com for evaluation
22
Multi-Master
Lots of folks want TWO active
masters and this can be done but
not recommended, You need to
have a sharp crew and plan for
conflicts.
Not generally recommended
before Group Replication
23
Multi-Master
MySQL Cluster
You can run active-active
master-master with MySQL
Cluster, even between data
centers.
This can be very expensive and
MySQl Cluster is not a full
featured version of the MySQL
Server.
24
Galera Cluster
Layer separate from MySQL that is
mainly for high availability (not high
performance).
Claims to have snapshot isolation on
transactions but watch out for ‘first
committer wins’ and prepare for
rollbacks.
Not low latency
25
How to setup
MySQL
Replication
26
Two types of
replication
w/ & w/o
GTIDs
A global transaction identifier (GTID) is a unique
identifier created and associated with each
transaction committed on the server of origin
(master). This identifier is unique not only to the
server on which it originated, but is unique across
all servers in a given replication setup. There is a
1-to-1 mapping between all transactions and all
GTIDs.
3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5
Note the 1-5 is a group of
transactions
27
Binlog & ID
Enable Binary Log on
Master, Unique ID
number
Create
User
Create user for
replication
Binlog
Offset
Get Master’s Binary
Log coordinates
Snapshot
Snapshot data, copy
onto slave
SLAVE
running
CHANGE MASTER
command and START
SLAVE
Before GTIDs (MySQL 5.5 and before) 28
Enable Binary Log & Unique ID on Master
Edit my.cnf file
[mysqld]
log-bin=mysql-bin
server-id=1
29
Create replication user
mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY
'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.* TO
'repl'@'%.mydomain.com';
30
Get binary log position
mysql> FLUSH TABLES WITH READ LOCK;
mysql > SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 73 | test | manual,mysql |
+------------------+----------+--------------+------------------+
31
Unlock tables mysql> UNLOCK TABLES;
32
Config
slave &
load data
No config thee slave
server. Remember
server-id must be
unique
[mysqld]
server-id=2
shell> mysql -h master
< fulldb.dump
33
Change Master & Start Slave
mysql> CHANGE MASTER TO
-> MASTER_HOST='master_host_name',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password',
-> MASTER_LOG_FILE='recorded_log_file_name',
-> MASTER_LOG_POS=recorded_log_position;
mysql> START SLAVE;
34
Read only
Sync both servers by
setting to read if
replication is already
running
Stop
Servers
Stop both servers
Restart
w/GTIDs
Restart both servers
with GTIDs enabled
Change
master
Instruct the slave to use the
master as the replication
data source and to use
auto-positioning, and then
start the slave.
Read Only
off
Remove read only
With GTIDs (MySQL 5.6 and later) 35
Replication Setup
with GTIDs
mysql> SET @@global.read_only = ON;
mysql>mysqladmin -uusername -p
shutdown
shell> mysqld --gtid-mode=ON
--enforce-gtid-consistency &
mysql> CHANGE MASTER TO
> MASTER_HOST = host,
> MASTER_PORT = port,
> MASTER_USER = user,
> MASTER_PASSWORD = password,
> MASTER_AUTO_POSITION = 1;
mysql> START SLAVE;
36
Finished!
(Almost)
David.Stokes@oracle.com
@stoker
slideshare.net/davidmstokes
37
Hard to get a lot of info in in
such a short time. Please get
details from the MySQL Manual
30 Minutes!!!
30 Minutes! Can’t Cover all of MySQL Replication in 30 minutes!!!!! 38
https://github.com/datacharmer
Giuseppe Maxia has lot of great code
for getting used to replication -- see
~/mysql-replication-sample
39
Great Place
to Find
examples &
MySQL
Sandbox
MySQL
Utilities
FREE Scripts written in Python, used with MySQL
Workbench or stand alone
1. Copy, diff databases
2. Disk usage, grants, copy users
3. Search for processed and kill ‘em
4. Setup replication and failover
40
Mysqlrplcheck -- check replication setup
shell> mysqlrplcheck --master=root@host1:3310 --slave=root@host2:3311
# master on host1: ... connected.
# slave on host2: ... connected.
Test Description Status
------------------------------------------------------------------------
Checking for binary logging on master [pass]
Are there binlog exceptions? [pass]
Replication user exists? [pass]
Checking server_id values [pass]
Is slave connected to master? [pass]
Check master information file [pass]
Checking InnoDB compatibility [pass]
Checking storage engines compatibility [pass]
Checking lower_case_table_names settings [pass]
Checking slave delay (seconds behind master) [pass]
# ...done.
41
Mysqlrplcheck -- replication checker
shell> mysqlrplsync --master=user:pass@localhost:3310 
--slaves=rpl:pass@localhost:3311,rpl:pass@localhost:3312
#
# GTID differences between Master and Slaves:
# - Slave 'localhost@3311' is 15 transactions behind Master.
# - Slave 'localhost@3312' is 12 transactions behind Master.
#
# Checking data consistency.
#
# Using Master 'localhost@3310' as base server for comparison.
# Checking 'test_rplsync_db' database...
# - Checking 't0' table data...
# [OK] `test_rplsync_db`.`t0` checksum for server 'localhost@3311'.
# [OK] `test_rplsync_db`.`t0` checksum for server 'localhost@3312'.
# - Checking 't1' table data...
# [OK] `test_rplsync_db`.`t1` checksum for server 'localhost@3311'.
# [OK] `test_rplsync_db`.`t1` checksum for server 'localhost@3312'.
# Checking 'test_db' database... 42
Mysqlslavetrx -- skip bad transactions
shell> mysqlslavetrx --gtid-set=af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9 
--slaves=user:pass@localhost:3311,user:pass@localhost:3312
WARNING: Using a password on the command line interface can be insecure.
#
# GTID set to be skipped for each server:
# - localhost@3311: af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9
# - localhost@3312: af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9
#
# Injecting empty transactions for 'localhost:3311'...
# Injecting empty transactions for 'localhost:3312'...
#
#...done.
43

More Related Content

PDF
MySQL as a Document Store
Dave Stokes
 
PPTX
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Dave Stokes
 
PDF
MariaDB 10.5 binary install (바이너리 설치)
NeoClova
 
PDF
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
PDF
Introduction to MySQL InnoDB Cluster
I Goo Lee
 
PDF
Intro ProxySQL
I Goo Lee
 
ODP
MySQL 101 PHPTek 2017
Dave Stokes
 
PPTX
Query logging with proxysql
YoungHeon (Roy) Kim
 
MySQL as a Document Store
Dave Stokes
 
MySQL Utilities -- Cool Tools For You: PHP World Nov 16 2016
Dave Stokes
 
MariaDB 10.5 binary install (바이너리 설치)
NeoClova
 
MySQL Audit using Percona audit plugin and ELK
I Goo Lee
 
Introduction to MySQL InnoDB Cluster
I Goo Lee
 
Intro ProxySQL
I Goo Lee
 
MySQL 101 PHPTek 2017
Dave Stokes
 
Query logging with proxysql
YoungHeon (Roy) Kim
 

What's hot (20)

PDF
MySQL Document Store
I Goo Lee
 
PDF
Percona Live 2012PPT: introduction-to-mysql-replication
mysqlops
 
PPT
Oracle12c Pluggable Database Hands On - TROUG 2014
Özgür Umut Vurgun
 
PDF
MySQL Advanced Administrator 2021 - 네오클로바
NeoClova
 
PPTX
MySQL InnoDB Cluster 미리보기 (remote cluster test)
Seungmin Yu
 
PPTX
ProxySQL & PXC(Query routing and Failover Test)
YoungHeon (Roy) Kim
 
PDF
MySQL Multi-Source Replication for PL2016
Wagner Bianchi
 
PDF
Introduction to ClustrixDB
I Goo Lee
 
PPTX
MySQL Replication Overview -- PHPTek 2016
Dave Stokes
 
PPTX
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
PDF
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
M Malai
 
PDF
MySQL Group Replication
Manish Kumar
 
PDF
Introduction to MariaDB MaxScale
I Goo Lee
 
PDF
Replication Troubleshooting in Classic VS GTID
Mydbops
 
PDF
Galera Replication Demystified: How Does It Work?
Frederic Descamps
 
PPTX
Easy MySQL Replication Setup and Troubleshooting
Bob Burgess
 
PDF
Managing MariaDB Server operations with Percona Toolkit
Sveta Smirnova
 
PDF
The Full MySQL and MariaDB Parallel Replication Tutorial
Jean-François Gagné
 
PDF
MySQL Timeout Variables Explained
Mydbops
 
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
MySQL Document Store
I Goo Lee
 
Percona Live 2012PPT: introduction-to-mysql-replication
mysqlops
 
Oracle12c Pluggable Database Hands On - TROUG 2014
Özgür Umut Vurgun
 
MySQL Advanced Administrator 2021 - 네오클로바
NeoClova
 
MySQL InnoDB Cluster 미리보기 (remote cluster test)
Seungmin Yu
 
ProxySQL & PXC(Query routing and Failover Test)
YoungHeon (Roy) Kim
 
MySQL Multi-Source Replication for PL2016
Wagner Bianchi
 
Introduction to ClustrixDB
I Goo Lee
 
MySQL Replication Overview -- PHPTek 2016
Dave Stokes
 
ConFoo MySQL Replication Evolution : From Simple to Group Replication
Dave Stokes
 
Ansible is Our Wishbone(Automate DBA Tasks With Ansible)
M Malai
 
MySQL Group Replication
Manish Kumar
 
Introduction to MariaDB MaxScale
I Goo Lee
 
Replication Troubleshooting in Classic VS GTID
Mydbops
 
Galera Replication Demystified: How Does It Work?
Frederic Descamps
 
Easy MySQL Replication Setup and Troubleshooting
Bob Burgess
 
Managing MariaDB Server operations with Percona Toolkit
Sveta Smirnova
 
The Full MySQL and MariaDB Parallel Replication Tutorial
Jean-François Gagné
 
MySQL Timeout Variables Explained
Mydbops
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
Ad

Similar to MySQL Replication Basics -Ohio Linux Fest 2016 (20)

PDF
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
PPTX
MySQL Replication Evolution -- Confoo Montreal 2017
Dave Stokes
 
PDF
2012 replication
sqlhjalp
 
PDF
2012 scale replication
sqlhjalp
 
PPT
Mysql replication @ gnugroup
Jayant Chutke
 
PDF
MySQL database replication
PoguttuezhiniVP
 
PPT
Download presentation
webhostingguy
 
PDF
MySQL Proxy: Architecture and concepts of misuse
weigon
 
PPTX
Mysql-Basics.pptx
ssuserf5adce
 
ODP
Mysql
abhijith
 
PDF
Scaling MySQL -- Swanseacon.co.uk
Dave Stokes
 
DOCX
Mater,slave on mysql
Vasudeva Rao
 
PDF
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
Mark Swarbrick
 
PDF
MySQL Utilities -- PyTexas 2015
Dave Stokes
 
PDF
MySQL replication best practices 105-232-931
Baruch Osoveskiy
 
PPT
Download presentation
Rachit Gaur
 
PPT
Download presentation531
Indra Pratap
 
PPTX
My sql failover test using orchestrator
YoungHeon (Roy) Kim
 
PDF
My sql crashcourse_intro_kdl
sqlhjalp
 
PPTX
M|18 How Facebook Migrated to MyRocks
MariaDB plc
 
MySQL Replication Update -- Zendcon 2016
Dave Stokes
 
MySQL Replication Evolution -- Confoo Montreal 2017
Dave Stokes
 
2012 replication
sqlhjalp
 
2012 scale replication
sqlhjalp
 
Mysql replication @ gnugroup
Jayant Chutke
 
MySQL database replication
PoguttuezhiniVP
 
Download presentation
webhostingguy
 
MySQL Proxy: Architecture and concepts of misuse
weigon
 
Mysql-Basics.pptx
ssuserf5adce
 
Mysql
abhijith
 
Scaling MySQL -- Swanseacon.co.uk
Dave Stokes
 
Mater,slave on mysql
Vasudeva Rao
 
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
Mark Swarbrick
 
MySQL Utilities -- PyTexas 2015
Dave Stokes
 
MySQL replication best practices 105-232-931
Baruch Osoveskiy
 
Download presentation
Rachit Gaur
 
Download presentation531
Indra Pratap
 
My sql failover test using orchestrator
YoungHeon (Roy) Kim
 
My sql crashcourse_intro_kdl
sqlhjalp
 
M|18 How Facebook Migrated to MyRocks
MariaDB plc
 
Ad

More from Dave Stokes (20)

PDF
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Dave Stokes
 
PPTX
Locking Down Your MySQL Database.pptx
Dave Stokes
 
PPTX
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Dave Stokes
 
PDF
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
PDF
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Dave Stokes
 
PDF
Windowing Functions - Little Rock Tech fest 2019
Dave Stokes
 
PDF
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
PPTX
Develop PHP Applications with MySQL X DevAPI
Dave Stokes
 
PDF
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
Dave Stokes
 
PDF
The Proper Care and Feeding of MySQL Databases
Dave Stokes
 
PDF
MySQL without the SQL -- Cascadia PHP
Dave Stokes
 
PDF
MySQL 8 Server Optimization Swanseacon 2018
Dave Stokes
 
PDF
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Dave Stokes
 
PDF
Presentation Skills for Open Source Folks
Dave Stokes
 
PPTX
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
Dave Stokes
 
PPTX
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Dave Stokes
 
PDF
Advanced MySQL Query Optimizations
Dave Stokes
 
PPTX
Making MySQL Agile-ish
Dave Stokes
 
PPTX
PHP Database Programming Basics -- Northeast PHP
Dave Stokes
 
PDF
MySQL's JSON Data Type and Document Store
Dave Stokes
 
Valkey 101 - SCaLE 22x March 2025 Stokes.pdf
Dave Stokes
 
Locking Down Your MySQL Database.pptx
Dave Stokes
 
Linuxfest Northwest 2022 - MySQL 8.0 Nre Features
Dave Stokes
 
MySQL Indexes and Histograms - RMOUG Training Days 2022
Dave Stokes
 
MySQL 8.0 Features -- Oracle CodeOne 2019, All Things Open 2019
Dave Stokes
 
Windowing Functions - Little Rock Tech fest 2019
Dave Stokes
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
Dave Stokes
 
Develop PHP Applications with MySQL X DevAPI
Dave Stokes
 
MySQL 8 Tips and Tricks from Symfony USA 2018, San Francisco
Dave Stokes
 
The Proper Care and Feeding of MySQL Databases
Dave Stokes
 
MySQL without the SQL -- Cascadia PHP
Dave Stokes
 
MySQL 8 Server Optimization Swanseacon 2018
Dave Stokes
 
MySQL Without The SQL -- Oh My! PHP[Tek] June 2018
Dave Stokes
 
Presentation Skills for Open Source Folks
Dave Stokes
 
MySQL Without the SQL -- Oh My! Longhorn PHP Conference
Dave Stokes
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
Dave Stokes
 
Advanced MySQL Query Optimizations
Dave Stokes
 
Making MySQL Agile-ish
Dave Stokes
 
PHP Database Programming Basics -- Northeast PHP
Dave Stokes
 
MySQL's JSON Data Type and Document Store
Dave Stokes
 

Recently uploaded (20)

PPTX
How tech helps people in the modern era.
upadhyayaryan154
 
PPTX
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
PPT
1965 INDO PAK WAR which Pak will never forget.ppt
sanjaychief112
 
PPTX
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
PDF
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
PPTX
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
PPTX
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
PDF
Latest Scam Shocking the USA in 2025.pdf
onlinescamreport4
 
PDF
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
PPTX
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
PDF
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
PDF
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
PDF
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
PPTX
Different Generation Of Computers .pptx
divcoder9507
 
PPTX
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
PPTX
Google SGE SEO: 5 Critical Changes That Could Wreck Your Rankings in 2025
Reversed Out Creative
 
PDF
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
PPTX
Parallel & Concurrent ...
yashpavasiya892
 
PDF
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
PPTX
ppt lighfrsefsefesfesfsefsefsefsefserrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrt.pptx
atharvawafgaonkar
 
How tech helps people in the modern era.
upadhyayaryan154
 
EthicalHack{aksdladlsfsamnookfmnakoasjd}.pptx
dagarabull
 
1965 INDO PAK WAR which Pak will never forget.ppt
sanjaychief112
 
Perkembangan Perangkat jaringan komputer dan telekomunikasi 3.pptx
Prayudha3
 
LB# 820-1889_051-7370_C000.schematic.pdf
matheusalbuquerqueco3
 
Artificial-Intelligence-in-Daily-Life (2).pptx
nidhigoswami335
 
Blue and Dark Blue Modern Technology Presentation.pptx
ap177979
 
Latest Scam Shocking the USA in 2025.pdf
onlinescamreport4
 
LOGENVIDAD DANNYFGRETRRTTRRRTRRRRRRRRR.pdf
juan456ytpro
 
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
Generative AI Foundations: AI Skills for the Future of Work
hemal sharma
 
The Internet of Things (IoT) refers to a vast network of interconnected devic...
chethana8182
 
DNSSEC Made Easy, presented at PHNOG 2025
APNIC
 
Different Generation Of Computers .pptx
divcoder9507
 
B2B_Ecommerce_Internship_Simranpreet.pptx
LipakshiJindal
 
Google SGE SEO: 5 Critical Changes That Could Wreck Your Rankings in 2025
Reversed Out Creative
 
PDF document: World Game (s) Great Redesign.pdf
Steven McGee
 
Parallel & Concurrent ...
yashpavasiya892
 
KIPER4D situs Exclusive Game dari server Star Gaming Asia
hokimamad0
 
ppt lighfrsefsefesfesfsefsefsefsefserrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrt.pptx
atharvawafgaonkar
 

MySQL Replication Basics -Ohio Linux Fest 2016

  • 1. MySQL Replication Basics Ohio Linux Fest 2016 Dave Stokes [email protected] @stokes slideshare.net/davidmstokes 1
  • 2. Safe Harbor 2 THE FOLLOWING IS INTENDED TO OUTLINE OUR GENERAL PRODUCT DIRECTION. IT IS INTENDED FOR INFORMATION PURPOSES ONLY, AND MAY NOT BE INCORPORATED INTO ANY CONTRACT. IT IS NOT A COMMITMENT TO DELIVER ANY MATERIAL, CODE, OR FUNCTIONALITY, AND SHOULD NOT BE RELIED UPON IN MAKING PURCHASING DECISIONS. THE DEVELOPMENT, RELEASE, AND TIMING OF ANY FEATURES OR FUNCTIONALITY DESCRIBED FOR ORACLE'S PRODUCTS REMAINS AT THE SOLE DISCRETION OF ORACLE.
  • 3. Quick MySQL Catch Up 21 Years Old MySQL has been part of Oracle’s family of databases for six years. MySQL 8 MySQl 5.7 is the current release but the next version will be MySQL 8. Big feature is real time data dictionary Group Replication Active master-master replication. JSON A new native JSON datatype to store documents in a column of a table Document Store Programmers not know SQL but need a database? X Devapi allows them to use RDMS from language of choice Encryption Use Oracle Key Vault to encrypt your data at rest. 3
  • 5. Basic Premise Behind Replication 1. Copy all the data off one server and on to another. 2. Setup Replication. 3. Log any changes on the first server to a log file, apply that logfile to the second server. 5
  • 6. Imagine Your Data as a Painting: Make copy of Mona Lisa 6
  • 7. Make copy of changes to Mona Lisa 7
  • 8. Keep making copy of changes to Mona Lisa 8
  • 9. Architecture Binary Log on master Slave Attaches I/O Thread Grabs Data Data copied to slave Applier thread changes slave’s data 9
  • 10. Graphic overview of replication 10
  • 11. But what are we replicating? 11
  • 12. Replicating changes to tables and data SBR: When using statement-based binary logging, the master writes SQL statements to the binary log. Replication of the master to the slave works by executing the SQL statements on the slave. This is called statement-based replication (often abbreviated as SBR), which corresponds to the standard MySQL statement-based binary logging format. Replication capabilities in MySQL version 5.1.4 and earlier used this format exclusively. Structured Query Language (SQL) RBR: When using row-based logging, the master writes events to the binary log that indicate how individual table rows are changed. Replication of the master to the slave works by copying the events representing the changes to the table rows to the slave. This is called row-based replication (often abbreviated as RBR). The Delta 12
  • 13. OrBoth MBR: You can also configure MySQL to use a mix of both statement-based and row-based logging, depending on which is most appropriate for the change to be logged. This is called mixed-format logging. When using mixed-format logging, a statement-based log is used by default. Depending on certain statements, and also the storage engine being used, the log is automatically switched to row-based in particular cases. Replication using the mixed format is often referred to as mixed-based replication or mixed-format replication. 13
  • 14. Notes for the future With MySQL.5.6 RBR started sending over only the primary key and the changed data (not sending unchanged data) which can drastically cut the amount of data sent to slave servers. This can be huge!! Many future products will work better with RBR as it more deterministic. So plan accordingly. 14
  • 15. Threading Before 5.6 MySQL Replication is SINGLE threaded – Airline boarding example MySQL 5.6 is multi-threaded at the database level MySQL 5.7 is multi-threaded at the table level 15
  • 17. Async and Semi Synchronous Semi Synchronous replication -- a commit performed on the master blocks before returning to the session that performed the transaction until at least one slave acknowledges that it has received and logged the events for the transaction (Note not written to table, just recorded for future). Asynchronous replication -- slave servers retrieve data, master unaware of slave’s consumption. 17
  • 20. Common - Read / Write Split 20
  • 22. Group Replcation -- Labs.MySQL.Com for evaluation 22
  • 23. Multi-Master Lots of folks want TWO active masters and this can be done but not recommended, You need to have a sharp crew and plan for conflicts. Not generally recommended before Group Replication 23
  • 24. Multi-Master MySQL Cluster You can run active-active master-master with MySQL Cluster, even between data centers. This can be very expensive and MySQl Cluster is not a full featured version of the MySQL Server. 24
  • 25. Galera Cluster Layer separate from MySQL that is mainly for high availability (not high performance). Claims to have snapshot isolation on transactions but watch out for ‘first committer wins’ and prepare for rollbacks. Not low latency 25
  • 27. Two types of replication w/ & w/o GTIDs A global transaction identifier (GTID) is a unique identifier created and associated with each transaction committed on the server of origin (master). This identifier is unique not only to the server on which it originated, but is unique across all servers in a given replication setup. There is a 1-to-1 mapping between all transactions and all GTIDs. 3E11FA47-71CA-11E1-9E33-C80AA9429562:1-5 Note the 1-5 is a group of transactions 27
  • 28. Binlog & ID Enable Binary Log on Master, Unique ID number Create User Create user for replication Binlog Offset Get Master’s Binary Log coordinates Snapshot Snapshot data, copy onto slave SLAVE running CHANGE MASTER command and START SLAVE Before GTIDs (MySQL 5.5 and before) 28
  • 29. Enable Binary Log & Unique ID on Master Edit my.cnf file [mysqld] log-bin=mysql-bin server-id=1 29
  • 30. Create replication user mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%.mydomain.com'; 30
  • 31. Get binary log position mysql> FLUSH TABLES WITH READ LOCK; mysql > SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 73 | test | manual,mysql | +------------------+----------+--------------+------------------+ 31
  • 32. Unlock tables mysql> UNLOCK TABLES; 32
  • 33. Config slave & load data No config thee slave server. Remember server-id must be unique [mysqld] server-id=2 shell> mysql -h master < fulldb.dump 33
  • 34. Change Master & Start Slave mysql> CHANGE MASTER TO -> MASTER_HOST='master_host_name', -> MASTER_USER='replication_user_name', -> MASTER_PASSWORD='replication_password', -> MASTER_LOG_FILE='recorded_log_file_name', -> MASTER_LOG_POS=recorded_log_position; mysql> START SLAVE; 34
  • 35. Read only Sync both servers by setting to read if replication is already running Stop Servers Stop both servers Restart w/GTIDs Restart both servers with GTIDs enabled Change master Instruct the slave to use the master as the replication data source and to use auto-positioning, and then start the slave. Read Only off Remove read only With GTIDs (MySQL 5.6 and later) 35
  • 36. Replication Setup with GTIDs mysql> SET @@global.read_only = ON; mysql>mysqladmin -uusername -p shutdown shell> mysqld --gtid-mode=ON --enforce-gtid-consistency & mysql> CHANGE MASTER TO > MASTER_HOST = host, > MASTER_PORT = port, > MASTER_USER = user, > MASTER_PASSWORD = password, > MASTER_AUTO_POSITION = 1; mysql> START SLAVE; 36
  • 37. Finished! (Almost) [email protected] @stoker slideshare.net/davidmstokes 37 Hard to get a lot of info in in such a short time. Please get details from the MySQL Manual
  • 38. 30 Minutes!!! 30 Minutes! Can’t Cover all of MySQL Replication in 30 minutes!!!!! 38
  • 39. https://github.com/datacharmer Giuseppe Maxia has lot of great code for getting used to replication -- see ~/mysql-replication-sample 39 Great Place to Find examples & MySQL Sandbox
  • 40. MySQL Utilities FREE Scripts written in Python, used with MySQL Workbench or stand alone 1. Copy, diff databases 2. Disk usage, grants, copy users 3. Search for processed and kill ‘em 4. Setup replication and failover 40
  • 41. Mysqlrplcheck -- check replication setup shell> mysqlrplcheck --master=root@host1:3310 --slave=root@host2:3311 # master on host1: ... connected. # slave on host2: ... connected. Test Description Status ------------------------------------------------------------------------ Checking for binary logging on master [pass] Are there binlog exceptions? [pass] Replication user exists? [pass] Checking server_id values [pass] Is slave connected to master? [pass] Check master information file [pass] Checking InnoDB compatibility [pass] Checking storage engines compatibility [pass] Checking lower_case_table_names settings [pass] Checking slave delay (seconds behind master) [pass] # ...done. 41
  • 42. Mysqlrplcheck -- replication checker shell> mysqlrplsync --master=user:pass@localhost:3310 --slaves=rpl:pass@localhost:3311,rpl:pass@localhost:3312 # # GTID differences between Master and Slaves: # - Slave 'localhost@3311' is 15 transactions behind Master. # - Slave 'localhost@3312' is 12 transactions behind Master. # # Checking data consistency. # # Using Master 'localhost@3310' as base server for comparison. # Checking 'test_rplsync_db' database... # - Checking 't0' table data... # [OK] `test_rplsync_db`.`t0` checksum for server 'localhost@3311'. # [OK] `test_rplsync_db`.`t0` checksum for server 'localhost@3312'. # - Checking 't1' table data... # [OK] `test_rplsync_db`.`t1` checksum for server 'localhost@3311'. # [OK] `test_rplsync_db`.`t1` checksum for server 'localhost@3312'. # Checking 'test_db' database... 42
  • 43. Mysqlslavetrx -- skip bad transactions shell> mysqlslavetrx --gtid-set=af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9 --slaves=user:pass@localhost:3311,user:pass@localhost:3312 WARNING: Using a password on the command line interface can be insecure. # # GTID set to be skipped for each server: # - localhost@3311: af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9 # - localhost@3312: af6b22ee-7b0b-11e4-aa8d-606720440b68:7-9 # # Injecting empty transactions for 'localhost:3311'... # Injecting empty transactions for 'localhost:3312'... # #...done. 43