GNU/Linux >> LINUX-Kenntnisse >  >> Linux

So richten Sie die MySQL-Master-Master-Replikation ein

Dieser Artikel fasst Informationen aus mehreren Quellen in dem Format zusammen, das ich zum Einrichten von MySQL Master/Master Replication verwende. Das Schöne an Linux und Open Source ist, dass es viele verschiedene Möglichkeiten gibt, dies zu tun. Bitte werfen Sie einen Blick auf meine Referenzen und nutzen Sie diese für Ihre Bedürfnisse. Wenn Sie Fragen haben oder auf Probleme stoßen, können Sie mir gerne eine Nachricht in den Kommentaren hinterlassen.

Annahmen

Dieser Artikel geht davon aus, dass Sie MySQL bereits auf jedem Ihrer Server installiert haben. Wenn nicht, können Sie dies ganz einfach über die MySQL-Website unter https://www.mysql.org/downloads tun. Dieser Artikel wurde nicht auf MariaDB getestet, sollte aber funktionieren, wenn Sie MariaDB bevorzugen.

Ändere SELINUX auf permissive (falls installiert)

Server 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
	

Server 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
	

Stoppen und deaktivieren Sie firewalld auf jedem Server

Server A

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

Führen Sie den folgenden Befehl aus, um sicherzustellen, dass keine Firewall-Regeln vorhanden sind

[[email protected] ~]# iptables -L

Das Ergebnis sollte wie folgt aussehen:

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

Server B

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

Führen Sie den folgenden Befehl aus, um sicherzustellen, dass keine Firewall-Regeln vorhanden sind.

[[email protected] ~]# iptables -L

Das Ergebnis sollte wie folgt aussehen:

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

Bearbeiten Sie /etc/my.cnf auf beiden Servern

Fügen Sie die folgenden Informationen am Ende des Abschnitts [mysqld] hinzu

Server 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
    

Server 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
    

Stellen Sie sicher, dass Sie name_of_database durch den Namen der Datenbank ersetzen, die Sie replizieren möchten

Starten und aktivieren Sie den MySQL-Daemon auf jedem Server neu

Server A

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

Server B

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

Erstellen Sie den Replikatorbenutzer auf jedem Server

[[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'@'%'

Protokolldateiinformationen zur Verwendung auf dem anderen Server abrufen

Server 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)

Beachten Sie die "Datei" und "Position" von diesem Befehl

Server 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;

Wiederholen Sie die gleichen Schritte auf Server B

Server 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)

Beachten Sie die "Datei" und "Position" von diesem Befehl

Server 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;

Starten Sie beide Server neu

Server A

[[email protected] ~]# systemctl reboot

Server B

[[email protected] ~]# systemctl reboot

Erstellen Sie auf beiden Servern Ihre Datenbank

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

mysql> CREATE DATABASE foo;

Überprüfen Sie auf dem anderen Server, ob die Datenbank vorhanden ist

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

mysql> SHOW DATABASES;

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

Quellen

  • 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. So konfigurieren Sie die MySQL Multi-Master-Replikation unter Oracle Linux

  2. Einrichten der Master-Master-Replikation mit MySQL unter Debian 8 (Jessie)

  3. So richten Sie LogAnalyzer mit Rsyslog und MySQL ein

  4. So richten Sie die MariaDB-Master-Slave-Replikation unter Ubuntu 18.04 ein

  5. Konfigurieren Sie die MySQL-Quell-Quell-Replikation

So richten Sie die CouchDB-Replikation unter Ubuntu 16.04 ein

So richten Sie MySQL mit Docker unter Linux ein

So richten Sie das neueste MySQL unter Ubuntu 20.04 LTS ein

So richten Sie die MySQL-Replikation in RHEL/Centos ein

So richten Sie eine sichere Remote-MySQL-Verbindung ein

Wie richte ich die MySQL-Master-Slave-Replikation auf RHEL 7 ein?