Replikering MySQL 5.1

From Linuxwiki
Jump to navigation Jump to search

Sätta upp replikering (mysql v5.1

Inställningar på klient (del 1)

Skapa en replikeringsanvändare på slaven:

mysql> CREATE USER 'repl'@'%.mydomain.com' IDENTIFIED BY 'slavepass';
mysql> GRANT REPLICATION SLAVE ON *.*
    -> TO 'repl'@'%.mydomain.com';

Inställningar på master (del 1)

Redigera my.cnf att hantera binloggning och sätt ett unikt värde på servern:

[mysqld]
log-bin=mysql-bin
server-id=1

Notering från MySQL.com: For the greatest possible durability and consistency in a replication setup using InnoDB with transactions, you should use innodb_flush_log_at_trx_commit=1 and sync_binlog=1 in the master my.cnf file.

Ensure that the skip-networking option is not enabled on your replication master. If networking has been disabled, your slave will not able to communicate with the master and replication will fail.

Lås tabellerna på master genom att på denna mata in:

FLUSH TABLES WITH READ LOCK;

For InnoDB tables, note that FLUSH TABLES WITH READ LOCK also blocks COMMIT operations.

Warning! Leave the client from which you issued the FLUSH TABLES statement running so that the read lock remains in effect. If you exit the client, the lock is released.

Sedan behöver man ta reda på vart slaven ska börja läsa datat:

SHOW MASTER STATUS;
+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 73       | test         | manual,mysql     |
+---------------+----------+--------------+------------------+

Inställning klient (del 2)

Dessa värden ska matas in i slaven:

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;

Det blir alltså i exemplet:
CHANGE MASTER TO
        MASTER_HOST='DNS eller IP till master', 
        MASTER_USER='repl', 
        MASTER_PASSWORD='slavepass', 
        MASTER_LOG_FILE='mysql-bin.003', 
        MASTER_LOG_POS=73; <- Notera att inga fnuttar ska vara runt positionen.

Sätt ett unikt värde för server även på slavens my.cnf:

[mysqld]
server-id=2

Glöm inte starta slaven med:

START SLAVE;

Kontrollera status med:

SHOW SLAVE STATUS\G

Inställning på master (del 2)

När detta är gjort kan man släppa låset på masters tabeller:

UNLOCK TABLES;

Ändring av logfil och position

Om man av någon anledning behöver ändra logfil och position så att slaven börjar hämta data "på annat ställe" från master räcker det med att uppdatera MASTER_LOG_FILE och MASTER_LOG_POS på slaven:

STOP SLAVE;
CHANGE MASTER TO
        MASTER_LOG_FILE='mysql-bin.007',
        MASTER_LOG_POS='89344';
START SLAVE;