mysql主从之基于gtid的主从复制

一 GITD介绍

1.1 gtid的含义

Global Transaction Identifier,全局事务标识

阿里云的rds目前已经使用gtid

基于gtid的主从复制原理

每个mysql数据库上都有一个唯一uuid

每个事务生成一个id

gtid由上面两者组合: uuid+事务id

1.2 优势

相对使用binlog+位置的方法来说

gtid让配置主从更加方便

从提升为主时比较方便

二 配置

2.1 主库的配置

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 1
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

[[email protected] mysql]# vim /etc/my.cnf

[[email protected] mysql]# systemctl stop mysqld

[[email protected] mysql]# rm -rf /data/mysql/*

[[email protected] mysql]# mysqld --initialize --user=mysql

查看uuid

[[email protected] mysql]# pwd
/data/mysql
[[email protected] mysql]# ll

查看数据库的uuid

在数据目录的auto.cnf

[[email protected] mysql]# cat auto.cnf

在mysql里面使用命令查看show global variables like ‘server_uuid‘

[[email protected] mysql]# systemctl start mysqld
[[email protected] mysql]# mysql -u root -p
Enter password:  rrV.sL)!d9zE
mysql> show global variables like ‘server_uuid‘;  #查看uuid
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | ad5ee77e-9f2e-11e9-a76d-000c2991dd19 |
+---------------+--------------------------------------+
1 row in set (0.01 sec)

2.2 从库配置

[mysqld]
bind-address=0.0.0.0
port=3306
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
skip-name-resolve
slow_query_log=on
long_query_time=1
slow_query_log_file=/data/mysql/mysql-slow.log
innodb-file-per-table=1
innodb_flush_log_at_trx_commit = 2
log_warnings = 1
connect_timeout = 60
net_read_timeout = 120
performance_schema_max_table_instances = 400
server-id = 2
gtid_mode=on
enforce_gtid_consistency=on
log-slave-updates=1
log-bin=master-bin
log-bin-index = master-bin.index
relay-log = relay-log
relay-log-index = relay-log.index
binlog_format=row

[mysqld_safe]
log-error=/data/mysql/mysqld.log
pid-file=/data/mysql/mysqld.pid

开启数据库,查看uuid

[[email protected] ~]# systemctl stop mysqld
[[email protected] ~]# rm -rf /data/mysql/*
[[email protected] ~]# mysqld --initialize --user=mysql
2019-07-05T14:24:23.975473Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 96435cd6-9f30-11e9-a575-000c2963fd11.
2019-07-05T14:24:23.977416Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2019-07-05T14:24:23.980860Z 1 [Note] A temporary password is generated for [email protected]: RN=hsqIFX2BO
[[email protected] ~]#  systemctl start mysql
Failed to start mysql.service: Unit not found.
[[email protected] ~]#  systemctl start mysqld
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like ‘server_uuid‘;
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 96435cd6-9f30-11e9-a575-000c2963fd11 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

[[email protected] ~]# cd /data/mysql/

[[email protected] mysql]# cat auto.cnf

三 配置主从

3.1 主库给从库复制权限

mysql> grant replication slave on *.* to ‘replication‘@‘192.168.132.122‘ identified by ‘1234567‘;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       177 |
| master-bin.000002 |       854 |
+-------------------+-----------+
mysql> show binlog events in ‘master-bin.000002‘;
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.26-log, Binlog ver: 4 |
| master-bin.000002 | 123 | Previous_gtids | 1 | 154 | |
| master-bin.000002 | 154 | Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1‘ |      # 事件类型是GTID  数据库的uuid+事务ID
| master-bin.000002 | 219 | Query | 1 | 398 | ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH ‘mysql_native_password‘ AS ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
| master-bin.000002 | 398 | Gtid | 1 | 463 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2‘ |
| master-bin.000002 | 463 | Query | 1 | 702 | GRANT REPLICATION SLAVE ON *.* TO ‘replication‘@‘192.168.132.122‘ IDENTIFIED WITH ‘mysql_native_password‘ AS ‘*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5‘ |
| master-bin.000002 | 702 | Gtid | 1 | 767 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3‘ |
| master-bin.000002 | 767 | Query | 1 | 854 | flush privileges |
+-------------------+-----+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

3.2 从库使用命令进行复制

mysql> change master to master_host=‘192.168.132.121‘,master_port=3306,master_user=‘replication‘,master_password=‘1234567‘,master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.01 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.132.121
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 854
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 1069
        Relay_Master_Log_File: master-bin.000002
             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: 854
              Relay_Log_Space: 1270
              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: 1
                  Master_UUID: ad5ee77e-9f2e-11e9-a76d-000c2991dd19    #主库的UUID
             Master_Info_File: /data/mysql/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: ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1-3      #执行的GTID位置
            Executed_Gtid_Set: 96435cd6-9f30-11e9-a575-000c2963fd11:1,
ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version: 

