问题描述:搭建过一主多从的环境,由于数据库数据一致性要求高,有些情景会搭建一主多从的架构,搭建多主一从的模式,相对来说适合数据整合,将多个业务的库整合到一起,方便做查询,也可以当做一个监控其他主库数据的作用。但是搭建这样的模式极易造成数据冲突,同步的库名一致,导致主从异常,同时也对网络并发提出一定得要求

环境介绍:

正常可以将数据库分别放在不通的服务器上,本文方便测试,以下三个数据库都放在了同一台服务器上

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搭建多主一从源复制环境的更多相关文章

  1. mysql搭建多主一从源复制环境

    问题描述:搭建过一主多从的环境,由于数据库数据一致性要求高,有些情景会搭建一主多从的架构,搭建多主一从的模式,相对来说适合数据整合,将多个业务的库整合到一起,方便做查询,也可以当做一个监控其他主库数据 ...

  2. MySQL 5.7的多源复制

    MySQL 5.7已经开始支持了多源复制,相信小伙们都很激动,MySQL 5.7之前只能实现一主一从.一主多从或者多主多从的复制,如果想实现多主一从的复制,只好使用MariaDB,但是MariaDB又 ...

  3. MariaDB多源复制环境搭建(多主一丛)

    环境: 192.168.1.248 HE1 主库 192.168.1.249 HE2 主库 192.168.1.250 HE3 从库 主库授权备份账户 mysql>  grant SELECT, ...

  4. 基于Docker搭建MySQL多源复制环境

    MySQL5.7在主从复制上面相对之前版本多了一些新特性,包括多源复制.基于组提交的并行复制.在线修改Replication Filter.GTID增强.半同步复制增强等. 多源复制:多源复制加入了一 ...

  5. mysql 5.7.16多源复制

    演示一下在MySQL下搭建多主一从的过程. 实验环境: 192.168.24.129:3306 192.168.24.129:3307 192.168.24.129:3308 主库操作 导出数据 分别 ...

  6. MySQL 5.7.9多源复制报错修复

    版本:5.7.9 用5.7.9的版本搭建MySQL多源复制测试环境 开发说复制出现问题,上去看了一下: mysql> show slave status\G******************* ...

  7. MySQL灾备恢复在线主从复制变成主主复制及多源复制【转】

    生产主主复制(A<--->B),和灾备主从复制(B--->C).当生产出现问题时,数据写入切换到灾备数据库,待生产恢复后,将灾备回写到生产.步骤如下: 1.灾备与生产其中一台建立主主 ...

  8. MySQL多源复制【转】

    什么是多源复制? 首先,我们需要清楚 multi-master 与multi-source 复制不是一样的. Multi-Master 复制通常是环形复制, 你可以在任意主机上将数据复制给其他主机. ...

  9. MySQL 5.7 多源复制实践

    多源复制使用场景 数据分析部门会需要各个业务部门的部分数据做数据分析,这个时候就可以用到多源复制把各个主数据库的数据复制到统一的数据库中. 在从服务器进行数据汇总,如果我们的主服务器进行了分库分表的操 ...

  10. mysql5.7 安装和多源复制实践

    MySQL 5.7发布后,在复制方面有了很大的改进和提升.比如开始支持多源复制(multi-source)以及真正的支持多线程复制了.多源复制可以使用基于二进制日子的复制或者基于事务的复制.下面我们说 ...

随机推荐

  1. 在gitlab仓库中cli 编译通不过 怎样解决

    1.先切换到master  分支 git  pull   (把代码拉下来) 2.再切换到当前分支 git  rebase   master 查看git  log 看是否跟新到最新的log 3.再在当前 ...

  2. ssm框架基本原理

    一.前言 SM框架是标准的MVC模式,将整个系统划分为四层:View层,Controller层,Service层,Dao层 SSM(Spring+SpringMVC+MyBatis)框架集由Sprin ...

  3. mapreduce启动命令

    mapreduce启动命令 hadoop jar /var/lib/hadoop-hdfs/codejar/flash_format_testip.jar com.js.dataclean.hm2_h ...

  4. IT工具知识-14:如何通过adb操作安卓剪切板?

    1.安装apk 下载地址 2.运行服务(每次重启系统都需要运行一次) adb shell am startservice ca.zgrs.clipper/.ClipboardService 3.设置剪 ...

  5. vue项目部署后页面加载首次很慢的优化方案

    参考: vue项目首次加载特别慢需要怎么配置? 1.看看你的依赖包是不是全局引入的,改为组件内按需引入,可大大降低加载时长.或者将组件引入方式改为cdn引入.需要注意的是,两种引入方式不能共存. 2. ...

  6. redis启动报错(TCP backlog setting of 511/overcommit_memory is set to 0/THP)

    WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is s ...

  7. idea项目编译时报错:程序包XXX不存在

    问题如下: 解决方法: 点击File-->点击settings-->点击maven-->点击Runner-->勾选Delegate IDE build/run action t ...

  8. C语言的导数和积分

    用C进行导数和积分的运算 进行求导 设一个dx,利用f(x)-f(x-dx)/dx或f(x)-f(x+dx)/dx进行计算. float qd(float x) { float dx=0.01,y; ...

  9. Go_day06

    Go基础语法 错误和异常 什么是错误error 错误是指可鞥出现问题的地方出现了问题,这种情况在预料之中 func main() { //打开一个文件 file, err := os.Open(&qu ...

  10. StoneDB 子查询优化

    StoneDB 子查询优化 摘要: 说明如何优化 exists 的 join 查询优化器的处理 核心函数: TwoDimensionalJoiner::ChooseJoinAlgorithm Join ...