mysql搭建多主一从源复制环境
问题描述:搭建过一主多从的环境,由于数据库数据一致性要求高,有些情景会搭建一主多从的架构,搭建多主一从的模式,相对来说适合数据整合,将多个业务的库整合到一起,方便做查询,也可以当做一个监控其他主库数据的作用。但是搭建这样的模式极易造成数据冲突,同步的库名一致,导致主从异常,同时也对网络并发提出一定得要求
环境介绍:
正常可以将数据库分别放在不通的服务器上,本文方便测试,以下三个数据库都放在了同一台服务器上
IP:192.168.163.21 3306--主库 server_id:29 3307--主库 server_id:27 3308--收集从库 server_id:28
1.配置3306主库参数
[mysqld]
basedir=/application/mysql
datadir=/data/3306/data
socket=/data/3306/mysql.sock
port=3306
log-error=/data/3306/mysql.log
log_bin=/data/3306/mysql-bin
binlog_format=row
#skip-name-resolve
server-id=29
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1 # 需要同步库
binlog-do-db = dev3306
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
配置复制用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
重启3306端口
systemctl restart mysqld3306
2.配置3307主库参数
[mysqld]
basedir=/application/mysql
datadir=/data/3307/data
socket=/data/3307/mysql.sock
port=3307
log-error=/data/3307/mysql.log
log_bin=/data/3307/mysql-bin
binlog_format=row
skip-name-resolve
server-id=27
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1 # 需要同步库
binlog-do-db = dev3307
# 不需要同步库
binlog-ignore-db = mysql
binlog_ignore_db = information_schema
binlog_ignore_db = performation_schema
binlog_ignore_db = sys
配置复制用户
mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'root1234';
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
重启3307端口
systemctl restart mysqld3307
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
| mysql-bin.000015 | 362 | dev3306 | mysql,information_schema,performation_schema,sys | 55826e3e-7f15-11eb-a9e3-000c2959ac6a:1-236538 |
+------------------+----------+--------------+--------------------------------------------------+-----------------------------------------------+
1 row in set (0.00 sec)
3.配置3307从库参数
[mysqld]
basedir=/application/mysql
datadir=/data/3308/data
port=3308
socket=/data/3308/mysql.sock
log-error=/data/3308/mysql.log
log_bin=/data/3308/mysql-bin
binlog_format=row
skip-name-resolve
server-id=28
gtid-mode=on
enforce-gtid-consistency=true
log-slave-updates=1 lower_case_table_names = 1 # 表名不区分大小写
master_info_repository = table
relay_log_info_repository = table
重启生效
systemctl restart mysqld3308
mysql> show master status;
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
| mysql-bin.000011 | 362 | dev3307 | mysql,information_schema,performation_schema,sys | 1a0da16a-7f15-11eb-a6b4-000c2959ac6a:1-16 |
+------------------+----------+--------------+--------------------------------------------------+-------------------------------------------+
1 row in set (0.00 sec)
4.开启同步
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.163.21',
-> MASTER_PORT=3306, MASTER_USER='slave',
-> MASTER_PASSWORD='root1234',
-> MASTER_LOG_FILE='mysql-bin.000015', MASTER_LOG_POS=362 for channel '29';
7, MASTER_USER='slave',
MASTER_PASSWORD='root1234',
MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=362 for channel '27';Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql>
mysql>
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.163.21',
-> MASTER_PORT=3307, MASTER_USER='slave',
-> MASTER_PASSWORD='root1234',
-> MASTER_LOG_FILE='mysql-bin.000011', MASTER_LOG_POS=362 for channel '27';
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave ;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.163.21
Master_User: slave
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 630
Relay_Log_File: mha4-relay-bin-27.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000010
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: 630
Relay_Log_Space: 529
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: 27
Master_UUID: 1a0da16a-7f15-11eb-a6b4-000c2959ac6a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 1bf45cd1-7f15-11eb-a810-000c2959ac6a:1-13
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: 27
Master_TLS_Version:
*************************** 2. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.163.21
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000014
Read_Master_Log_Pos: 630
Relay_Log_File: mha4-relay-bin-29.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000014
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: 630
Relay_Log_Space: 529
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: 29
Master_UUID: 55826e3e-7f15-11eb-a9e3-000c2959ac6a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 1bf45cd1-7f15-11eb-a810-000c2959ac6a:1-13
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name: 29
Master_TLS_Version:
2 rows in set (0.01 sec)
5.验证同步
在从库上手动创建dev3306和dev3307
3306
mysql> use dev3306;
Database changed
mysql>
mysql> show tables;
Empty set (0.00 sec) mysql>
mysql> create table t1;
ERROR 1113 (42000): A table must have at least 1 column
mysql>
mysql>
mysql> create table t1 (id int);
Query OK, 0 rows affected (0.01 sec) mysql> insert into t1 values(1),(2),(3);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
3307
mysql> use dev3307;
Database changed
mysql>
mysql> create table t2(id int);
Query OK, 0 rows affected (0.00 sec) mysql> insert into t2 values(4),(5),(6);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
3308
mysql> select * from dev3306.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec) mysql> select * from dev3307.t2;
+------+
| id |
+------+
| 4 |
| 5 |
| 6 |
+------+
3 rows in set (0.00 sec)
6.启动和停止单独slave进程
mysql> stop slave for channel '27';
Query OK, 0 rows affected (0.01 sec) mysql> start slave for channel '27';
Query OK, 0 rows affected (0.00 sec)
7.但是以上主库参数设置binlog-do-db也会带来弊端,用这个参数限制同步哪些库的话,但是又想整体对mysql中所有的库做主从,就会比较麻烦。限制了同步某一个库,其他的库没法做实时同步,就不能保证mysql中其他库的安全性。另一方面如果不加这个参数进行多主一从的模式,进行全库都复制同步,就会造成数据冲突,从而主从异常
binlog-do-db=需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可
binlog-ignore-db=不需要复制的数据库苦命,如果复制多个数据库,重复设置这个选项即可
参考文档:https://blog.csdn.net/wangsdsdfds/article/details/105809232
mysql搭建多主一从源复制环境的更多相关文章
- MySQL 5.7的多源复制
MySQL 5.7已经开始支持了多源复制,相信小伙们都很激动,MySQL 5.7之前只能实现一主一从.一主多从或者多主多从的复制,如果想实现多主一从的复制,只好使用MariaDB,但是MariaDB又 ...
- MariaDB多源复制环境搭建(多主一丛)
环境: 192.168.1.248 HE1 主库 192.168.1.249 HE2 主库 192.168.1.250 HE3 从库 主库授权备份账户 mysql> grant SELECT, ...
- 基于Docker搭建MySQL多源复制环境
MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等. 多源复制:多源复制加入了一 ...
- mysql 5.7.16多源复制
演示一下在MySQL下搭建多主一从的过程. 实验环境: 192.168.24.129:3306 192.168.24.129:3307 192.168.24.129:3308 主库操作 导出数据 分别 ...
- MySQL 5.7.9多源复制报错修复
版本:5.7.9 用5.7.9的版本搭建MySQL多源复制测试环境 开发说复制出现问题,上去看了一下: mysql> show slave status\G******************* ...
- MySQL灾备恢复在线主从复制变成主主复制及多源复制【转】
生产主主复制(A<--->B),和灾备主从复制(B--->C).当生产出现问题时,数据写入切换到灾备数据库,待生产恢复后,将灾备回写到生产.步骤如下: 1.灾备与生产其中一台建立主主 ...
- MySQL多源复制【转】
什么是多源复制? 首先,我们需要清楚 multi-master 与multi-source 复制不是一样的. Multi-Master 复制通常是环形复制, 你可以在任意主机上将数据复制给其他主机. ...
- MySQL一主二从复制环境切换主从库
假设有一个一主二从的环境,当主库M出现故障时,需要将其中一个从库S1切换为主库,同时将S2指向新的主库S1,如果可能,需要将故障的主库M修复并重置为新的从库. 搭建一主二从复制环境可参考:mysql5 ...
- MySQL 5.7 多源复制实践
多源复制使用场景 数据分析部门会需要各个业务部门的部分数据做数据分析,这个时候就可以用到多源复制把各个主数据库的数据复制到统一的数据库中. 在从服务器进行数据汇总,如果我们的主服务器进行了分库分表的操 ...
随机推荐
- 新元科技签订5.8亿元IPFS合同,IPFS国家认可吗?IPFS挖矿是不是合法的?
就在昨天,也就是 3 月 15 日,FIL 又有一波较大的涨幅,现流通市值排名第 15,总市值预计 6818.16 亿.FIL 见涨,一些公司也按耐不住了.也是在3月15日,A 股上市公司新元科技签订 ...
- 攻防世界 reverse 2ex1
2ex1 CISCN-2018-Quals mark 1 import base64 2 3 std_base= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijk ...
- c/s应用程序自动更新组件GeneralUpdate3.2.1发布
一.组件简介 GeneralUpdate是基于.net standard 开发的一款(c/s应用)自动升级程序.该组件将更新的核心部分抽离出来方便应用于多种项目当中目前适用于wpf,控制台应用,win ...
- Hi3559AV100 NNIE开发(7) Ruyistudio 输出mobileface_func.wk与板载运行mobileface_chip.wk输出中间层数据对比
前面随笔讲了关于NNIE的整个开发流程,并给出了Hi3559AV100 NNIE开发(5)mobilefacenet.wk仿真成功量化及与CNN_convert_bin_and_print_featu ...
- Spring Boot 轻量替代框架 Solon 1.3.15 发布
Solon 是一个微型的Java开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,4000多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:RPC.REST ...
- vue封装公用弹出框方法,实现点击出现操作弹出框
vue封装公用弹出框方法,实现点击出现操作弹出框 如上图所示,这次要实现一个点击出现操作弹框的效果:并将这个功能封装成一个函数,便于在项目的多个地方使用. 具体思路是: 封装一个组件,组件保护一个插槽 ...
- KeyError:‘uid' Python常见错误
使用不存在的字典键值 检查字典和要查的内容 如有不正确改正即可
- [源码解析] 并行分布式任务队列 Celery 之 Task是什么
[源码解析] 并行分布式任务队列 Celery 之 Task是什么 目录 [源码解析] 并行分布式任务队列 Celery 之 Task是什么 0x00 摘要 0x01 思考出发点 0x02 示例代码 ...
- 使用python制作大数据词云
1 from wordcloud import WordCloud 2 import PIL.Image as image 3 import numpy as np 4 import jieba 5 ...
- Install Tensorflow object detection API in Anaconda (Windows)
This blog is to explain how to install Tensorflow object detection API in Anaconda in Windows 10 as ...