3.3 观察从的binlog日志

mysql> show master logs;
+-------------------+-----------+
| Log_name          | File_size |
+-------------------+-----------+
| master-bin.000001 |       177 |
| master-bin.000002 |      1098 |
+-------------------+-----------+
mysql> show binlog events in ‘master-bin.000002‘;
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                     |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 |    4 | Format_desc    |         2 |         123 | Server ver: 5.7.26-log, Binlog ver: 4                                                                                                                    |
| master-bin.000002 |  123 | Previous_gtids |         2 |         154 |                                                                                                                                                          |
| master-bin.000002 |  154 | Gtid           |         2 |         219 | SET @@SESSION.GTID_NEXT= ‘96435cd6-9f30-11e9-a575-000c2963fd11:1‘                                                                                        |
| master-bin.000002 |  219 | Query          |         2 |         398 | ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH ‘mysql_native_password‘ AS ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘                                     |
| master-bin.000002 |  398 | Gtid           |         1 |         463 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:1‘                                                                                        |
| master-bin.000002 |  463 | Query          |         1 |         642 | ALTER USER ‘root‘@‘localhost‘ IDENTIFIED WITH ‘mysql_native_password‘ AS ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘                                     |
| master-bin.000002 |  642 | Gtid           |         1 |         707 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:2‘                                                                                        |
| master-bin.000002 |  707 | Query          |         1 |         946 | GRANT REPLICATION SLAVE ON *.* TO ‘replication‘@‘192.168.132.122‘ IDENTIFIED WITH ‘mysql_native_password‘ AS ‘*6A7A490FB9DC8C33C2B025A91737077A7E9CC5E5‘ |
| master-bin.000002 |  946 | Gtid           |         1 |        1011 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3‘                                                                                        |
| master-bin.000002 | 1011 | Query          |         1 |        1098 | flush privileges                                                                                                                                         |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ 

四 验证

增加数据来验证是否同步成功

4.1 建库验证

主创建一个数据库

mysql> create database darren;

mysql> show binlog events in ‘master-bin.000002‘;

从端查看

mysql> show databases;

mysql> show slave status\G;

4.2 建表验证

主创建一个表

mysql> use darren;
Database changed
mysql> create table test (id int);
Query OK, 0 rows affected (0.01 sec)

mysql> show binlog events in ‘master-bin.000002‘;
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name          | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                     |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000002 |  702 | Gtid           |         1 |         767 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:3‘                                                                                        |
| master-bin.000002 |  767 | Query          |         1 |         854 | flush privileges                                                                                                                                         |
| master-bin.000002 |  854 | Gtid           |         1 |         919 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:4‘                                                                                    |
| master-bin.000002 |  919 | Query          |         1 |        1019 | create database darren                                                                                                                                   |
| master-bin.000002 | 1019 | Gtid           |         1 |        1084 | SET @@SESSION.GTID_NEXT= ‘ad5ee77e-9f2e-11e9-a76d-000c2991dd19:5‘                                                                                        |
| master-bin.000002 | 1084 | Query          |         1 |        1188 | use `darren`; create table test (id int)                           #建表,同是增长ID                                                                                      |
+-------------------+------+----------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+

从端:

mysql> show slave status\G;

4.3.插改验证

mysql> insert into test values (1);

mysql> update test set id = 3 where id = 1;

mysql> show binlog events in ‘master-bin.000002‘;

从端查看

mysql> show slave status\G;

mysql> select * from darren.test;

删除

mysql> delete from test;

mysql> drop database darren;

mysql> show binlog events in ‘master-bin.000002‘;

从端查看

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> show binlog events in ‘master-bin.000002‘;

基本的配置验证完成

五 日志名未指定,主机名改变导致主从失效

前面介绍过类似的情况,因为配置文件没有指定日志名,导致MySQL在重启的时候,日志名发生改变,导致同步失败

从端的slave日志未指定,解决

stop slave;    #停掉salve

reset slave;   #重置salve

change master to master_host=‘192.168.132.121‘,master_port=3306,master_user=‘replication‘,master_password=‘1234567‘,master_auto_position = 1;   #重新配从端复制数据

start slave;    #开启slave

主端未指定,修复主从

stop slave;   #停掉slave

reset slave;   #重置slave

reset master;  #重置master,必须做,很重要

