mysql的全同步复制

时间:2019-05-20 13:40:02   收藏:0   阅读:153

介绍:

Mysql的复制方法有三种:

基于行,基于sql语句,基于混合部署,还有一种就是GTIDS

Mysql的主从复制方式有三种:

同步、异步、半同步

全同步复制:在主节点上写入的数据,在从服务器上都同步完了以后才会给客户端返回成功消息,相对来说比较安全,比较靠谱。但是返回信息的时间比较慢

 

异步复制:在主节点接收到客户端发送的数据就给客户端返回执行成功的消息。然后再开始再从上面同步,不太靠谱,因为如果我给你返回消息以后,但是我的主节点坏了,并没有在从节点上同步完成,数据就会丢失,就算给客户端返回成功消息,但是我执行是不成功的(mysql默认使用异步复制)

 

半同步复制:在接收到客户端发送的数据,主节点会将数据同步到至少一台从节点以后再给客户端发送执行成功的消息,这个以前是没有的,是谷歌贡献的一个插件才可以做,它相当于是同步和异步的一个中和的复制方式。

 

MySQL全同步复制:

 

实验环境:没有主从之分,全都是一样的。

主机名

功能

Server1:192.168.200.101

组成员

Server2:192.168.200.102

组成员

Server3:192.168.200.103

组成员

 

实验步骤:

  1. 安装mysql:(三台全装,单台做演示)(因为mariadb版本太低,用mysql做)

 

  1. 为了我们数据保持一致性,我们需要将/etc/my.cnf中datadir所指定的mysql数据全部清空,保持三台数据的一致性(三台全做,单台演示)(如果是新安装的就不需要清除数据)

[root@server1 ~]# cd /usr/local/mysql/data

[root@server1 mysql]# rm -rf *

[root@server1 mysql]#/etc/init.d/mysqld start

  1. 配置/etc/my.cnf文件

配置server1:

.首先关闭数据库,删除/usr/local/mysql/data所有文件,编辑/etc/my.cnf,开启数据库,进行安全检测更改密码

 

[mysqld]

datadir=/usr/local/mysql/data

socket=/tmp/mysql.sock

server_id=1

gtid_mode=ON

enforce_gtid_consistency=ON

master_info_repository=TABLE

relay_log_info_repository=TABLE

binlog_checksum=NONE

log_slave_updates=ON

log_bin=binlog

binlog_format=ROW

 

transaction_write_set_extraction=XXHASH64

loose-group_replication_group_name="df8e2d2f-d5aa-4200-9a55-4314739974d1"

loose-group_replication_start_on_boot=off

loose-group_replication_local_address="192.168.200.101:24901"

loose-group_replication_group_seeds="192.168.200.101:24901,192.168.200.102:24901,192.168.200.103:24901"

loose-group_replication_bootstrap_group=off

loose-group_replication_single_primary_mode=off

loose-group_replication_enforce_update_everywhere_checks=on

loose-group_replication_ip_whitelist="192.168.200.0/24,127.0.0.1/8"

参数说明:

 

server_id=1        #这些设置将server配置为使用唯一标识号,以启动全局事物标示符号

gtid_mode=ON        #打开gtid保证半同步

enforce_gtid_consistency=ON    #强制持续打开

master_info_repository=TABLE    #master的信息存放在系统表中,而不是文件中

relay_log_info_repository=TABLE #io从主获取的信息

binlog_checksum=NONE         #它设置 server 打开二进制日志记录,使用基于行的格式并禁

用二进制日志事件校验和

log_slave_updates=ON        

log_bin=binlog

binlog_format=ROW        #二进制文件格式

 

#组复制设置

#loose-如果在 server 启动时尚未加载组复制插件,上面 group_replication 变量使用的 loose- 前缀将指示 server 继续启动

transaction_write_set_extraction=XXHASH64 #server必须为每个事务收集写集合,并使用 XXHASH64 哈希算法将其编码为散列。

loose-group_replication_group_name="3ec5f888-1a7d-4754-802c-d9a122ee1ad1"#告知插件,正在加入或创建的组要命名为3ec5f888-1a7d-4754-802c-d9a122ee1ad1“(uuid号)可以uuidgen查看”

loose-group_replication_start_on_boot=off #server 启动时不自动启动组复制。

loose-group_replication_local_address= "192.168.200.101:24901" #插件使用的ip地址,端口用于接受来自组中其他成员的传入连接

loose-group_replication_group_seeds="192.168.200.101:24901,192.168.200.102:24902,192.168.200.103:24903"#种子成员

loose-group_replication_bootstrap_group= off  #插件是否自动引导,注意此选项在任何时候只能在一个 server 实例上使用,通常是首次引导组时(或在整个组被崩溃然后恢复的情况下)。 如果您多次引导组,例如,当多个 server 实例设置了此选项,则它们可能会人为地造成脑裂的情况,其中存在两个具有相同名称的不同组。 在第一个server 实例加入组后禁用此选项。组中的所有 server 成员的配置都非常相似。

loose-group_replication_ip_whitelist="192.168.200.0/24" #组的白名单用户,如果不设置则不能,加入组

loose-group_replication_enforce_update_everywhere_checks=on

loose-group_replication_single_primary_mode=off

    组复制使用异步复制协议来实现分布式恢复

 

2.server1上初始化密码

 

grep password /var/log/mysqld.log

 

mysql_secure_installation

 

