演示一下在MySQL下搭建多主一从的过程。

实验环境:

192.168.24.129:3306

192.168.24.129:3307

192.168.24.129:3308

主库操作

导出数据

分别在3306和3307上导出需要的数据库。

3306:

登录数据库:

[root@localhost 3306]# mysql -uroot -poldboy123 -S /tmp/mysql3306.sock

锁表:

mysql> flush tables with read lock;

状态点:

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File| Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000006 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

另开窗口开始导数据:

[root@localhost tmp]# mysqldump -uroot -poldboy123 -S /tmp/mysql3306.sock -F -R -x --master-data=2 -A --events|gzip >/tmp/dockerwy.sql.gz

在此查看状态点两个要保持一致,否则表没有锁住

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File| Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000007 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

解锁表:

mysql> unlock tables;

3307:

登录3307数据库:

[root@localhost 3307]# mysql -uroot -poldboy123 -S /tmp/mysql3307.sock

锁表:

mysql>flush tables with read lock;

查看状态点:

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File| Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000007 |      154 |              |                |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

另开窗口导数据:

[root@localhost 3307]# mysqldump -uroot -poldboy123 -S /tmp/mysql3307.sock -F -R -x --master-data=2 -A --events|gzip >/tmp/dockerwy_2.sql.gz

从新查看状态点:

mysql> show master status;

+------------------+----------+--------------+------------------+-------------------+

| File| Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000008 |      154 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

解锁表:

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

建立授权账号

分别在3306和3307上面建立授权账号

3306:

mysql> grant replication slave on *.* to 'backup'@'192.168.24.129' identified by 'backup';

3307:

mysql> grant replication slave on *.* to 'backup'@'192.168.24.129' identified by 'backup';

从库操作

修改从库存储方式

修改3308的master-info和relay-info方式,从文件存储改为表存储。

编辑配置文件

[root@localhost 3308]# vim my.cnf

在[mysqld]模块下添加如下两行

master_info_repository=TABLE

relay_log_info_repository=TABLE

重启3308数据库:

[root@localhost 3308]# /data/3308/mysqld restart

重启之后我们可以登录数据库查看;

[root@localhost 3308]# mysql -uroot -poldboy123 -S /tmp/mysql3308.sock

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'relay_log_info_repository';

+---------------------------+-------+

| Variable_name             | Value |

+---------------------------+-------+

| relay_log_info_repository | TABLE |

+---------------------------+-------+

1 row in set (0.01 sec)

mysql> show variables like 'master_info_repository';

+------------------------+-------+

| Variable_name          | Value |

+------------------------+-------+

| master_info_repository | TABLE |

+------------------------+-------+

1 row in set (0.01 sec)

导入数据

导入3306的数据:

[root@localhost 3308]# gzip -d /tmp/dockerwy.sql.gz

[root@localhost 3308]# mysql -uroot -poldboy123 -S /tmp/mysql3308.sock < /tmp/dockerwy.sql.

导入3307的数据:

[root@localhost 3308]# gzip -d /tmp/dockerwy_2.sql.gz

[root@localhost 3308]# mysql -uroot -poldboy123 -S /tmp/mysql3308.sock < /tmp/dockerwy_2.sql

执行change master to

登录slave进行同步操作,分别change master两台服务器,后面以for channel ‘channel_name’区分

mysql> change master to master_host='192.168.24.129',master_user='backup',master_port=3306,master_password='backup',master_log_file='mysql-bin.000006',master_log_pos=154 for channel 'master_1';

Query OK, 0 rows affected, 2 warnings (0.07 sec)

mysql> change master to master_host='192.168.24.129',master_user='backup',master_port=3307,master_password='backup',master_log_file='mysql-bin.000007',master_log_pos=154 for channel 'master_2';

Query OK, 0 rows affected, 2 warnings (0.04 sec)

启动slave操作

可以通过start slave的方式去启动所有的复制,也可以通过单个复制源的方式,下面介绍单个复制的的启动演示

mysql> start slave for channel 'master_1';

Query OK, 0 rows affected (0.01 sec)

mysql> start slave for channel 'master_2';

Query OK, 0 rows affected (0.02 sec)

查看同步状态

正常启动后,可以查看同步的状态,执行show slave status for channel ‘channel_name\G’查看复制源master_1的同步状态;

