MySQL5.7.10多元复制功能搭建

MySQL5.7.10多元复制功能搭建

1.环境:centos6.5
[[email protected] mysql-5.7.10]# cat /etc/redhat-release
CentOS release 6.5 (Final)
主机:

mysql-m1   192.168.56.21
mysql -m2  192.168.56.22
mysql-s    192.168.56.23

现在是mysql-m1数据库已经搭建好,我使用自己的虚拟机进行clone 就省去多次安装数据库。
2.确保两个主库的server_id log-bin 开启 并且三个库的server_id 不同:

[[email protected] aliyun]# sed -n ‘36,37p‘ /etc/my.cnf
log-bin = /aliyun/data/mysqllogs/bin-log/mysql-bin
server_id = 1

[[email protected] mysql-5.7.10]# sed -n ‘36,37p‘ /etc/my.cnf
log-bin = /aliyun/data/mysqllogs/bin-log/mysql-bin
server_id = 2
[[email protected] mysql-5.7.10]# sed -n ‘36,37p‘ /etc/my.cnf
log-bin = /aliyun/data/mysqllogs/bin-log/mysql-bin
server_id = 3

3.master上创建测试库和表:
3.1登陆数据库mysql-m1创建库test01 和表h1:

[[email protected] tmp]# mysql -uroot -p -S /tmp/mysql.sock

mysql> create database test01;
Query OK, 1 row affected (0.01 sec)
mysql> use test01;
Database changed

mysql> CREATE TABLE `h1` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `b1` int(11) DEFAULT NULL,   `b2` int(11) DEFAULT NULL,   `b3` int(11) GENERATED ALWAYS AS ((`b1` + `b2`)) VIRTUAL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected (0.03 sec)

mysql> desc h1;
+-------+---------+------+-----+---------+-------------------+
| Field | Type    | Null | Key | Default | Extra             |
+-------+---------+------+-----+---------+-------------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment    |
| b1    | int(11) | YES  |     | NULL    |                   |
| b2    | int(11) | YES  |     | NULL    |                   |
| b3    | int(11) | YES  |     | NULL    | VIRTUAL GENERATED |
+-------+---------+------+-----+---------+-------------------+
4 rows in set (0.00 sec)