3.创建具有preplication-slave权限的mysql用户,此操作不应该记录到二进制日志中,以避免将更改传递到其他server实例上,步骤如下:创建用户myslave,密码为123123,配置服务器是需要使用正确的用户名和密码。

 

[root@server1 mysql]# mysql -u root -p123123

Enter password:

--------------

mysql> SET SQL_LOG_BIN=0;   #关闭防止之后传到其他server

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@‘%‘ IDENTIFIED BY ‘123123‘;

Query OK, 0 rows affected, 1 warning (0.00 sec)    #创建用户授权

 

mysql> FLUSH PRIVILEGES;  #刷新表

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;  #删除了二进制日志,这一步很重要,必须要做。

Query OK, 0 rows affected (0.16 sec)

4.开启日志

 

mysql> reset master;

Query OK, 0 rows affected (0.57 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER=‘myslave‘, MASTER_PASSWORD=‘123123‘ FOR CHANNEL ‘group_replication_recovery‘;

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

 

mysql> INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;

Query OK, 0 rows affected (0.37 sec)

 

mysql> SHOW PLUGINS;

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

| Name                       | Status   | Type               | Library              | License |

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

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

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

46 rows in set (0.00 sec)

5.开启全同步复制,创建一个表用于同步检测

 

mysql>  SET GLOBAL group_replication_bootstrap_group=ON;

Query OK, 0 rows affected (0.00 sec)

 

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected (2.80 sec)

 

mysql> SET GLOBAL group_replication_bootstrap_group=OFF;

Query OK, 0 rows affected (0.00 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

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

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

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

1 row in set (0.00 sec)

 

mysql> CREATE DATABASE test;

Query OK, 1 row affected (0.05 sec)

 

mysql> use test

Database changed

mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);

Query OK, 0 rows affected (0.81 sec)

 

mysql> INSERT INTO t1 VALUES (1, ‘Luis‘);

Query OK, 1 row affected (0.14 sec)

 

mysql> select * from t1;

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

| c1 | c2   |

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

|  1 | Luis |

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

1 row in set (0.00 sec)

6.server2上做类似步骤

 

/etc/my.cnf

 

server_id=2

 

loose-group_replication_local_address= "172.25.254.2:24901"

 

/etc/my.cnf

 

server_id=3

 

loose-group_replication_local_address= "172.25.254.3:24901"

 

[root@server2 mysql]# mysql -u root -p123123

Enter password:

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

Your MySQL connection id is 9

Server version: 5.7.17-log 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> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@‘%‘ IDENTIFIED BY ‘123123‘;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;

Query OK, 0 rows affected (0.45 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER=‘myslave‘, MASTER_PASSWORD=‘123123‘ FOR CHANNEL ‘group_replication_recovery‘;

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

 

mysql> INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;

Query OK, 0 rows affected (0.23 sec)

 

mysql> SHOW PLUGINS;

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

| Name                       | Status   | Type               | Library              | License |

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

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

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

46 rows in set (0.00 sec)

 

mysql> START GROUP_REPLICATION;

 

Query OK, 0 rows affected (6.98 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

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

| group_replication_applier | c352b7e2-d80e-11e8-a945-525400de467d | server2     |        3306 | RECOVERING   |

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

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

2 rows in set (0.00 sec)

 

mysql> +---------------------------+--------------------------------------+-

 

server3:

 

[root@server3 mysql]# mysql -uroot -p123123

Enter password:

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

Your MySQL connection id is 9

Server version: 5.7.17-log 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> SET SQL_LOG_BIN=0;

Query OK, 0 rows affected (0.00 sec)

 

mysql> GRANT REPLICATION SLAVE ON *.* TO myslave@‘%‘ IDENTIFIED BY ‘123123‘;

Query OK, 0 rows affected, 1 warning (0.00 sec)

 

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

 

mysql> reset master;

Query OK, 0 rows affected (0.46 sec)

 

mysql> SET SQL_LOG_BIN=1;

Query OK, 0 rows affected (0.00 sec)

 

mysql> CHANGE MASTER TO MASTER_USER=‘myslave‘, MASTER_PASSWORD=‘123123‘ FOR CHANNEL ‘group_replication_recovery‘;

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

 

mysql> INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;

Query OK, 0 rows affected (0.20 sec)

 

mysql> Query OK, 0 rows affected (0.59 sec)

    -> ^C

mysql>  SHOW PLUGINS;

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

| Name                       | Status   | Type               | Library              | License |

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

| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | GPL     |

| mysql_native_password      | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| sha256_password            | ACTIVE   | AUTHENTICATION     | NULL                 | GPL     |

| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | GPL     |

| group_replication          | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |

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

46 rows in set (0.00 sec)

 

mysql>  START GROUP_REPLICATION;

Query OK, 0 rows affected (3.94 sec)

 

mysql> SELECT * FROM performance_schema.replication_group_members;

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

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

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

| group_replication_applier | 4c0ae162-d80f-11e8-8946-525400e512c6 | server3     |        3306 | ONLINE       |

| group_replication_applier | c352b7e2-d80e-11e8-a945-525400de467d | server2     |        3306 | ONLINE       |

| group_replication_applier | c94d76cc-d80a-11e8-8c01-5254002ac7eb | server1     |        3306 | ONLINE       |

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

3 rows in set (0.00 sec)

 

mysql>

7.测试

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!