You are on page 1of 46

Advanced MySQL

Replicate & Optimize !

PHPSCHOOL

loveme@phpschool.com
1

http://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 Layout
2

http://phpschool.com

About me

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

http://phpschool.com

Quick Survey
MySQL
MySQL
3.23 vs. 4.0


PHP, C/C++, Perl ,Java

Replication

http://phpschool.com

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


local remote

Very Fast
Reliable
Scalable
Inexpensive
Oracle
5

http://phpschool.com

What is MySQL?
Full-text Search Indexing
2byte

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

http://phpschool.com

Why MySQL?
Speed
MySQL DB

Features


Limit, full-text ,

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

http://phpschool.com

Why MySQL?
Simplicity
(10 )
DBA

Cost
H/W

Scale
Replication

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 (?)

http://phpschool.com

MySQL Architecture
Connection Management/Security

SQL Parsing/Execution/Caching
Storage Engines

MyISAM

InnoDB

HEAP

NDB

(default)

(transactions)

(in-memory)

(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 !
12

http://phpschool.com

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

transaction
Foreign key

Transaction

13

http://phpschool.com

InnoDB Tables

tablespace

Row
Redo/Undo

Transaction
transaction



Rows
buffer pool
14

http://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.html
17

http://phpschool.com

Replication process
Master

Slave
BINLOG

Client

Logging

Insert
Update
Delete

1.Insert
2.Update
3.Delete
4..

Insert
Update
Delete

IO Thread

SQL Thread

DB/Tables

Insert
Update
Delete

DB/Tables

18

http://phpschool.com

Replication situation
[Master]
[root@db 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]
[root@www1 data]# ls *relay* master*
master.info
www1-relay-bin.009
relay-log.info www1-relay-bin.index
[root@www1 data]#

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

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> show master status ;


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

mysql>

19

http://phpschool.com

Replication Layout
Master/Slave Replication

Master

Slave

Slave

20

Slave

http://phpschool.com

Replication Layout
Dual-Master Replication

Auto-Increment
primary

Master

Master

21

http://phpschool.com

Replication Layout
Complex Replication
Master

Master

Slave

Slave

Slave

Slave

Slave

22

http://phpschool.com

Replication Layout
Two Data
Local Master

Master

Slave

Slave

Slave

23

Slave

http://phpschool.com

L4 Load Balancing
L4 Switch
Read Connection

Slave

Slave

Slave
Write Connection

Master
24

http://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 repl@'%'
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 repl@'%'
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
27

http://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.
29

http://phpschool.com

Replication setup

6. master
slave start data
. .
7. slave start
1) 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 .c
30

http://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.html
31

http://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)
33

http://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.com

Troubleshooting

,

Replication
Slave
Slave
http://www.mysql.com/doc/en/Replication_Features.html

Slave

35

http://phpschool.com

Troubleshooting

CPU 100%

DNS



I/O

RAID

36

http://phpschool.com

Monitoring
OS
Disk
CPU / Memory

MySQL
Performance (queries/sec)
Cache
Connections

37

http://phpschool.com

MySQL monitoring
SHOW FULL PROCESSLIST
clients

SHOW STATUS
MySQL
,

SHOW VARIABLES
( )

mytop
MySQL
38

http://phpschool.com

MySQL monitoring
Mytop :

http://jeremy.zawodny.com/mysql/mytop/

39

http://phpschool.com

MySQL monitoring
Mytop : Command stats

40

http://phpschool.com

MySQL monitoring
mysqlrrd

41

http://phpschool.com

MySQL monitoring
#mysqladmin -uroot -plove -i3 status

42

http://phpschool.com

MySQL monitoring
class of mysql status -

43

http://linuxchannel.net/

http://phpschool.com

PHPSCHOOL System Layout


WWW1

WWW2

www1 / www2
P4 CPU 1.8 GHz
512 M / 40 G
Red Hat 8.0

HTML Data
RSYNC

MySQL
Replication

TCP/IP Sockets

DNS / backup

MySQL DBMS
MySQL

Cool Backup
DNS / backup
P4 1.3GHz
256 M / 40 G
Red Hat 8.0

DBMS
AMD Opteron 1.4G
RAM 2 GByte
SCSI 40G
Red Hat 8.0 x86_64

44

http://phpschool.com

Q&A

45

http://phpschool.com

loveme@phpschool.com

46

http://phpschool.com

You might also like