Database · 3 min read · Sep 27, 2025

Replicazione Master Master MySQL

Tutorial sulla Replicazione Master Master MySQL

Questo tutorial descrive come impostare la replicazione master-master di MySQL. Dobbiamo replicare i server MySQL per ottenere alta disponibilità (HA). Nel mio caso ho bisogno di due master che siano sincronizzati tra loro in modo che, se uno di essi si interrompe, l’altro possa subentrare e non si perda alcun dato. Allo stesso modo, quando il primo torna attivo, sarà ancora utilizzato come slave per quello attivo.

Ecco un tutorial di base passo dopo passo, che coprirà la replicazione master e slave di MySQL e descriverà anche la replicazione master e master di MySQL.

Notioni: chiameremo il sistema 1 master1 e slave2 e il sistema 2 master2 e slave 1.

Passo 1:

Installa mysql su master 1 e slave 1. Configura i servizi di rete su entrambi i sistemi, come

Master 1/Slave 2 ip: 192.168.16.4

Master 2/Slave 1 ip : 192.168.16.5

Passo 2:

Su Master 1, apporta modifiche in my.cnf:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
old_passwords=1

log-bin
binlog-do-db= # inserisci il database che deve essere replicato
binlog-ignore-db=mysql # inserisci il database che deve essere ignorato per la replicazione
binlog-ignore-db=test

server-id=1

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Passo 3:

Su master 1, crea un account slave per la replicazione in mysql.

mysql> grant replication slave on . to ‘replication’@192.168.16.5 \
identified by ‘slave’;

e riavvia il mysql master1.

Passo 4:

Ora modifica my.cnf su Slave1 o Master2 :

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
old_passwords=1

server-id=2

master-host = 192.168.16.4
master-user = replication
master-password = slave
master-port = 3306

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Passo 5:

Riavvia mysql slave 1 e a

mysql> start slave;
mysql> show slave status\G;

* 1. riga *

Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.16.4
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MASTERMYSQL01-bin.000009
Read_Master_Log_Pos: 4

Relay_Log_File: MASTERMYSQL02-relay-bin.000015

Relay_Log_Pos: 3630
Relay_Master_Log_File: MASTERMYSQL01-bin.000009
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: 4
Relay_Log_Space: 3630
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: 1519187

1 riga nel set (0.00 sec)

Le righe evidenziate sopra devono indicare i file di log correlati e Slave_IO_Running e Slave_SQL_Running: devono essere impostati su YES.

Passo 6:

Su master 1:

mysql> show master status;
+————————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+————————+———-+————–+——————+
|MysqlMYSQL01-bin.000008 | 410 | adam | |
+————————+———-+————–+——————+
1 riga nel set (0.00 sec)

Lo scenario sopra è per master-slave, ora creeremo uno scenario slave master per gli stessi sistemi e funzionerà come master master.

Passo 7:

Su Master2/Slave 1, modifica my.cnf e inserisci le voci master:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

old_passwords=1
server-id=2

master-host = 192.168.16.4
master-user = replication
master-password = slave
master-port = 3306

log-bin #informazioni per diventare master aggiunte
binlog-do-db=adam

[mysql.server]
user=mysql
basedir=/var/lib

[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Passo 8:

** Crea un account slave per la replicazione su master2 per master1:

mysql> grant replication slave on *.* to 'replication'@192.168.16.4 identified by 'slave2';

Passo 9:

** Modifica my.cnf su master1 per le informazioni del suo master.

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

old_passwords=1

log-bin
binlog-do-db=adam
binlog-ignore-db=mysql
binlog-ignore-db=test

server-id=1
#informazioni per diventare slave.
master-host = 192.168.16.5
master-user = replication
master-password = slave2
master-port = 3306

[mysql.server]user=mysqlbasedir=/var/lib

Passo 10:

Riavvia sia mysql master1 che master2.

Su mysql master1:

mysql> start slave;

Su mysql master2:

mysql > show master status;

Su mysql master 1:

mysql> show slave status\G;

* 1. riga *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.16.5
Master_User: replica
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Mysql1MYSQL02-bin.000008
Read_Master_Log_Pos: 410
Relay_Log_File: Mysql1MYSQL01-relay-bin.000008
Relay_Log_Pos: 445
Relay_Master_Log_File: Mysql1MYSQL02-bin.000008
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: 410
Relay_Log_Space: 445
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: 103799
1 riga nel set (0.00 sec)

ERRORE:
Nessuna query specificata

Controlla le righe evidenziate, assicurati che stia funzionando. Ora puoi creare tabelle nel database e vedrai le modifiche nello slave. Buon divertimento!!

Share: X/Twitter LinkedIn

Ricevi i nuovi post nella tua casella di posta.

Nessuno spam. Disiscriviti in qualsiasi momento.