mysql replication hol v2 · pdf fileit is not a commitment to deliver any material, code, ......
TRANSCRIPT
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 1
MySQL Hands-On Lab:
Getting Started with MySQL Replication
Sven Sandberg, Luis Soares MySQL Replication Team
Presented by Ben Krug Senior MySQL Support Engineer
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 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.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 3
Agenda
Replication Basics Hands on Exercises to Set Up MySQL Replication More Resources
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 4
Replication Basics
MySQL Master Server - Changes data
- Logs changes (Events) into a file (Binary Log)
MySQL Slave Server
- Retrieves events from the master
- Replays the events on the slave's databases
MySQL Replication Components
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 5
Replication Basics
The Binary Log - File based log that records the changes on the master.
- Statement or Row based format (may be intermixed).
- Split into transactional groups.
BEGIN ... Ev1 Ev2 COMMIT
MySQL Replication Components: Binary Log
BEGIN ... Ev1 Ev2 COMMIT
Event Layout on a Binary Log File
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 6
Replication Basics MySQL Replication Components: Binary Log
Binary Log
Binary log files
Index
Under the Hood Binary log files: mysql-bin.000001, mysql-bin.000002, …
- The actual data.
Index: mysql-bin.index
- An index over the binary log files.
Log coordinate:
- binlog file name + event offset in the file (3.23.15+)
- Global Transaction Identifiers (5.6+)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 7
Session Dump
Binary log
I/O SQL
Relay log
Slave Master
Session Session
Replication Basics MySQL Replication Architecture
Master
info
Relay log info
I/O and SQL Thread Replication Metadata is stored in files. Starting in MySQL 5.6 the Metadata can be stored in tables.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 8
Replication Basics
Asynchronous Replication (MySQL 3.23.15+) - Transactions are committed and externalized without interaction with
the replication layer.
- Events are sent to slave after the commit operation is acknowledged.
- Faster but vulnerable to lost updates on server crashes and inconsistency.
- Built into the server.
Semi-synchronous Replication (MySQL 5.5+)
- Master commits transaction but waits until one slave acknowledges having received and stored the event before replying to the client.
Changes Propagation
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 9
Replication Basics
Hands-On
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 10
Replication Basics Getting MySQL
Head to - http://dev.mysql.com
... and get the latest MySQL 5.6 generic tar.gz package from there
… or alternatively, you can find MySQL 5.6.6 linux generic package at your local directory /opt/ .
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 11
Replication Basics Getting MySQL
$ tar xfvz /opt/mysql-....tar.gz --directory=$HOME/rephol
$ mkdir $HOME/rephol/ $ cd $HOME/rephol/
Create a directory for storing the binaries (e.g., under your home).
Unpack the package (e.g., using tar).
$ mv $HOME/rephol/mysql-... $HOME/rephol/mysql-5.6
Rename the directory for simplicity
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 12
Replication Basics Setting Up MySQL
$ mysql_install_db --basedir=/usr/ --datadir=$HOME/rephol/master --user=`whoami`
Create Two Data Directories
$ mysql_install_db --basedir=/usr/ --datadir=$HOME/rephol/slave --user=`whoami`
Create two Data
Directories
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 13
Replication Basics Setting Up MySQL Master Server
[mysqld] server-id=1 log-bin=master-bin log-error=master.err port=12000 socket=/tmp/master.sock
Create a defaults file called $HOME/rephol/master.cnf
Set the unique server id.
Turn ON the binary log.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 14
Replication Basics
$ mysqld --defaults-file=$HOME/rephol/master.cnf --lc-messages-dir=/usr/share/mysql --datadir=$HOME/rephol/master/
Start the master server.
$ mysql -u root --port 12000 --protocol=tcp --prompt='master> '
Log in to the master server.
Setting Up MySQL Master Server
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 15
Replication Basics Inspecting the Master Status
master> SHOW BINARY LOGS; ... master> SHOW BINLOG EVENTS; ... master> SHOW MASTER STATUS;
What binlog files are in use?
What's in the first binary log?
What binary log is in use what is its position?
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 16
master> SHOW BINARY LOGS;
master> SHOW BINLOG EVENTS;
master> SHOW MASTER STATUS;
Replication Basics Inspecting the Master Status
+-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 120 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
+-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | master-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.6-m9-log, Binlog ver: 4 | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ 1 row in set (0.00 sec)
+-------------------+-----------+ | Log_name | File_size | +-------------------+-----------+ | master-bin.000001 | 120 | +-------------------+-----------+ 1 row in set (0.00 sec)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 17
Replication Basics Binary Log Layout
Log Events
Format description
Rotate
Log Events
Format description
Rotate
Log Events
Format description Multiple Files.
Files begin with Format Description event.
Each log file ends with a Rotate event.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 18
Replication Basics Setting Up Replication User (that the slave will connect to the master with)
master> CREATE USER `rpl_user`@`localhost`; master> GRANT REPLICATION SLAVE ON *.* TO `rpl_user`@`localhost` IDENTIFIED BY 'secret'; master> FLUSH PRIVILEGES;
We could use any user.
Needs replication grants to read any change
on the master
Better use a dedicated
user for connecting the slave.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 19
Replication Basics Setting Up MySQL Slave Server
[mysqld] server-id=2 relay-log=slave-relay-bin log-error=slave.err port=12001 socket=/tmp/slave.sock
Set the unique server id.
Set the name for the relay log.
Create a defaults file called $HOME/rephol/slave.cnf
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 20
Replication Basics Starting Up The MySQL Slave Server
$ mysqld --defaults-file=$HOME/rephol/slave.cnf --lc-messages-dir=/usr/share/mysql --datadir=$HOME/rephol/slave/
Start the slave server.
$ mysql -u root --port 12001 --protocol=tcp --prompt='slave> '
Log in to the slave server.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 21
Replication Basics Starting the Slave Threads.
slave> CHANGE MASTER TO MASTER_HOST = 'localhost', MASTER_PORT = 12000, MASTER_USER = 'rpl_user', MASTER_PASSWORD = 'secret';
Point the slave server to the master
server.
Use the credentials we had granted
before.
slave> START SLAVE; Start the slave
threads.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 22
Replication Basics Inspecting the Slave Status
slave> SHOW SLAVE STATUS\G
Inspect the slave status.
(...) Slave_IO_State: Waiting for master to send event Master_Host: localhost Master_User: rpl_user Master_Port: 12000 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 120 Relay_Log_File: slave-relay-bin.000003 Relay_Log_Pos: 284 Relay_Master_Log_File: master-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes (...)
Both slave threads are up and running!
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 23
Replication Basics Replicating From Master to Slave.
master> USE test; master> CREATE TABLE t1 (a INT); slave> USE test; slave> SHOW TABLES; master> INSERT INTO t1 VALUES(1); slave> SELECT * FROM t1;
Use the test db.
Create a table.
Slave has replicated the table.
Slave has replicated the data.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 24
Replication Basics What's in the Binary Log?
master> SHOW BINLOG EVENTS [IN 'master-bin.0000002'];
+-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | master-bin.000002 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.6-m9-log, Binlog ver: 4 | | master-bin.000002 | 120 | Query | 1 | 217 | use `test`; CREATE TABLE t1 (a INT) | | master-bin.000002 | 217 | Query | 1 | 296 | BEGIN | | master-bin.000002 | 296 | Query | 1 | 395 | use `test`; INSERT INTO t1 VALUES (1) | | master-bin.000002 | 395 | Xid | 1 | 426 | COMMIT /* xid=21 */ | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ 5 rows in set (0.00 sec)
The “CREATE TABLE” statement.
The “INSERT” statement.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 25
Replication Basics Replicating From Master to Slave – binary log formats.
• Three formats: - Statement – every change logged as a statement.
Reexecuted on the slave. - Row – every change logged as data.
Data changes are applied on the slave. - MIXED – every change logged either as data or statements.
Automatically switches from statement to row on non-deterministic statements.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 26
Replication Basics Replicating From Master to Slave – binary log formats.
master> set binlog_format=ROW; master> INSERT INTO t1 VALUES(2); master> SHOW BINLOG EVENTS; +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ | master-bin.000002 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.6-m9-log, Binlog ver: 4 | | master-bin.000002 | 120 | Query | 1 | 217 | use `test`; CREATE TABLE t1 (a INT) | | master-bin.000002 | 217 | Query | 1 | 296 | BEGIN | | master-bin.000002 | 296 | Query | 1 | 395 | use `test`; INSERT INTO t1 VALUES (1) | | master-bin.000002 | 395 | Xid | 1 | 426 | COMMIT /* xid=21 */ | | master-bin.000002 | 426 | Query | 1 | 498 | BEGIN | | master-bin.000002 | 498 | Table_map | 1 | 543 | table_id: 70 (test.t1) | | master-bin.000002 | 543 | Write_rows | 1 | 583 | table_id: 70 flags: STMT_END_F | | master-bin.000002 | 583 | Xid | 1 | 614 | COMMIT /* xid=27 */ | +-------------------+-----+-------------+-----------+-------------+-----------------------------------------+ 9 rows in set (0.00 sec)
Let's change the format.
The second “INSERT” statement.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 27
Replication Basics Master replication files.
$ ls -la master/
(...) -rw-rw---- 1 XXXXXX XXXXXX 143 Sep 26 11:10 master-bin.000001 -rw-rw---- 1 XXXXXX XXXXXX 614 Sep 26 11:40 master-bin.000002 -rw-rw---- 1 XXXXXX XXXXXX 40 Sep 26 11:10 master-bin.index (...)
The binary log files.
Index file over the existing binary log files.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 28
Replication Basics Slave replication files.
$ ls -la slave/
(...) -rw-rw---- 1 XXXXXXX XXXXXXX 128 Sep 26 11:40 master.info (...) -rw-rw---- 1 XXXXXXX XXXXXXX 57 Sep 26 11:40 relay-log.info (...) -rw-rw---- 1 XXXXXXX XXXXXXX 337 Sep 26 11:10 slave-relay-bin.000002 -rw-rw---- 1 XXXXXXX XXXXXXX 778 Sep 26 11:40 slave-relay-bin.000003 -rw-rw---- 1 XXXXXXX XXXXXXX 50 Sep 26 11:10 slave-relay-bin.index (...)
The relay log files.
Index file over the existing binary log files.
Persists IO thread replication metadata (master host, username, … and positioning on the master's binlog).
Persists SQL thread replication metadata. Basically, the positioning in the relay log.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 12 29
More Resources White Paper: MySQL Replication: An Introduction http://www.mysql.com/why-mysql/white-papers/mysql-replication-introduction/ White Paper: MySQL Replication Tutorial: Configuration, Provisioning and
Management http://www.mysql.com/why-mysql/white-papers/mysql-replication-tutorial/ Documentation: Replication http://dev.mysql.com/doc/refman/5.6/en/replication.html MySQL Forums: Replication http://forums.mysql.com/list.php?26