User Tools

Site Tools


doc:appunti:linux:sa:mysql_replica_master_master

This is an old revision of the document!


MySQL replica master-master

Configurare la replica master-master

Con questa configurazione si tiene replicato un solo database.

-- Su server1
GRANT REPLICATION SLAVE ON *.* TO 'slave2_user'@'%' IDENTIFIED BY 'slave2_password';
FLUSH PRIVILEGES;

CREATE DATABASE exampledb;
CONNECT exampledb;

CREATE TABLE clienti (
    id INTEGER NOT NULL AUTO_INCREMENT,
    name CHAR(100),
    PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE fatture (
    id INTEGER NOT NULL AUTO_INCREMENT,
    cliente_id INTEGER,
    PRIMARY KEY (id),
    FOREIGN KEY (cliente_id) REFERENCES clienti(id)
) ENGINE=INNODB;

SHOW TABLE STATUS;

INSERT INTO clienti (name) VALUES ('Rigacci.Org');
INSERT INTO fatture (cliente_id) VALUES (1);
DELETE FROM clienti;

vi /etc/mysql/my.cnf
# Vedere la man page di my.cnf, che non esiste.
/etc/init.d/mysql restart

-- Su server2
GRANT REPLICATION SLAVE ON *.* TO 'slave1_user'@'%' IDENTIFIED BY 'slave1_password';
FLUSH PRIVILEGES;

vi /etc/mysql/my.cnf
# Vedere la man page di my.cnf, che non esiste.
/etc/init.d/mysql restart

Per vedere se si sono assegnati i permessi di replica:

SHOW GRANTS FOR 'slave2_user'@'%';

Ovviamente è opportuno concedere i grant non dall'indirizzo IP wildcard %, ma al singolo utente/indirizzo, del tipo 'replication'@'116.213.177.13'.

Sincronizzare il db

-- Su server1
USE exampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
-- Prendere nota dello status.
-- Fare un dump e copiarlo sull'altro host.
UNLOCK TABLES;

-- Su server2
-- mysqladmin --user=root --password stop-slave
CREATE DATABASE exampledb;
-- mysql -u root -p exampledb < exampledb.sql
USE exampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
-- Prendere nota dello status.
UNLOCK TABLES;
CHANGE MASTER TO MASTER_HOST='192.168.3.71', MASTER_USER='slave2_user',
    MASTER_PASSWORD='slave2_password', MASTER_LOG_FILE='mysql-bin.000001',
    MASTER_LOG_POS=106;
START SLAVE;
SHOW SLAVE STATUS;

-- Su server1
STOP SLAVE;
CHANGE MASTER TO MASTER_HOST='192.168.3.73', MASTER_USER='slave1_user',
    MASTER_PASSWORD='slave1_password', MASTER_LOG_FILE='mysql-bin.000001',
    MASTER_LOG_POS=2359;
START SLAVE;
SHOW SLAVE STATUS;

Verifica dello stato SLAVE

Come utente Unix root ci si collega al database MASTER/SLAVE:

mysql dbname

Per vedere lo stato di slave:

SHOW STATUS LIKE 'Slave_running';

Per verificare lo stato di slave e in particolare il numero di log in corso di replica (Relay_Master_Log_File):

SHOW SLAVE STATUS\G;
MariaDB [dbname]> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.2.163
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mariadb-bin.004356
          Read_Master_Log_Pos: 845159197
               Relay_Log_File: relay-bin.012490
                Relay_Log_Pos: 819964666
        Relay_Master_Log_File: mariadb-bin.004300
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 819964380
              Relay_Log_Space: 61048049539
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 6789
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 2
1 row in set (0.00 sec)

Crash test

Test 1 (funziona)

  1. Spengere server2
  2. Su server1 inserire una una fattura (id = 15)
  3. Riavviare server1
  4. Verificare se la fattura id = 15 viene replicata.
    Sì, dopo qualche istante.

Test 2 (fallisce)

  1. Inserire un cliente:
    INSERT INTO clienti (name) VALUES ('Altro cliente');
  2. Interrompere la connettività sui due:
    iptables -I INPUT --proto tcp --dport 3306 -j REJECT
    /etc/init.d/mysql restart
  3. Su server1 rimuovere il cliente:
    DELETE FROM clienti WHERE id = 5;
  4. Su server2 aggiungere una fattura per il cliente:
    INSERT INTO fatture (cliente_id) VALUES (5);
  5. Ripristinare la connettività e attendere.
    No, non riesce a replicare! Lo si può vedere con un SHOW SLAVE STATUS, dove il valore Slave_SQL_Running = No.
doc/appunti/linux/sa/mysql_replica_master_master.1741788000.txt.gz · Last modified: 2025/03/12 15:00 by niccolo