change master to  master_host=‘192.168.132.121‘,master_port=3306, master_user=‘replication‘,master_password=‘123456‘,master_auto_position = 1;  3重新配置主从

start slave; #开启slave

原文地址:https://www.cnblogs.com/zyxnhr/p/11141234.html

时间: 2024-10-02 01:49:54

mysql主从之基于gtid的主从复制的相关文章

Mysql 基于GTID的主从复制及切换

参考 http://imysql.com/tag/gtid http://mysqllover.com/?p=594 Mysql 基于GTID的主从复制及切换 一.主从复制配置 两个mysql服务的my.cnf 中相关内容配置 [mysqld] #从复制数据库表设置 replicate-wild-ignore-table = mysql.%,information_schema.%,innodb.%,innodb_log.%,performance_schema.%,test.%,tmp.% #

企业——MYSQL(基于GTID)的主从复制

一.什么是主从复制? 主从复制,是用来建立一个和主数据库完全一样的数据库环境,称为从数据库:主数据库一般是准实时的业务数据库. 二.主从复制的作用(好处,或者说为什么要做主从) 1.做数据的热备,作为后备数据库,主数据库服务器故障后,可切换到从数据库继续工作,避免数据丢失. 2.架构的扩展.业务量越来越大,I/O访问频率过高,单机无法满足,此时做多库的存储,降低磁盘I/O访问的频率,提高单个机器的I/O性能. 3.读写分离,使数据库能支撑更大的并发.在报表中尤其重要.由于部分报表sql语句非常的

MySQL 基于 GTID 的主从复制和多实例配置

配置基于 GTID 的主从复制 1.修改 master 和 slave 的配置文件 server-id=113 gtid_mode=on enforce-gtid-consistency=on replicate-do-db=gateway_target # 如果只需同步部分表,就在 slave 上配置这两个额外项 replicate-do-table=gateway_target.t_target_snapshot 2.导出 master 的库和表结构到 slave,先停止 master my

基于GTID的主从复制数据库

基于GTID的主从复制数据库 全局身份识别 GTID(global transaction identifier) 为了实现主备数据库的强一致性 GTID = source_id:transaction_id source_id 表示执行事务的主库 transaction_id 是一个序列号,表示这个主库上执行的第 n 个事务. server_uuid是系统自动生成的,用来的替代server_id,因为source_id是手工设置的,可能会有冲突 数据库的安装和初始化 server33,44:

基于GTID的主从复制搭建

前置检查 server-id = 10,master/slave不允许重复 log-bin gtid-mode = ON enforce-gtid-consistency = ON 1,利用mysqlpump复制master数据到slave,搭建基于GTID的主从复制,缺少GTID处理方法,暂不成功. mysqlpump --host= --user= --password= --single-transaction --default-parallelism=4 --compress-outp

MySQL/MariaDB数据库基于SSL实现主从复制

前言 备份数据库是生产环境中的首要任务,有时候不得不通过网络进行数据库的复制,由于MySQL/MariaDB的主从复制是明文传送的,如果在生产环境中跨网络传送,数据的安全性就无法完全保证,为了解决这一问题,我们需要一种安全的方式进行传送,即基于SSL加密进行数据传输. 部署配置 实验拓扑 实验环境 系统环境:CentOS6.6 数据库版本:mariadb-5.5.36 #注意:主从服务器数据库版本须一致:主从服务器时间须同步 #此实验从服务器只做一组为例 配置主从复制 安装mariadb [[e

MySQL 5.7基于GTID复制的常见问题和修复步骤(二)

[问题二] 有一个集群(MySQL5.7.23)切换后复制slave报1236,其实是不小心在slave上执行了事务导致 Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containin

docker下部署MySQL8基于GTID的主从复制

安装docker#yum install docker添加docker镜像仓库#vim /etc/docker/daemon.json {"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]} 拉取mysql镜像*# docker pull mysql 创建mysql容器# docker run -dit --name lisamysql001 -p 33307:3307 -e MYSQLROOTPAS

mysql主从配置&&基于keepalived的主备切换

mysql互为主从设置 && 主备切换配置 需求说明: 1.公司架构一直是一台单独的mysql在线上跑,虽然一直没有出现什么宕机事件,但是出于一个高可用的考虑,提出主从备份.主备切换的需求: 2.实现这个需求的前一段时间只是在做数据库备份的时候实现了主从热备,为了实现主备切换功能,继续操作上述需求: 实验环境: master1:10.1.156.3:3306 master2:10.1.156.5:3306 my.cnf配置文件关于主从这块的修改: master1: server-id =