mysql> insert into h1(b1,b2) values(2,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from h1;
+----+------+------+------+
| id | b1   | b2   | b3   |
+----+------+------+------+
|  1 |    2 |    2 |    4 |
+----+------+------+------+
1 row in set (0.00 sec)

3.2登陆数据库mysql-m2创建库test01 和表和h2:

mysql> create database test01;
Query OK, 1 row affected (0.03 sec)

mysql>  CREATE TABLE `h2` (   `id` int(11) NOT NULL AUTO_INCREMENT,   `e1` int(11) DEFAULT NULL,   `e2` int(11) DEFAULT NULL,   `e3` int(11) GENERATED ALWAYS AS ((`e1` + `e2`)) VIRTUAL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into h2(e1,e2) values(3,3);
Query OK, 1 row affected (0.06 sec)

mysql> select * from h2;
+----+------+------+------+
| id | e1   | e2   | e3   |
+----+------+------+------+
|  1 |    3 |    3 |    6 |
+----+------+------+------+
1 row in set (0.00 sec)

以下不停库锁表,自动切割bin-log日志,全量备份bin-log,导入从库中(此方法只适合5.5版本以下的数据库备份,不适合5.7版本的数据库备份,有待验证)(不是这篇文档的步骤)

[[email protected] bin-log]# mysqldump -uroot -p -S /tmp/mysql.sock --master-data=2 -A -B -R -x -F --events|gzip >/tmp/m1.sql.gz

[[email protected] mysql-5.7.10]#  mysqldump -uroot -p -S /tmp/mysql.sock --master-data=2 -A -B -R -x -F --events|gzip >/tmp/m2.sql.gz

适合5.7版本的多元同步的数据库备份(不同的表同步到一个数据库中):

[[email protected] tmp]#  mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases  --add-drop-database  test01  >m1.sql
[[email protected] tmp]#  mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases  --add-drop-database  test01  >m2.sql
[[email protected] tmp]# cat m1.sql|grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000012‘, MASTER_LOG_POS=154;
[[email protected] tmp]#  cat m2.sql|grep "CHANGE MASTER"
-- CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000010‘, MASTER_LOG_POS=154;

[[email protected] tmp]#  scp -rp -P22 m1.sql 192.168.56.23:/tmp/
[[email protected] tmp]#  scp -rp -P22 m2.sql 192.168.56.23:/tmp/

4.slave上操作:

[[email protected] tmp]# ll /tmp/
total 396
-rw-r--r--. 1 root  root  199233 May  8 01:15 m1.sql
-rw-r--r--. 1 root  root  199243 May  8 01:14 m2.sql
srwxrwxrwx. 1 mysql mysql      0 May  8 00:35 mysql.sock
-rw-------. 1 mysql mysql      5 May  8 00:35 mysql.sock.lock
[[email protected] tmp]# mysql -uroot -p -S /tmp/mysql.sock
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| txt001             |
+--------------------+
5 rows in set (0.06 sec)

分别在Slave上把Master_1和Master_2的数据导入Slave服务器,在导入前先修改从库的MySQL存储master-info和relay-info的方式,即从文件存储改为表存储,在my.cnf里添加以下选择:(注意此修改的是从库)

[[email protected] tmp]# cat -n /etc/my.cnf|sed -n ‘36,39p‘
    36  log-bin = /aliyun/data/mysqllogs/bin-log/mysql-bin
    37  server_id = 1
    38  master_info_repository=TABLE
    39  relay_log_info_repository=TABLE

或者再MySQL从库命令行操作:

[[email protected] ~]# mysql -uroot -p -S /tmp/mysql.sock
mysql> stop slave;
mysql> SET GLOBAL master_info_repository = ‘TABLE‘;
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL relay_log_info_repository = ‘TABLE‘;
Query OK, 0 rows affected (0.00 sec)

把数据导入数据库:
[[email protected] tmp]# mysql -uroot -p -S /tmp/mysql.sock </tmp/m2.sql
[[email protected] tmp]# mysql -uroot -p -S /tmp/mysql.sock </tmp/m1.sql

[[email protected] ~]# mysql -uroot -p -S /tmp/mysql.sock
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test01             |
| txt001             |
+--------------------+
6 rows in set (0.00 sec)

mysql> use test01;
Database changed
mysql> show tables;
+------------------+
| Tables_in_test01 |
+------------------+
| h1               |
| h2               |
+------------------+
2 rows in set (0.00 sec)

mysql> select * from h1;
+----+------+------+------+
| id | b1   | b2   | b3   |
+----+------+------+------+
|  1 |    2 |    2 |    4 |
+----+------+------+------+
1 row in set (0.00 sec)

mysql> select * from h2;
+----+------+------+------+
| id | e1   | e2   | e3   |
+----+------+------+------+
|  1 |    3 |    3 |    6 |
+----+------+------+------+
1 row in set (0.00 sec)

5.在mysql-m1和mysql-m2上创建复制账号:
这个操作跟MySQL 5.7之前版本一样:
在mysql-m1上创建

mysql> grant replication slave on *.* to ‘rep‘@‘192.168.56.23‘ identified by ‘123456‘;
mysql> flush privileges;(必须刷新权限否则不生效的)

在mysql-m2上创建

mysql> grant replication slave on *.* to ‘rep‘@‘192.168.56.23‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.02 sec
mysql> flush privileges; (必须刷新权限否则不生效的)

6.在slave从库上操作进行change master :

[[email protected] ~]# mysql -uroot -p -S /tmp/mysql.sock
mysql> change master to MASTER_HOST=‘192.168.56.21‘ ,MASTER_USER=‘rep‘ ,MASTER_PASSWORD=‘123456‘ ,MASTER_LOG_FILE=‘mysql-bin.000012‘ ,MASTER_LOG_POS=154 FOR CHANNEL ‘mysql-m1‘;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql>  change master to MASTER_HOST=‘192.168.56.22‘ ,MASTER_USER=‘rep‘ ,MASTER_PASSWORD=‘123456‘ ,MASTER_LOG_FILE=‘mysql-bin.000010‘ ,MASTER_LOG_POS=154 FOR CHANNEL ‘mysql-m2‘;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave for CHANNEL ‘mysql-m1‘;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave for CHANNEL ‘mysql-m2‘;
Query OK, 0 rows affected (0.00 sec)

mysql>

[[email protected] ~]# mysql -uroot -p123456 -S /tmp/mysql.sock -e "SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m1‘\G"|egrep "Slave_IO_Running|Slave_SQL_Running"
mysql: [Warning] Using a password on the command line interface can be insecure.
Slave_IO_Running: Connecting (报错) 此处为yes才是正常的
Slave_SQL_Running: Yes
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
7.同步报错解决方法:

7.1.看错误日志:
[[email protected] ~]# cat /aliyun/data/mysql/mysql_3306.err

7.2.检查防火墙是否关闭:
(从库本地和主库本地的防火墙)

[[email protected] ~]# mysql -urep -p123456 -h192.168.56.21
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.56.21‘ (113)

[[email protected] ~]# mysql -urep -p123456 -h192.168.56.22
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘192.168.56.22‘ (113)

[[email protected] ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:
[[email protected] tmp]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:  

[[email protected] tmp]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[[email protected] tmp]#

7.3. 修改/ect/my.cnf中server_id 未生效:

[[email protected] ~]# mysql -urep -p123456 -h192.168.56.21 成功连接
但是从库连接mysql-m1还是报错:
但是从库连接mysql-m2还是报错:

[[email protected] ~]# mysql -uroot -p123456 -S /tmp/mysql.sock -e "SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m1‘\G"|egrep  "Slave_IO_Running|Slave_SQL_Running|Last_IO_Error"
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
      Last_IO_Error_Timestamp: 160508 03:03:05

原因是/ect/my.cnf 配置文件中修改server_id时。没有重启服务,所以修改没有生效
[[email protected] mysql-5.7.10]# cd /aliyun/server/mysql-5.7.10/
pkill mysqld
[[email protected] mysql-5.7.10]# sh start_mysql.sh
三个数据库都同样操作

7.4.但是还是报错:

 [[email protected] ~]# mysql -uroot -p123456 -S /tmp/mysql.sock -e "SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m1‘\G"|egrep  "Slave_IO_Running|Slave_SQL_Running|Last_IO_Error"
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: No
            Slave_SQL_Running: Yes
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
      Last_IO_Error_Timestamp: 160508 03:23:36

原因是mysql-s 虚拟机是基于mysql-m1的虚拟机克隆过来的,mysqlm1上的文件auto.cnf和mysql-s /mysql=m2上文件的auto.cnf 是一样的,所以报错

[[email protected] mysql]# cat /aliyun/data/mysql/auto.cnf
[auto]
server-uuid=3652e06e-1467-11e6-ab9f-000c2970db66

[[email protected] mysql-5.7.10]# cat /aliyun/data/mysql/auto.cnf
[auto]
server-uuid=3652e06e-1467-11e6-ab9f-000c2970db66
[[email protected] mysql-5.7.10]# cat /aliyun/data/mysql/auto.cnf
[auto]
server-uuid=3652e06e-1467-11e6-ab9f-000c2970db66
把这三个auto.cnf文件中的数值改成随便不一样的就行。然后依次重启数据库,就好了
[[email protected] mysql-5.7.10]# mysql -uroot -p123456 -S /tmp/mysql.sock -e "SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m1‘\G"|egrep  "Slave_IO_Running|Slave_SQL_Running|Last_IO_Error"
mysql: [Warning] Using a password on the command line interface can be insecure.
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
                Last_IO_Error:
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
      Last_IO_Error_Timestamp:

可以通过查看performance_schema相关的表查看同步状态,执行命令:

mysql> SELECT * FROM performance_schema.replication_connection_status;
监控复制状态
+--------------+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+
| CHANNEL_NAME | GROUP_NAME | SOURCE_UUID                          | THREAD_ID | SERVICE_STATE | COUNT_RECEIVED_HEARTBEATS | LAST_HEARTBEAT_TIMESTAMP | RECEIVED_TRANSACTION_SET | LAST_ERROR_NUMBER | LAST_ERROR_MESSAGE | LAST_ERROR_TIMESTAMP |
+--------------+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+
| mysql-m1     |            | 3652e06e-1467-11e1-ab9f-000c2970db66 |      NULL | OFF           |                        37 | 2016-05-08 04:26:38      |                          |                 0 |                    | 0000-00-00 00:00:00  |
| mysql-m2     |            | 3652e06e-1467-11e6-ab9f-000c2970db66 |        41 | ON            |                        47 | 2016-05-08 04:51:32      |                          |                 0 |                    | 0000-00-00 00:00:00  |
+--------------
+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+

mysql> stop slave;(停止时必须同时开启各个slave,否则不能同步)

Query OK, 0 rows affected (0.01 sec)

mysql> set global sql_slave_skip_counter=10;
mysql> start slave for CHANNEL ‘mysql-m1‘;
mysql> start slave for CHANNEL ‘mysql-m2‘;
mysql> SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m1‘\G
mysql> SHOW SLAVE STATUS FOR CHANNEL ‘mysql-m2‘\G

8.MySQL 5.7的多源复制用处:
8.1、MySQL 5.7的多源复制,能有效的解决分库分表的数据统计问题,同时也可以实现在一台从服务器对多台主服务器的数据备份。

8.2、MySQL 5.7的多源复制的出现,我们就不需要使用MariaDB 的多主一从的架构了,让很多小伙伴又看到了新的希望。
参考资料:

http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html
http://www.longlong.asia/2015/10/21/mysql57-new-features.html

MySQL-5.7文档看考资料:
http://www.cnblogs.com/xuanzhi201111/p/5148113.html
mysql多元复制资料地址
http://www.mamicode.com/info-detail-1189601.html
mysql 数据库5.7编译安装
http://www.cnblogs.com/xuanzhi201111/p/5148113.html
http://www.mamicode.com/info-detail-1189601.html
http://huqiji.iteye.com/blog/2068613

原文地址:http://blog.51cto.com/wujianwei/2066178

时间: 2024-10-03 02:43:45

MySQL5.7.10多元复制功能搭建的相关文章

MySQL5.1版本的主从复制搭建

MySQL5.1版本的主从复制搭建         [备注]: 主机192.168.250.20 备机192.168.250.40 主从复制 1主机上面已经有了MySQL5.1.53-log版. 2备机上没有mysql数据库,需要在备机上安装MySQL5.1.53-log软件,具体参考svn文档"system\mysql\document\IT_SYS_MySQL_Installation.docx". 3再次确定主服务器与从服务器的MySQL版本一致 Showvariables li

mysql5.7 MGR集群搭建

mysql5.7 MGR集群搭建部署 此文章由队员(谆谆)拟写 此文章来自 乌龟运维 官网 wuguiyunwei.com QQ群 602183872 最近看了一下mysql5.7的MGR集群挺不错的,有单主和多主模式,于是乎搭建测试了一下效果还不错,我指的不错是搭建和维护方面都比较简单.网上绝大多数都是单主模式,当然我这里也是,为了加深印象,特意记录一下搭建过程,等以后再去尝试多主模式,相信大家现在数据库的瓶颈基本都是在写,读写分离虽然是一种可行的解决方案,但是如果数据量很大,写一样会有问题,

mysql主从复制概述以及配置mysql5.7.10实现简单主从复制

什么是主从复制: 通过将Mysql的某一台主机的 数据复制到其它主机,复制过程中一个服务器充当主服务器(master),而一个或多个其它服务器充当从服务器(slave).进行复制时,所有对数据表的写操作必须在主服务器上进行.否则,因为主服务器不会同步从服务器的数据,会导致主从数据不一致的问题.mysql的主从复制功能是构建高性能大型应用服务器的基础 主从复制的作用: 1.辅助实现数据的备份 2.实现数据服务的高可用和异地容灾 3.实现多个服务器分摊负载 主从复制的实现原理: 实现整个复制过程主要

spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 思路都是一样的,只不过把struts2替换成了spring mvc 二.不同的地方 工程目录及jar包: action包改成controller: 删除struts2 jar包,添加spring mvc包(已有的话,不需添加):     web.xml配置: 跟之前不同的地方是把struts2的过滤器替换成了一个se

Windows安装Mysql5.7.10绿色版

今天在Windows上安装Mysql的时候,去官网上下了一个最新版本的Mysql5.7.10绿色版,但是之前网上安装方式都过时了,比如会报一些常见的错误“[ERROR] Fatal error: Can't open and lock privilegetables: Table 'mysql.user' doesn't exist”,毕竟是开源的东西,官方文档比较全,自己动手丰衣足食啊.下面是我参考官方文档的安装步骤. 1.解压下载的mysql安装包,复制mysql-default.ini,然

MySQL5.7.10配置和使用

前两天搞mybatis和springmvc的结合,搞了半天项目就是跑不起来,由于都没有接触过,也不知道原因出在哪里.今天想彻底学习下mybatis,于是就想使用MySQL.从官网上下了,MySQL-5.7.10免安装版,本来以为只奥简单配置下就好的,硬是花了两个小时,汗.... 1.下载MySQL解压D:\mysql-5.7.10-winx64 2.在MySQL目录下新建一个my.ini文件,将下列内容复制过去: [mysql] # 设置mysql客户端默认字符集 default-charact

MySQL5.7.10免安装版配置

 最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: mysql-5.7.10-winx64 + win7(64位) 一: 下载 下载东西, 肯定是去官网下载, 点击 http://dev.mysql.com/downloads/mysql/ 下载 选择对应的版本, 进行进行下载. 这个地方可以点击跳过, 不用登陆就可以直接下载. 二: 配置环境变量 我习惯把软件安装在 D:\Program Files 下面, 所以

MYSQL5.7.10 安装文档

1.安装依赖包 yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison 2.安装boost库: 首先先查询是否已经安装过boost rpm -qa boost* 卸载旧boost-*等库: yum -y remove boost-* 下载Boost库,在解压后复制到/usr/local/boost目录下,然后重新cmake并在后面的

CentOS 5.9编译安装配置mysql-5.6.10

说明:操作系统:CentOS 5.9 64位MySQL版本:mysql-5.6.10MySQL安装目录:/usr/local/mysqlMySQL数据库存放目录:/data/mysql准备篇:一.配置好IP.DNS .网关,确保使用远程连接工具能够连接服务器,服务器yum命令可以正常使用二.配置防火墙,开启3306端口vi /etc/sysconfig/iptables  #编辑-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -