Top Banner

Click here to load reader

Mysql Replication[1]

Oct 26, 2014

ReportDownload

Documents

Advanced MySQL Replicate & Optimize !

PHPSCHOOL [email protected]://phpschool.com

Contents Quick Survey What is MySQL? MySQL Architecture MySQL Tables Replication Replication process Replication situation Replication Layout L4 Load Balancing Replication setup Troubleshooting MySQL monitoring PHPSCHOOL System Layout2http://phpschool.com

About me http://phpschool.com administrator Using MySQL : Since 1998 Yahoo! Korea Communications team In charge of Kr.club.yahoo.com

3

http://phpschool.com

Quick Survey MySQL MySQL 3.23 vs. 4.0

PHP, C/C++, Perl ,Java

Replication

4

http://phpschool.com

What is MySQL? RDBMS(Relational Database Server) SQL , , local remote

Very Fast Reliable Scalable Inexpensive Oracle 5http://phpschool.com

What is MySQL? Full-text Search Indexing 2byte

Replication Query Cache UNIONs SSL (Encrypted connections) Multi-Table Deletes

6

http://phpschool.com

Why MySQL? Speed MySQL DB

Features Limit, full-text ,

API C, C++, C#, Java, Perl, PHP, Python, Ruby,

7

http://phpschool.com

Why MySQL? Simplicity (10 ) DBA

Cost H/W

Scale Replication

8

http://phpschool.com

TODO list Replication over SSL (4.1) Multiple Character Sets (4.1) Sub-queries (4.1) Views (5.x) Stored Procedures (5.x) XML Output/Query (?)

9

http://phpschool.com

MySQL ArchitectureConnection Management/Security

SQL Parsing/Execution/Caching Storage Engines

MyISAM(default)

InnoDB(transactions)

HEAP(in-memory)

NDB(clustered)

10

http://phpschool.com

MyISAM Tables Table Locking /

Logging applications

11

http://phpschool.com

MyISAM Tables DB /usr/local/mysql/data/PHPSCHOOL 3 mytable.frm mytable.MYI mytable.MYD

Rows !12http://phpschool.com

InnoDB Tables Oracle row-level locks / Non-locking SELECT

transaction Foreign key Transaction 13http://phpschool.com

InnoDB Tables tablespace Row Redo/Undo

Transaction transaction

Rows buffer pool14http://phpschool.com

HEAP Tables memory

HEAP CREATE TEMPORARY TABLE

15

http://phpschool.com

NDB Tables Alzato NDB / http://www.mysql.com/press/release_2003_30.html MySQL AB Acquires Alzato

16

http://phpschool.com

Replication MySQL LOG Replication Master binary log ( = binlog = replication log )

Slaves binlog IO (relay) thread SQL thread

Replication Master/slave Master/Master (dual-master) Master/slave/slave (chained slaves)

http://www.mysql.com/doc/en/Replication.html17http://phpschool.com

Replication processMaster BINLOG Client Logging 1.Insert 2.Update 3.Delete 4.. IO Thread Slave

Insert Update DeleteInsert Update Delete

SQL Thread

Insert Update Delete

DB/Tables

DB/Tables

18

http://phpschool.com

Replication situation[Master][[email protected] data]# ls db* db-bin.001 db-bin.010 db-bin.002 db-bin.011 db-bin.003 db-bin.012 db-bin.004 db-bin.013 db-bin.005 db-bin.014 db-bin.006 db-bin.015 db-bin.007 db-bin.016 db-bin.008 db-bin.017 db-bin.009 db-bin.018

[Slave]db-bin.019 db-bin.020 db-bin.021 db-bin.022 db-bin.023 db-bin.index db-slow.log db.phpschool.com.err db.phpschool.com.pid[[email protected] data]# ls *relay* master* master.info www1-relay-bin.009 relay-log.info www1-relay-bin.index [[email protected] data]# mysql> show slave status \G *************************** 1. row *************************** Master_Host: 210.118.169.50 Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: db-bin.023 Read_Master_Log_Pos: 111148 Relay_Log_File: www1-relay-bin.009 Relay_Log_Pos: 778804537 Relay_Master_Log_File: db-bin.023 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: PHPSCHOOL,LOVESERA Replicate_ignore_db: mysql.user Last_errno: 0 Last_error: Skip_counter: 0 Exec_master_log_pos: 111148 Relay_log_space: 778804537 1 row in set (0.00 sec) mysql>

mysql> show master status ; +------------+----------+--------------+------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +------------+----------+--------------+------------------+ | db-bin.023 | 112198 | | | +------------+----------+--------------+------------------+ 1 row in set (0.00 sec) mysql>

19

http://phpschool.com

Replication Layout Master/Slave Replication

Master

Slave

Slave

Slave

20

http://phpschool.com

Replication Layout Dual-Master Replication Auto-Increment primary

