原 【DB宝65】MySQL主从之多源复制(3主1从)搭建及同步测试
[toc]
一、MySQL主从复制简介
1.1、MySQL主从复制简介
MySQL主从复制(MySQL Replication)是指从一个MySQL主服务器(master)将数据拷贝到另一台或多台MySQL从服务器(slaves)的过程。将主数据库的DDL和DML操作通过二进制日志(binlog)传到从服务器(slave)上,然后在从服务器上对这些日志重新执行,从而使得主从服务器的数据保持同步。
MySQL从3.23版本开始提供复制的功能。
MySQL的Replication是一个多MySQL数据库做主从同步的方案,广泛用在各种对MySQL有更高性能、更高可靠性要求的场合。
1.2、主从复制的好处
主从复制有以下几方面的好处:
- 数据备份(Data Backup)
只是简单的对数据库进行备份,降低数据丢失的风险, - 线下统计
用于报表等对数据时效性要求不高的场合。 - 负载均衡(Load Balance)、读写分离
主要用在MySQL集群,解决单点故障或做故障切换;以降低单台服务器的负载和风险,如实现读写分离,可以使得服务器访问负荷比较均衡。 - 数据分发(Data DistributIOn)、灾备
主要用于多数据中心或异地备份,实现数据分发与同步。 - 高可用和数据容错(High Availability and Failover)
MySQL自带的健康监控和检测,根据配置的时间间隔,可以检测主库是否正常工作,一旦发现主库宕机或无法正常工作,就会选择到最好的一个备库上。
1.3、MySQL主从复制流程
1.4、主从拓扑结构
二、3主1从多源复制搭建
2.1、MySQL环境初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | -- 搭建3主1从 -- 配置网络环境 docker pull mysql:5.7.30 docker network create --subnet=172.72.0.0/24 mysql-network -- 创建参数目录 mkdir -p /lhrmysqltest2/master1/conf.d mkdir -p /lhrmysqltest2/master2/conf.d mkdir -p /lhrmysqltest2/master3/conf.d mkdir -p /lhrmysqltest2/slave/conf.d -- 删除之前的 docker rm -f mysql5730M33265 mysql5730M33266 mysql5730M33267 mysql5730S33268 -- 申请主库1 docker run -d --name mysql5730M33265 \ -h master1 -p 33265:3306 --net=mysql-network --ip 172.72.0.10 \ -v /lhrmysqltest2/master1/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30 -- 申请主库2 docker run -d --name mysql5730M33266 \ -h master2 -p 33266:3306 --net=mysql-network --ip 172.72.0.11 \ -v /lhrmysqltest2/master2/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30 -- 申请主库3 docker run -d --name mysql5730M33267 \ -h master3 -p 33267:3306 --net=mysql-network --ip 172.72.0.12 \ -v /lhrmysqltest2/master3/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30 -- 申请从库 docker run -d --name mysql5730S33268 \ -h slave1 -p 33268:3306 --net=mysql-network --ip 172.72.0.13 \ -v /lhrmysqltest2/slave/conf.d:/etc/mysql/conf.d \ -e MYSQL_ROOT_PASSWORD=lhr \ mysql:5.7.30 -- 配置主库1的参数 cat > /lhrmysqltest2/master1/conf.d/my.cnf << "EOF" [mysqld] port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 573033265 log-bin = binlog_format=row expire_logs_days = 30 max_binlog_size = 100M binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performance_schema binlog-ignore-db = sys replicate_ignore_db=information_schema replicate_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=sys gtid-mode=ON enforce-gtid-consistency=on skip_name_resolve report_host=172.72.0.10 EOF -- 配置主库2的参数 cat > /lhrmysqltest2/master2/conf.d/my.cnf << "EOF" [mysqld] port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 573033266 log-bin = binlog_format=row expire_logs_days = 30 max_binlog_size = 100M binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performance_schema binlog-ignore-db = sys replicate_ignore_db=information_schema replicate_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=sys gtid-mode=ON enforce-gtid-consistency=ON skip_name_resolve report_host=172.72.0.11 EOF -- 配置主库3的参数 cat > /lhrmysqltest2/master3/conf.d/my.cnf << "EOF" [mysqld] port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 573033267 log-bin = binlog_format=row expire_logs_days = 30 max_binlog_size = 100M binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performance_schema binlog-ignore-db = sys replicate_ignore_db=information_schema replicate_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=sys gtid-mode=ON enforce-gtid-consistency=ON skip_name_resolve report_host=172.72.0.12 EOF -- 配置从库的参数 cat > /lhrmysqltest2/slave/conf.d/my.cnf << "EOF" [mysqld] port=3306 character_set_server=utf8mb4 secure_file_priv= server-id = 573033268 log-bin = binlog_format=row expire_logs_days = 30 max_binlog_size = 100M binlog-ignore-db = mysql binlog-ignore-db = information_schema binlog-ignore-db = performance_schema binlog-ignore-db = sys replicate_ignore_db=information_schema replicate_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=sys gtid-mode=ON enforce-gtid-consistency=ON skip_name_resolve report_host=172.72.0.13 master-info-repository = table relay-log-info-repository = table EOF -- 重启主机 docker restart mysql5730M33265 docker restart mysql5730M33266 docker restart mysql5730M33267 docker restart mysql5730S33268 docker ps docker exec -it mysql5730M33265 bash docker exec -it mysql5730M33265 mysql -uroot -plhr mysql -uroot -plhr -h192.168.66.35 -P33265 -e "select @@hostname,@@server_id,@@server_uuid" mysql -uroot -plhr -h192.168.66.35 -P33266 -e "select @@hostname,@@server_id,@@server_uuid" mysql -uroot -plhr -h192.168.66.35 -P33267 -e "select @@hostname,@@server_id,@@server_uuid" mysql -uroot -plhr -h192.168.66.35 -P33268 -e "select @@hostname,@@server_id,@@server_uuid" |