Basic Knowledge on MySql Replication

Preview:

DESCRIPTION

Mysql Replication Basics

Citation preview

MySQL REPLICATION

Shishir Kumar SahaSoftware EngineerTasawr Interactive

shishir@tasawr.com

What is replication?

Copying data from one instance of MySQL to another where all results are guaranteed to be the same on both end

Atomic

Advantages

Robustness Load Balancing Easy Backup

MySQL Replication Types

Master-Slave Replication Master-Master Circular Replication

Archetecture

Master - Connective Slaves - Connective Masters

Slaves - I/O Threads - SQL Threads

Control Files Master

- hostname-bin.001 - hostname-bin.index Slave - master.info - relay-log.info - hostname-relay-bin.001

- hostname-relay-bin.index

Master Setup Login to Master Database Server as root Set up an account for replication

- mysql>GRANT REPLICATION SLAVE ON * TO "slave" IDENTIFIED BY "slave_password";

Make a file named my.cnf (/etc/my.cnf) with contents –

For Simple Master-Slave Replication:[mysqld]

log_binserver-id=100

Master Setup Cont. For Master-Master Circular Replication: grant all privileges on sff_production.* to 'sff_159'@'74.50.56.157' identified by ‘sff_pass'; grant all privileges on sff_production.* to 'sff_159'@'74.50.56.158' identified by ‘sff_pass';

GRANT SUPER, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'sff_159'@'74.50.56.157, 74.50.56.158, 74.50.56.159';

server-id = 30log-bin = /var/log/mysql/mysql-bin.loglog-slave-updatesreplicate-same-server-id = 0auto_increment_increment = 10auto_increment_offset = 3master-host = 74.50.56.158master-user = sff_158master-password = sff_passreport-host = 74.50.56.159replicate-do-db = sff_productionsync_binlog = 1slave_exec_mode = IDEMPOTENT

Restart MySql Server

Snapshot

rsync/scp copy a slave mysqldump

Slave Setup Insert data/copy data into place Configure my.cnf file For Simple Master-Slave Replication:

[mysqld] log_binserver-id=101

For Master-Master Circular Replication:Same as master server

Set master position,change master to MASTER_HOST=‘master ip', MASTER_USER='client', MASTER_PASSWORD='abc', master_log_file=’ localhost-bin.001’,master_log_pos=79;

Start replication

SQL for Slave

Stop Slave Start Slave Show Slave Status

What if it breaks?

SET GLOBAL SQL_SLAVE_SKIP_COUNTER = n

Check MySql binlog Delete the relay logs Restart Replication

Monitor Replication Status (Master)mysql> show processlist;+----+---------+-----------------------+------+-------------+------

+----------------------------------------------------------------+------------------+| Id | User | Host | db | Command | Time | State

| Info |+----+---------+-----------------------+------+-------------+------

+----------------------------------------------------------------+------------------+| 1 | root | localhost | NULL | Query | 0 | NULL

| show processlist || 2 | client1 | 202.161.134.117:32777 | NULL | Binlog Dump | 419 |

Has sent all binlog to slave; waiting for binlog to be updated | NULL |

+----+---------+-----------------------+------+-------------+------+----------------------------------------------------------------+---------

Monitor Replication Status (Master)mysql> show master status;

+----------------------+-----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+----------------------+-----------+--------------+------------------+| localhost-bin.000001 | 957574845 | | | +----------------------+-----------+--------------+------------------+

Monitor Replication Status (Slave)mysql> show processlist; +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+| 2 | system user | | NULL | Connect | 406 | Waiting for master to send event | NULL | | 3 | system user | | NULL | Connect | 861 | Has read all relay log; waiting for the slave I/O thread to update it | NULL | | 4 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+--------------

Monitor Replication Status (Slave)mysql> show slave status;+----------------------------------+---------------+-------------+-------------+---------------+----------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |+----------------------------------+---------------+-------------+-------------+---------------+----------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+| Waiting for master to send event | 74.50.56.157 | gent333 | 3306 | 60 | localhost-bin.000001 | 958545954 | localhost-relay-bin.000004 | 56204926 | localhost-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 958545954 | 56204926 | None | | 0 | No | | | | | | 0 | +----------------------------------+---------------+-------------+-------------+---------------+----------------------+---------------------+----------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+

Question?

Recommended