camscape - for excellent IT solutions itkb.ro - IT knowledge base

linux :: mysql master-slave sau master-master

Cristian
Cristian M.
TitleMySQL Master-Slave sau Master-Master
TagsMySql,master-slave,master-master,multi master
Desc.MySQL Master-Slave sau Master-Master
CodeKBLN0032 v1.0
Date 9 mai 2015

Avem doua servere:

 

SRV1 IP: 1.1.1.1

SRV2 IP: 2.2.2.2

 

si dorim sa avem un sistem de replicare a bazelor de date MySQL, folosind SRV1 ca master, SRV2 ca slave.

 

 

Pe SRV1 (MySQL MASTER), se modifica my.cnf :

 

# Numar unic in cadrul serverelor ce vor fi sincronizate
server-id = 1

# IP-ul MASTER, lasati comentat pentru MASTER-SLAVE
# decomentati pentru MASTER-MASTER
#master-host = 2.2.2.2

# User de replicare, lasati comentat pentru MASTER-SLAVE
# decomentati pentru MASTER-MASTER
#master-user = repuser

# Parola de replicare, lasati comentat pentru MASTER-SLAVE
# decomentati pentru MASTER-MASTER
#master-password = PaRoLa

log-bin = mysql-bin

auto_increment_increment = 2
auto_increment_offset = 2

relay-log = /public/data/mysql/slave-relay.log
relay-log-index = /public/data/mysql/slave-relay.index

expire_logs_days = 10

max_binlog_size = 500M

replicate-same-server-id = 0

sync_binlog = 1
log-slave-updates = 1
skip-slave-start = 0

# Lista bazelor de date care se sincronizeaza, cate una pe linie
binlog_do_db = baza_de_date_1
binlog_do_db = baza_de_date_2
binlog_do_db = baza_de_date_3
binlog_do_db = baza_de_date_4

 

Salvati si reporniti MySQL. Asigurati-va ca bind-address este comentata.

 

 

Pe SRV2 (MySQL SLAVE), se modifica my.cnf :

 

# Numar unic in cadrul serverelor ce vor fi sincronizate
server-id = 2

# IP-ul MASTER
master-host = 1.1.1.1

# User de replicare
master-user = repuser

# Parola de replicare
master-password = PaRoLa

log-bin = mysql-bin

auto_increment_increment = 2
auto_increment_offset = 2

relay-log = /public/data/mysql/slave-relay.log
relay-log-index = /public/data/mysql/slave-relay.index

expire_logs_days = 10

max_binlog_size = 500M

replicate-same-server-id = 0

sync_binlog = 1
log-slave-updates = 1
skip-slave-start = 0

# Lista bazelor de date care se sincronizeaza, cate una pe linie
binlog_do_db = baza_de_date_1
binlog_do_db = baza_de_date_2
binlog_do_db = baza_de_date_3
binlog_do_db = baza_de_date_4

 

Salvati si reporniti MySQL. Asigurati-va ca bind-address este comentata.

 

 

Acum, cream pe SRV1 si SRV2, pe rand, userul de replicare:

 

mysql -u root -p
Password: *******
> CREATE USER `repuser`@`%` IDENTIFIED BY `PaRoLa`;
> GRANT REPLICATION SLAVE ON *.* TO `repuser`@`%`;
> FLUSH PRIVILEGES;

 

 

Suntem gata sa incepem replicarea. Pe SRV1:

 

mysql -u root -p
Password: *******
> SHOW MASTER STATUS;

 

veti primi ceva asemanator:

 

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      408 | .......      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 

 

Cu aceasta informatie, pe SRV2:

 

mysql -u root -p
Password: *******
> SLAVE STOP;
> CHANGE MASTER TO MASTER_HOST = `1.1.1.1`, MASTER_USER = `repuser`, \
  MASTER_PASSWORD = `PaRoLa`, MASTER_LOG_FILE = `mysql-bin.000005`, \
  MASTER_LOG_POS = 408; 
SLAVE START;

 

Daca ati optat pentru un MASTER-MASTER (vezi configuratia din my.cnf de pe SRV1), desi NU RECOMAND, atunci, pe SRV1:

 

mysql -u root -p
Password: *******
> SLAVE STOP;
> CHANGE MASTER TO MASTER_HOST = `2.2.2.2`, MASTER_USER = `repuser`, \
  MASTER_PASSWORD = `PaRoLa`, MASTER_LOG_FILE = `mysql-bin.000005`, \
  MASTER_LOG_POS = 408; 
SLAVE START;