mysql> show slave status for channel 'master_1'\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.24.129

Master_User: backup

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000008

Read_Master_Log_Pos: 154

Relay_Log_File: localhost-relay-bin-master_1.000006

Relay_Log_Pos: 367

Relay_Master_Log_File: mysql-bin.000008

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: 154

Relay_Log_Space: 634

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: 129

Master_UUID: df233252-afd5-11e6-8070-000c2962d708

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:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name: master_1

Master_TLS_Version:

1 row in set (0.00 sec)

查看master_2的同步状态

mysql> mysql> show slave status for channel 'master_2'\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.24.129

Master_User: backup

Master_Port: 3307

Connect_Retry: 60

Master_Log_File: mysql-bin.000008

Read_Master_Log_Pos: 154

Relay_Log_File: localhost-relay-bin-master_2.000004

Relay_Log_Pos: 367

Relay_Master_Log_File: mysql-bin.000008

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: 154

Relay_Log_Space: 634

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: 130

Master_UUID: 49bf20e1-afe2-11e6-aef5-000c2962d708

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:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name: master_2

Master_TLS_Version:

1 row in set (0.00 sec)

mysql 5.7.16多源复制的更多相关文章

  1. MySQL 5.7的多源复制

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

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

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

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

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

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

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

  5. MySQL多源复制【转】

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

  6. MySQL 5.7 多源复制实践

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

  7. mysql 5.7多源复制(用于生产库多主库合并到一个查询从库)

    目前我们使用的是主从+分库分表的系统架构,主库有N个分库,从库为多个slave做负载均衡,所以数据库端的架构是下面这样的: 因为差不多有一年半没有专门搞技术为主了,顺带回顾下. 这就涉及到多个主库数据 ...

  8. (转)MySQL多源复制

    原文:https://dev.mysql.com/doc/refman/5.7/en/replication-multi-source.html MySQL多源复制概述 MySQL多源复制使复制从接受 ...

  9. MySQL 5.7.9的多源复制

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

随机推荐

  1. windows service自动重启服务

    服务一般都能正常的运行,但有时候也会有一些假死现象,比如公司有一考勤服务就因为依赖于硬件厂家的api, 但厂家api运行一段时间后会默名的假死,引起整个服务假死,因为这一假死现象具有不确定性,所以不太 ...

  2. WEB API 用MemoryStream流做下载功能

    刚开始把MemoryStream 放在 var streamResult = new MemoryStream(); HttpResponseMessage response = new HttpRe ...

  3. 为什么在Windows有两个临时文件夹的环境变量Temp和Tmp?

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:为什么在Windows有两个临时文件夹的环境变量Temp和Tmp?.

  4. Asp.net Mvc 自定义Session (一),

    大家都知道用系统默认的session 会存在这样的问题 如果用户过多的话 session 会自动消亡,而且不能支持分布式和集群. 这系列博客主要讲解  怎样 解决用户过多的session自动消亡,和分 ...

  5. 14周事情总结-机器人-大数据hadoop

    14周随着考试的进行,其他该准备的事情也在并行的处理着,考试内容这里不赘述了 首先说下,关于机器人大赛的事情,受益颇多,机器人的制作需要机械和电控两方面 昨天参与舵机的测试,遇到的问题:舵机不动 排查 ...

  6. 计算内存容量(measure)

    $m =gwmi Win32_PhysicalMemory $m|measure -Property capacity #计算 Property 出现次数 $m|measure -Property c ...

  7. SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' 的访问

    sqlserver2008导入excel到数据库的时候报错: SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程 'sys.sp_OACreate' ...

  8. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  9. 【转】以XML文件方式保存用户数据——2013-08-25 22

    正在做项目中有很多游戏数据要保存,常见的玩家数据这些比较简单的可以用CCUserDefault.它是cocos2d-x用来存取基本数据类型用的.保存为XML文件格式. 主要方法:(和java的map很 ...

  10. 【转】cocos2d-x 模仿计时器效果,动态增加分数——2013-08-25 16

    http://www.cocos2dev.com/?p=90 游戏中要用到分数是动态增加的,而不是瞬间加上去的. bool HelloWorld::init() { if ( !CCLayer::in ...