GNU/Linux >> Tutoriales Linux >  >> Linux

Cómo configurar la replicación maestro-maestro de MySQL

Este artículo consolida información de varias fuentes en el formato que uso para configurar MySQL Master/Master Replication. La belleza de Linux y el código abierto es que hay muchas formas diferentes de hacer esto. Por favor, eche un vistazo a mis referencias y utilícelas para satisfacer cualquier necesidad que pueda tener. Si tiene alguna pregunta o tiene algún problema, no dude en enviarme una línea en los comentarios.

Supuestos

Este artículo asume que ya ha instalado MySQL en cada uno de sus servidores. Si no, puede hacerlo fácilmente a través del sitio web de MySQL en https://www.mysql.org/downloads. Este artículo no ha sido probado en MariaDB pero debería funcionar si prefiere usar MariaDB.

Cambiar SELINUX a permisivo (si está instalado)

Servidor A

[[email protected] ~]# vi /etc/selinux/config
  
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
	

Servidor B

[[email protected] ~]# vi /etc/selinux/config
  
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
	

Detener y deshabilitar firewalld en cada servidor

Servidor A

[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld

Ejecute el siguiente comando para asegurarse de que no haya reglas de firewall

[[email protected] ~]# iptables -L

El resultado debería verse así:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Servidor B

[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld

Ejecute el siguiente comando para asegurarse de que no haya reglas de firewall.

[[email protected] ~]# iptables -L

El resultado debería verse así:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Editar /etc/my.cnf en ambos servidores

Agregue la siguiente información al final de la sección [mysqld]

Servidor A

[[email protected] ~]# vi /etc/my.cnf
	
    server-id=1
    log-bin="mysql-bin"
    binlog-do-db=name_of_database
    replicate-do-db=name_of_database
    relay-log="mysql-relay-log"
    auto-increment-offset = 1
    

Servidor B

[[email protected] ~]# vi /etc/my.cnf
    
    server-id=2
    log-bin="mysql-bin"
    binlog-do-db=name_of_database
    replicate-do-db=name_of_database
    relay-log="mysql-relay-log"
    auto-increment-offset = 2
    

Asegúrese de reemplazar nombre_de_base_de_datos con el nombre de la base de datos que desea replicar

Reiniciar y habilitar el demonio MySQL en cada servidor

Servidor A

[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# systemctl enable mysqld

Servidor B

[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]# systemctl enable mysqld

Crear el usuario replicador en cada servidor

[[email protected] ~]# mysql -u root -p

mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY 'change_me';
mysql> GRANT REPLICATION SLAVE ON foo.* TO 'replicator'@'%'
[[email protected] ~]# mysql -u root -p

mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY 'change_me';
mysql> GRANT REPLICATION SLAVE ON foo.* TO 'replicator'@'%'

Obtener información del archivo de registro para usar en el otro servidor

Servidor A

[[email protected] ~]# mysql -u root -p

mysql> SHOW MASTER STATUS;

+------------------+----------+------------------+------------------+
| File             | Position | Binlog_Do_DB     | Binlog_Ignore_DB |
+------------------+----------+------------------+------------------+
| mysql-bin.000001 | 154      | name_of_database |                  |
+------------------+----------+------------------+------------------+
1 row in set (0.00 sec)

Tenga en cuenta el "Archivo" y la "Posición" de este comando

Servidor B

[[email protected] ~]# mysql -u root -p

mysql> STOP SLAVE;

mysql> CHANGE MASTER TO MASTER_HOST = 'Server A IP Address or HOSTNAME',MASTER_USER = 'replicator', MASTER_PASSWORD = 'change_me', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 154;
mysql> START SLAVE;

Repita los mismos pasos en el Servidor B

Servidor B

[[email protected] ~]# mysql -u root -p mysql> SHOW MASTER STATUS;

+------------------+----------+------------------+------------------+
| File             | Position | Binlog_Do_DB     | Binlog_Ignore_DB |
+------------------+----------+------------------+------------------+
| mysql-bin.000001 | 154      | name_of_database |                  |
+------------------+----------+------------------+------------------+
1 row in set (0.00 sec)

Tenga en cuenta el "Archivo" y la "Posición" de este comando

Servidor A

[[email protected] ~]# mysql -u root -p 

mysql> STOP SLAVE; CHANGE MASTER TO MASTER_HOST = 'Server B IP Address or HOSTNAME', MASTER_USER = 'replicator', MASTER_PASSWORD = 'passw0rd', MASTER_LOG_FILE = 'mysql-bin.000001', MASTER_LOG_POS = 154;
mysql> START SLAVE;

Reiniciar ambos servidores

Servidor A

[[email protected] ~]# systemctl reboot

Servidor B

[[email protected] ~]# systemctl reboot

En cualquiera de los servidores crea tu base de datos

[[email protected] ~]# mysql -u root -p 

mysql> CREATE DATABASE foo;

En el otro servidor verifique que la base de datos esté allí

[[email protected] ~]# mysql -u root -p

mysql> SHOW DATABASES;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| foo                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

Fuentes

  • https://www.howtoforge.com/mysql_database_replication
  • https://www.digitalocean.com/community/tutorials/how-to-set-up-mysql-master-master-replication
  • https://www.howtoforge.com/mysql_master_master_replication
  • http://www.ryadel.com/en/mysql-master-master-replication-setup-in-5-easy-steps/

Linux
  1. Configuración de Master-Master Replication con MySQL en Debian 8 (Jessie)

  2. Cómo configurar Pure-FTPD con MySQL en CentOS y RedHat

  3. Cómo configurar LogAnalyzer con Rsyslog y MySQL

  4. Cómo configurar la replicación de MySQL en CentOS

  5. Configurar la replicación fuente-fuente de MySQL

Cómo configurar la replicación de CouchDB en Ubuntu 16.04

Cómo configurar MySQL con Docker en Linux

Cómo configurar la última versión de MySQL en Ubuntu 20.04 LTS

Cómo configurar la replicación de MySQL en RHEL/Centos

Cómo configurar una conexión MySQL remota segura

¿Cómo configurar la replicación maestro-esclavo de MySQL en RHEL 7?