前言

多源复制即多主一从结构,多个主服务器端的数据都会同步到后端一个从服务器上面。至于为什么要做多源复制下面的总结很到位。

1、灾备作用:将各个库汇总在一起,就算是其他库都挂了(整个机房都无法连接了),还有最后一个救命稻草;
2、备份:直接在这个从库中做备份,不影响线上的数据库;
3、减少成本:不需要每个库都做一个实例,也减少了DBA的维护成本;
4、数据统计:后期的一些数据统计,需要将所有的库汇总在一起。
 

MySQL多源复制演示

准备

主1服务器端:10.220.5.137

主2服务器端:10.220.5.138

从服务器端:10.220.5.139

配置两个主服务端

首先要保证三个节点中的server_id不一致,检查各个服务器端server_id

检查主1服务端id

[root@ken ~]# vim /etc/my.cnf
...
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 1003307
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days = 10
...

检查主2服务服务器端id

[root@ken ~]# vim /etc/my.cnf
...
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id =
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog =
expire_logs_days =
...

检查从服务服务器端id

[root@ken ~]# vim /etc/my.cnf
...
#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 1003309
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog = 0
expire_logs_days = 10
...

在两个主服务器端建立用于复制的用户

mysql> grant replication slave on *.* to 'ken'@'%' identified by 'xx';

这样两个主服务器端就已经配置完成了,现在来配置从服务器端

配置从服务器端

检查从段的master_info_repository以及relay_log_info_repository

MySQL [(none)]> show global variables like '%info%';
+--------------------------------+----------------+
| Variable_name | Value |
+--------------------------------+----------------+
| master_info_repository | FILE |
| relay_log_info_file | relay-log.info |
| relay_log_info_repository | FILE |
| session_track_transaction_info | OFF |
| sync_master_info | |
| sync_relay_log_info | |
+--------------------------------+----------------+
rows in set (0.01 sec)

修改从段的master_info_repository以及relay_log_info_repository 为table

MySQL [(none)]> set global master_info_repository = 'table';
MySQL [(none)]> set global relay_log_info_repository = 'table';

从端与主1建立连接

首先需要查看主1以及主2服务器的二进制日志位置以便开始复制

主1服务器

mysql> show master status\G
*************************** . row ***************************
File: mysql-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
row in set (0.00 sec)

主2 服务器

MySQL [(none)]> show master status\G
*************************** . row ***************************
File: mysql-bin.
Position:
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
row in set (0.00 sec)

与主1建立连接

###注意:

在master_log_pos=234,后面这个起始数字不能加单引号或者双引号,否则会报错

change master to master_host='10.220.5.137',master_user='ken',master_password='xx',master_log_file='mysql-bin.000014',master_log_pos=234 for channel 'm1';

与主2建立连接

change master to master_host='10.220.5.138',master_user='ken',master_password='xx',master_log_file='mysql-bin.000009',master_log_pos=234 for channel 'm2';

启动slave

MySQL [(none)]> start slave;

查看连接状态

MySQL [(none)]> show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.137 <<主1连接成功
Master_User: ken
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin-m1.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 987ac782-d7b8-11e8-a462-000c292218ec
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 19e7075d-d7d2-11e8-8b2d-000c29629b02:-,
987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name: m1
Master_TLS_Version:
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.220.5.138 <<主2连接成功
Master_User: ken
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin-m2.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: c01b1811-d7b3-11e8--000c29492f7b
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 19e7075d-d7d2-11e8-8b2d-000c29629b02:-,
987ac782-d7b8-11e8-a462-000c292218ec:-,
c01b1811-d7b3-11e8--000c29492f7b:-
Auto_Position:
Replicate_Rewrite_DB:
Channel_Name: m2
Master_TLS_Version:
rows in set (0.00 sec)

测试

下面我们就分别在主1以及主2上面建立库和表,检查是否可以都同步到从节点上

主1服务器

在主1服务器上面建立一个ken的数据库,并在里面创建一张ken1的表,并插入一些数据等待验证

mysql> create database ken;
Query OK, row affected (0.00 sec) mysql> use ken;
Database changed
mysql> create table ken1(id int);
Query OK, rows affected (0.02 sec) mysql> insert into ken1 values();
Query OK, row affected (0.05 sec) mysql> select * from ken1;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)

主2服务器

在主2服务器上面建立一个ken6的数据库,并在里面创建一张ken的表,并插入一些数据等待验证

MySQL [(none)]>  create database ken6;
Query OK, row affected (0.00 sec) MySQL [(none)]> use ken5;
Database changed
MySQL [ken6]> create table ken(id int);
Query OK, rows affected (0.01 sec) MySQL [ken6]> insert into ken values();
Query OK, row affected (0.05 sec) MySQL [ken6]> select * from ken;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)

从服务器端

查看是否有主1以及主2新建的库表即数据

可以看到下面已经同步过来了主1上面的ken数据库以及主2上面的ken5

MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ken |
| ken5 |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
rows in set (0.00 sec)

查看主1同步过来数据

MySQL [(none)]> select * from ken.ken1;
+------+
| id |
+------+
| |
+------+

查看主2同步过来的数据