Master

Master

21

http://phpschool.com

Replication Layout Complex ReplicationMaster Master

Slave

Slave

Slave

Slave22

Slavehttp://phpschool.com

Replication Layout Two Data Master Local Master

Slave

Slave

Slave

Slave

23

http://phpschool.com

L4 Load BalancingL4 SwitchRead Connection

Slave

Slave

SlaveWrite Connection

Master24http://phpschool.com

Replication setup 1. MySQL # # # # # # # # # # # groupadd mysql useradd -g mysql mysql cd /usr/local gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf ln -s full-path-to-mysql-VERSION-OS mysql cd mysql scripts/mysql_install_db chown -R root . chown -R mysql data chgrp -R mysql . bin/safe_mysqld --user=mysql &

25

http://phpschool.com

Replication setup 2. Master repl mysql> GRANT REPLICATION SLAVE ON *.* TO [email protected]'%' IDENTIFIED BY 'replpasswd'; Query OK, 0 rows affected (0.00 sec)

LOAD TABLE FROM MASTER LOAD DATA FROM MASTER .mysql> GRANT SUPER , RELOAD ON *.* TO [email protected]'%' IDENTIFIED BY 'replpasswd';

26

http://phpschool.com

Replication setup 3. master snap-shot 3.1 READ LOCK .mysql> FLUSH TABLES WITH READ LOCK;

3.2 # tar cvfpz ./master_snap.tgz . # tar cvfpz ./master_snap.tgz ./PHPSCHOOL( DB repl.)

master.info ,relay-log.info , replication slave 27http://phpschool.com

Replication setup 3.3 binlog mysql> SHOW MASTER STATUS; +------------+----------+--------------+------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +------------+----------+--------------+------------------+ | db-bin.004 | 7138| | | +------------+----------+--------------+------------------+ 1 row in set (0.00 sec)

3.4 Lock .mysql> UNLOCK TABLES;

3.5 Slave .

28

http://phpschool.com

Replication setup 4. Master binlog . /etc/my.cnf . [mysqld] log-bin server-id=1

mysql.server restart .

5. slave my.cnf [mysqld] server-id=2

: master . slave . 1.29http://phpschool.com

Replication setup 6. master slave start data . . 7. slave start1) repl mysql> CHANGE MASTER TO MASTER_HOST='210.118.169.99', MASTER_USER='repl', MASTER_PASSWORD='replpasswd', MASTER_LOG_FILE='db-bin.004', MASTER_LOG_POS=7138 ; mysql> start slave;

data/master.info .c30http://phpschool.com

Replication setup 2) /etc/my.cnf mysql.server start (master.info )#vi my.cnf .... server-id = 2 master-host = 210.118.169.50 master-user = repl master-password = repl master-port = 3306 slave-skip-errors=all replicate-ignore-db = mysql.user replicate-do-db = PHPSCHOOL replicate-ignore-table = PHPSCHOOL.adviews .... # mysql.server start

* http://www.mysql.com/doc/en/Replication_Options.html31http://phpschool.com

Replication setup 8. Master/Slave Status [master]mysql> show master status; +------------+----------+--------------+------------------+ | File | Position | Binlog_do_db | Binlog_ignore_db | +------------+----------+--------------+------------------+ | db-bin.004 | 7032623 | | | +------------+----------+--------------+------------------+ 1 row in set (0.00 sec)

32

http://phpschool.com

Replication setup [slave]mysql> show slave status \G *************************** 1. row *************************** Master_Host: 210.118.169.** Master_User: repl Master_Port: 3306 Connect_retry: 60 Master_Log_File: db-bin.004 Read_Master_Log_Pos: 11595623 Relay_Log_File: www1-relay-bin.002 Relay_Log_Pos: 427515 Relay_Master_Log_File: db-bin.004 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_do_db: PHPSCHOOL Replicate_ignore_db: mysql.user Last_errno: 0 Last_error: Skip_counter: 0 Exec_master_log_pos: 11595623 Relay_log_space: 427515 1 row in set (0.00 sec)33http://phpschool.com

Replication setup 9. process list [master]mysql> SHOW PROCESSLIST ; +-------+--------+-----------+--------+-------------+------+----------------------------------+--------+ | Id | User | Host | db | Command | Time | State | Info | +-------+--------+-----------+--------+-------------+------+----------------------------------+--------+ | 44980 | repl | p2:36630 | NULL | Binlog Dump | 283 | Slave: waiting for binlog update | NULL | ....

[slave]mysql> SHOW PROCESSLIST \G *************************** 1. row *************************** Id: 1 User: system user Host: db: NULL Command: Connect Time: 3028861 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 2 User: system user Host: db: NULL Command: Connect Time: 4294967272 State: Has read all relay log; waiting for the I/O slave thread to update it Info: NULL

34

http://phpschool.