Base de datos · 3 min read · Sep 27, 2025

Replicación Maestro-Maestro de MySQL

Tutorial de Replicación Maestro-Maestro de MySQL

Este tutorial describe cómo configurar la replicación maestro-maestro de MySQL. Necesitamos replicar servidores MySQL para lograr alta disponibilidad (HA). En mi caso, necesito dos maestros que estén sincronizados entre sí, de modo que si uno de ellos falla, el otro pueda tomar el control y no se pierda ningún dato. De manera similar, cuando el primero vuelva a estar en línea, seguirá siendo utilizado como esclavo del que está activo.

Aquí hay un tutorial básico paso a paso, que cubrirá la replicación maestro y esclavo de MySQL y también describirá la replicación maestro y maestro de MySQL.

Notiones: llamaremos al sistema 1 como maestro1 y esclavo2 y al sistema2 como maestro2 y esclavo1.

Paso 1:

Instalar mysql en maestro 1 y esclavo 1. Configurar los servicios de red en ambos sistemas, como

Maestro 1/Esclavo 2 ip: 192.168.16.4

Maestro 2/Esclavo 1 ip: 192.168.16.5

Paso 2:

En Maestro 1, realizar cambios en my.cnf:

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

log-bin
binlog-do-db= # ingresar la base de datos que debe ser replicada
binlog-ignore-db=mysql # ingresar la base de datos que debe ser ignorada para la replicación
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

Paso 3:

En maestro 1, crear una cuenta de esclavo de replicación en mysql.

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

y reiniciar el mysql maestro1.

Paso 4:

Ahora edita my.cnf en Esclavo1 o Maestro2:

[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

Paso 5:

Reiniciar mysql esclavo 1 y en

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

* 1. fila *

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 fila en el conjunto (0.00 seg)

Las filas resaltadas anteriores deben indicar archivos de registro relacionados y Slave_IO_Running y Slave_SQL_Running: deben estar en YES.

Paso 6:

En maestro 1:

mysql> show master status;
+————————+———-+————–+——————+
| Archivo | Posición | Binlog_Do_DB | Binlog_Ignore_DB |
+————————+———-+————–+——————+
|MysqlMYSQL01-bin.000008 | 410 | adam | |
+————————+———-+————–+——————+
1 fila en el conjunto (0.00 seg)

El escenario anterior es para maestro-esclavo, ahora crearemos un escenario de esclavo maestro para los mismos sistemas y funcionará como maestro maestro.

Paso 7:

En Maestro2/Esclavo 1, edita my.cnf y agrega las entradas de maestro:

[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 # información para convertirse en maestro añadida
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

Paso 8:

** Crear una cuenta de esclavo de replicación en maestro2 para maestro1:

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

Paso 9:

** Editar my.cnf en maestro1 para la información de su maestro.

[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

master-host = 192.168.16.5
master-user = replication
master-password = slave2
master-port = 3306

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

Paso 10:

Reiniciar tanto mysql maestro1 como maestro2.

En mysql maestro1:

mysql> start slave;

En mysql maestro2:

mysql > show master status;

En mysql maestro 1:

mysql> show slave status\G;

* 1. fila *
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 fila en el conjunto (0.00 seg)

ERROR:
No se especificó ninguna consulta

Verifique las filas resaltadas, asegúrese de que esté funcionando. Ahora puede crear tablas en la base de datos y verá cambios en el esclavo. ¡Disfruta!

Share: X/Twitter LinkedIn

Recibe nuevas publicaciones en tu bandeja de entrada.

No spam. Cancela la suscripción en cualquier momento.