在企业级数据库架构中,高可用性是确保业务连续性的关键。MySQL双主复制(Master-Master Replication)通过允许两个数据库服务器互为主从,实现数据的双向同步,从而提升系统的容错能力和读写分离的灵活性。本文将详细介绍在Linux环境下搭建MySQL 8.4双主复制的标准化步骤 。
在开始之前,请确保你已准备好以下环境:
|
主机名 |
IP地址 |
操作系统 |
MySQL版本 |
|
Master1 |
192.168.186.11 |
RHEL 6.4 |
MySQL 8.4.28 |
|
Master2 |
192.168.186.12 |
RHEL 6.4 |
MySQL 8.4.28 |
注意 :如果两台服务器均为全新安装且无数据,可跳过数据备份与恢复步骤。
双主复制的核心在于正确配置MySQL参数,以下是必须设置的参数项:
编配置文件 /etc/my.cnf ,主要参数如下:
[mysqld] user=mysql port=3306 basedir=/mysql/app datadir=/mysql/product/data log_error=/mysql/product/data/myerror.log #会话参数 max_connections=4532 max_user_connections=4000 max_connect_errors=50000 wait_timeout=7200 interactive_timeout=7200 thread_cache_size=256 #binlog日志参数 log-bin=mysql-bin binlog_cache_size=131072 binlog_error_action=ABORT_SERVER binlog_format=ROW server_id=1 gtid_mode=on enforce_gtid_consistency=on expire_logs_days=30 sync_binlog=1 auto-increment-increment = 2 #每次增长2 auto-increment-offset = 1 #设置自动增长的字段的偏移量 #慢日志参数 slow_query_log=0 long_query_time=1 log_output=FILE #Redo日志参数 innodb_log_file_size=1048576000 innodb_log_files_in_group=3 #缓存参数 sort_buffer_size=720896 read_buffer_size=720896 read_rnd_buffer_size=360448 join_buffer_size=360448 max_allowed_packet=1073741824 max_heap_table_size=16777216 tmp_table_size=2097152 #innodb参数 innodb_buffer_pool_size=134217728 #改为内存的50%-80% innodb_data_file_path=ibdata1:200M:autoextend innodb_file_per_table=ON innodb_log_buffer_size=16777216 innodb_flush_method=O_DIRECT #事务隔离级别 transaction-isolation=READ-COMMITTED #字符集 character_set_server=utf8mb4 #查询缓存 query_cache_size=0 query_cache_type=OFF #设置时区 default_time_zone='+8:00' log_timestamps=SYSTEM #大小写不敏感 lower_case_table_names=1 #以下参数根据服务器性能调整 sync_relay_log=1 #默认值是10000 sync_relay_log_info=1 #默认值是10000 sync_source_info = 1 #默认值是10000 relay_log_recovery=on #默认值off
编辑配置文件 /etc/my.cnf ,主要参数如下:
[mysqld] user=mysql port=3306 basedir=/mysql/app datadir=/mysql/product/data log_error=/mysql/product/data/myerror.log #会话参数 max_connections=4532 max_user_connections=4000 max_connect_errors=50000 wait_timeout=7200 interactive_timeout=7200 thread_cache_size=256 #binlog日志参数 log-bin=mysql-bin binlog_cache_size=131072 binlog_error_action=ABORT_SERVER binlog_format=ROW server_id=2 gtid_mode=on enforce_gtid_consistency=on expire_logs_days=30 sync_binlog=1 auto-increment-increment = 2 #每次增长2 auto-increment-offset = 2 #设置自动增长的字段的偏移量 #慢日志参数 slow_query_log=0 long_query_time=1 log_output=FILE #Redo日志参数 innodb_log_file_size=1048576000 innodb_log_files_in_group=3 #缓存参数 sort_buffer_size=720896 read_buffer_size=720896 read_rnd_buffer_size=360448 join_buffer_size=360448 max_allowed_packet=1073741824 max_heap_table_size=16777216 tmp_table_size=2097152 #innodb参数 innodb_buffer_pool_size=134217728 #改为内存的50%-80% innodb_data_file_path=ibdata1:200M:autoextend innodb_file_per_table=ON innodb_log_buffer_size=16777216 innodb_flush_method=O_DIRECT #事务隔离级别 transaction-isolation=READ-COMMITTED #字符集 character_set_server=utf8mb4 #查询缓存 query_cache_size=0 query_cache_type=OFF #设置时区 default_time_zone='+8:00' log_timestamps=SYSTEM #大小写不敏感 lower_case_table_names=1 #以下参数根据服务器性能调整 sync_relay_log=1 #默认值是10000 sync_relay_log_info=1 #默认值是10000 sync_source_info = 1 #默认值是10000 relay_log_recovery=on #默认值off
关键点 :
确保 server_id 唯一;
开启GTID模式以简化复制管理;
自增字段设置避免主键冲突。
若Master1已有数据,需先备份并同步至Master2。
#
在
Master1
执行备份
xtrabackup --defaults-file=/etc/my.cnf -uroot -pmysql -S /tmp/mysql.sock --slave-info --backup --target-dir=/xtar
# 在 Master2 恢复数据
全量备份应用redo日志(前滚已提交的事务,回滚未提交的事务) xtrabackup --prepare --target-dir=/xtar 将备份数据从备份位置copy至datadir指定位置 xtrabackup --copy-back --target-dir=/xtar 启动目标端MySQL服务 chown -R mysql:mysql /mysql/product/data #路径datadir指定 service mysqld start
#
在
Master1
执行备份
mysqldump -uroot -pAbcd321# -S /tmp/mysql.sock --single-transaction -A -R -E --triggers --master-data=2 --flush-logs > full.sql
#
在
Master2
恢复数据
mysql -uroot -p < full.sql
在双方服务器上创建用于复制的专用账户。
在Master1上执行:
CREATE USER 'backup'@'192.168.186.12' IDENTIFIED BY 'mcbackup'; GRANT REPLICATION SLAVE ON *.* TO 'backup'@'192.168.186.12'; FLUSH PRIVILEGES;
在Master2上执行:
CREATE USER 'backup'@'192.168.186.11' IDENTIFIED BY 'mcbackup'; GRANT REPLICATION SLAVE ON *.* TO 'backup'@'192.168.186.11'; FLUSH PRIVILEGES;
在Master2上执行:
change replication source to source_host = '192.168.186.11',source_port = 3306, source_user = 'backup', source_password = 'mcbackup', source_auto_position = 1,get_source_public_key = 1;
start replica; show replica status\g; …… Slave_IO_Running: Yes Slave_SQL_Running: Yes ……
确认 Slave_IO_Running 和 Slave_SQL_Running 均为 yes ,则复制正常。
在Master1上执行:
change replication source to source_host = '192.168.186.12',source_port = 3306, source_user = 'backup', source_password = 'mcbackup', source_auto_position = 1,get_source_public_key = 1;
start replica; show replica status\g; …… Slave_IO_Running: Yes Slave_SQL_Running: Yes ……
同样检查 Slave_IO_Running 和 Slave_SQL_Running 状态。
在Master1上创建表或插入数据;
在Master2上检查是否同步;
在Master2上更新数据;
在Master1上确认变更已同步。
若双方数据实时同步,则双主复制搭建成功!
双主复制适用于读多写少的场景,需注意数据冲突的避免;
定期监控复制状态,确保 Seconds_Behind_Master 接近0;
建议配合读写分离中间件使用,提升性能。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29956245/viewspace-3117197/ ,如需转载,请注明出处,否则将追究法律责任。