MySQL的多主多从架构涉及到了多个主库(Master)和从库(Slave)之间的数据复制和同步。以下是MySQL多主多从的基本流程:
-
二进制日志(Binary Log):
- 在MySQL中,二进制日志(BINLOG)是一种记录了数据库变更操作的日志文件,包括插入、更新和删除等操作。主服务器(Master)将每个变更操作都写入二进制日志,并且按照顺序记录。
-
从服务器连接主服务器:
- 每个从服务器都会连接到一个或多个主服务器,建立复制连接。从服务器通过连接主服务器的复制通道,获取主服务器上的二进制日志。
-
从服务器请求复制事件:
- 从服务器会向主服务器发送一个复制事件请求,请求获取主服务器上的二进制日志。主服务器会将对应的二进制日志发送给从服务器。
-
从服务器应用复制事件:
- 从服务器接收到主服务器发送的二进制日志后,会将这些日志应用到自己的数据库中。从服务器会按照日志的顺序逐个应用,确保数据的一致性。
-
从服务器记录中继日志(Relay Log):
- 从服务器在应用主服务器的二进制日志时,会记录自己的中继日志。当中继日志积累到一定程度后,从服务器会将这些日志刷新到自己的数据文件中,完成数据的复制和同步。
在多主架构中,每个主库都可以接收写操作,并且需要与其他主库进行数据同步,以确保数据的一致性。为了实现这一点,通常会采用双向复制或其他复杂的同步机制。
请注意,虽然多主多从架构可以提供更高的负载均衡和高可用性,但也会增加系统的复杂性和维护成本。因此,在设计和实施这种架构时,需要仔细考虑业务需求、系统性能和可维护性等因素。
参考:数据库主从备份-CSDN博客
环境
linux:redhat 9.2
mariadb版本:10.5.16
master1:192.168.200.133
master2: 192.168.200.130
salve1:192.168.200.128
salve2:192.168.200.129
步骤
1、更名
[root@admin ~]# hostnamectl hostname master1
[root@admin ~]# bash
[root@master1 ~]#
[root@host02 ~]# hostnamectl hostname master2
[root@host02 ~]# bash
[root@master2 ~]#
[root@localhost ~]# hostnamectl hostname savle1
[root@localhost ~]# bash
[root@savle1 ~]#
[root@client ~]# hostnamectl hostname savle2
[root@client ~]# bash
[root@savle2 ~]#
2、关闭防火墙
[root@master1 ~]# systemctl stop firewalld.service
[root@master1 ~]# setenforce 0
[root@master1 ~]#
[root@master2 ~]# systemctl stop firewalld.service
[root@master2 ~]# setenforce 0
[root@master2 ~]#
[root@savle1 ~]# systemctl stop firewalld.service
[root@savle1 ~]# setenforce 0
[root@savle1 ~]#
[root@savle2 ~]# systemctl stop firewalld
[root@savle2 ~]# setenforce 0
[root@savle2 ~]#
3、安装mariadb
设置开机自启
[root@master1 ~]# yum -y install mariadb*
[root@master1 ~]# systemctl restart mariadb.service
[root@master1 ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@master1 ~]#
[root@master2 ~]# yum -y install mariadb*
[root@master2 ~]# systemctl restart mariadb.service
[root@master2 ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@master2~]#
[root@savle1 ~]# yum -y install mariadb*
[root@savle1 ~]# systemctl restart mariadb.service
[root@savle1 ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@savle1 ~]#
[root@savle2 ~]# yum -y install mariadb*
[root@savle2 ~]# systemctl restart mariadb.service
[root@savle2 ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@savle2 ~]#
4、初始化配置
[root@master1 ~]# mysql_secure_installation
[root@master2 ~]# mysql_secure_installation
[root@savle1 ~]# mysql_secure_installation
[root@savle2 ~]# mysql_secure_installation
5、对两台主进行主主配置
[root@master1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
//增加以下
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 1
log-bin = master-log
relay-log = slave-log
auto_increment_offset = 1
auto_increment_increment = 2
[root@master1 ~]# systemctl restart mariadb.service
[root@master2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
//增加
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 2
log-bin = master-log
relay-log = slave-log
auto_increment_offset = 2
auto_increment_increment = 2
[root@master2 ~]# systemctl restart mariadb.service
[root@master2 ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> show master status \G;
*************************** 1. row ***************************
File: master-log.000001
Position: 329
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.000 sec)
ERROR: No query specified
MariaDB [(none)]>
# 跳过DNS解析,提高连接速度。当客户端连接时,不使用DNS解析主机名。
skip_name_resolve = ON
# InnoDB存储引擎为每个InnoDB表创建一个.ibd文件,而不是将所有的数据保存在共享的表空间内。
# 这有助于管理和备份,因为可以单独备份和恢复每个表。
innodb_file_per_table = ON
# MySQL服务器的唯一标识符,通常用于复制设置。每个服务器在复制环境中都需要一个唯一的ID。
server-id = 1
# 启用二进制日志。这通常用于复制和恢复操作。
log-bin = master-log
# 用于复制的从服务器(slave)的中继日志。
# 这允许从服务器从主服务器接收二进制日志事件,并写入自己的中继日志以供稍后处理。
relay-log = slave-log
# 设置自增字段的起始偏移量,通常与auto_increment_increment配合使用
auto_increment_offset = 1
# 设置自增字段的递增量,用于在多服务器或分区环境中避免ID冲突
auto_increment_increment = 2
6、创建用户并授权
#master1
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'192.168.200.%' identified by '1';
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> change master to master_host='192.168.200.130',master_user='repluser',master_password='1',master_log_file='master-log.000001',master_log_pos=329;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.130
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000003
Read_Master_Log_Pos: 566
Relay_Log_File: slave-log.000005
Relay_Log_Pos: 556
Relay_Master_Log_File: master-log.000003
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: 566
Relay_Log_Space: 1469
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: 0
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
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
MariaDB [(none)]>
#master2
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'192.168.200.%' identified by '1';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> change master to master_host='192.168.200.133',master_user='repluser',master_password='1',master_log_file='master-log.000001',master_log_pos=329;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.133
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000003
Read_Master_Log_Pos: 566
Relay_Log_File: slave-log.000004
Relay_Log_Pos: 866
Relay_Master_Log_File: master-log.000003
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: 566
Relay_Log_Space: 1779
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: 0
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 3
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
ERROR: No query specified
MariaDB [(none)]>
如遇到错误检查 日志文件 /var/log/mariadb/mariadb.log
7、主主测试
#master1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.008 sec)
MariaDB [(none)]> create database mydb;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]>
#master2上查看
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.009 sec)
#删除mydb库随后去到master1上查看
MariaDB [(none)]> drop database mydb;
Query OK, 0 rows affected (0.002 sec)
#master1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
8、配置从服务器
先为两台主增加配置
[root@master1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
......
log-slave-updates //增加这一行即可
[root@master1 ~]# systemctl restart mariadb.service
[root@master2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
........
log-slave-updates //记录二进制日志
[root@master2 ~]# systemctl restart mariadb.service
#slave1
[root@savle1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
log-bin=master-log
server-id=3 //优先级
[root@savle1 ~]# systemctl restart mariadb.service
#slave2
[root@savle2 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid
log-bin=master-log
server-id=4
[root@savle2 ~]# systemctl restart mariadb.service
9、配置同步信息
#slave1
MariaDB [(none)]> change master to master_host='192.168.200.130',master_user='repluser',master_password='1',master_log_file='master-log.000001',master_log_pos=329;
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.130
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000004
Read_Master_Log_Pos: 343
Relay_Log_File: mariadb-relay-bin.000005
Relay_Log_Pos: 643
Relay_Master_Log_File: master-log.000004
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: 343
Relay_Log_Space: 1696
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: 0
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
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 3
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
MariaDB [(none)]>
#slave2
MariaDB [(none)]> change master to master_host='192.168.200.133',master_user='repluser',master_password='1',master_log_file='master-log.000001',master_log_pos=329;
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.133
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000004
Read_Master_Log_Pos: 343
Relay_Log_File: mariadb-relay-bin.000006
Relay_Log_Pos: 643
Relay_Master_Log_File: master-log.000004
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: 343
Relay_Log_Space: 1254
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: 0
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 5
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)
MariaDB [(none)]>
10、多主多从测试
#master2创建操作
MariaDB [(none)]> create database ASD;
Query OK, 1 row affected (0.000 sec)
#master1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| ASD |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]>
#slave1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| ASD |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
MariaDB [(none)]>
#slave2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| ASD |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]>
#master1删除操作
MariaDB [(none)]> drop database ASD;
Query OK, 0 rows affected (0.001 sec)
#master2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
#slave1
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
#slave2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
MariaDB [(none)]>