MySQL [ken5]> select * from ken5.ken;
+------+
| id |
+------+
| |
+------+
row in set (0.00 sec)

至此,多源复制的架构就完成了。

如果要想清除slave status可以先停掉同步,再执行reset slave all;即可

MySQL [ken5]> stop slave;
Query OK, rows affected (0.01 sec) MySQL [ken5]> reset slave all;
Query OK, rows affected (0.00 sec) MySQL [ken5]> show slave status\G
Empty set (0.01 sec)

MySQL系列详解十:MySQL多源复制演示-技术流ken的更多相关文章

  1. MySQL系列详解八:MySQL多线程复制演示-技术流ken

    前言 Mysql 采用多线程进行复制是从 Mysql 5.6 开始支持的内容,但是 5.6 版本下有缺陷,虽然支持多线程,但是每个数据库只能一个线程,也就是说如果我们只有一个数据库,则主从复制时也只有 ...

  2. MySQL系列详解九:MySQL级联复制演示-技术流ken

    前言 级联复制就是master服务器,只给后端一台slave服务器同步数据,然后这个slave服务器在向后端的所有slave服务器同步数据,这样就可以降低master服务器的写压力,和复制数据的网络I ...

  3. MySQL系列详解六:MySQL主从复制/半同步演示-技术流ken

    前言 随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求.此时数据库集群就很好的解决了这个问题了.采用MySQL分布式集群,能够搭建一个高并发.负载均衡的集群服务器.在 ...

  4. MySQL系列详解五: xtrabackup实现完全备份及增量备份详解-技术流ken

    xtrabackup简介 xtrabackup是一个用来对mysql做备份的工具,它可以对innodb引擎的数据库做热备.xtrabackup备份和还原速度快,备份操作不会中断正在执行的事务,备份完成 ...

  5. MySQL系列详解三:MySQL中各类日志详解-技术流ken

    前言 日志文件记录了MySQL数据库的各种类型的活动,MySQL数据库中常见的日志文件有 查询日志,慢查询日志,错误日志,二进制日志,中继日志 .下面分别对他们进行介绍. 查询日志 1.查看查询日志变 ...

  6. MySQL系列详解一:MySQL&&多实例安装-技术流ken

    简介 MySQL是一个真正的多用户.多线程SQL数据库服务器.SQL(结构化查询语言)是世界上最流行的和标准化的数据库语言,它使得存储.更新和存取信息更加容易.MySQL是一个客户机/服务器结构的实现 ...

  7. MySQL系列详解七:MySQL双主架构演示-技术流ken

    前言 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mys ...

  8. MySQL系列详解四:MySQL事务-技术流ken

    MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...

  9. MySQL系列详解二:MySQL语句操作-技术流ken

    简介 本篇博客将详细讲解mysql的一些常用sql语句操作,例如创建数据库,删除数据库,创建表,修改表,删除表,以及简单查询案例. 关于mysql数据中的SQL的大小写问题 1.不区分大小写 1. s ...

随机推荐

  1. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  2. 信息安全技能树(SecWiki中Web安全工程师职位建议)

    职位描述: 对公司网站.业务系统进行安全评估测试(黑盒.白盒测试): 对公司各类系统进行安全加固: 对公司安全事件进行响应,清理后门,根据日志分析攻击途径: 安全技术研究,包括安全防范技术,黑客技术等 ...

  3. bzoj1031(sa)

    省选前练习模板系列: #include<iostream> #include<cstdio> #include<cmath> #include<cstring ...

  4. kvm-qcow2派生镜像的远程备份的方法!

    在虚拟化环境中,关于虚拟机的远程备份是一个比较重要的环节,这个是有关于整个机房挂掉之后,仍然可以恢复的最后一招. 在kvm中这种情况可以通过直接备份虚拟机的镜像文件(qcow2)到远端存储解决. 但有 ...

  5. div+css+position实现简单的纵向导航栏

    完成效果: 这应该是很简单的纵向导航栏了. OK,进入正题 首先,我们看上面的效果图可以分析得出,要实现这个效果需要用到的技术点 1.hover 2.position 3.布局 我认为在这个效果的实现 ...

  6. 传统对象池&AB对象池

    前序: Q:为啥需要对象池? A: 游戏中大量出现或销毁对象时会反复的开堆和放堆,程序与内存之间交互过于频繁导致资源的大量浪费 Q: 对象池实现原理? A: 当子对象池没有物体的时候,它会和普通没加对 ...

  7. shiro授权测试

    shiro-permission.ini 创建存放权限的配置文件shiro-permission.ini,如下: [users] #用户zhang的密码是1111111,此用户具有role1和role ...

  8. QEMU KVM libvirt 手册(3) - Storage Media

    访问Hard Drive 使用-hda –hdb qemu-system-x86_64 -enable-kvm -name ubuntutest  -m 2048 -hda ubuntutest.im ...

  9. 背水一战 Windows 10 (120) - 后台任务: 后台上传任务

    [源码下载] 背水一战 Windows 10 (120) - 后台任务: 后台上传任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台上传任务 示例演示 uwp 的后台上 ...

  10. Python编程练习:使用 turtle 库完成叠边形的绘制

    绘制效果: 源代码: # 叠边形 import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-100) turtle.l ...