MYSQL---GTID主从备份

MYSQL—GTID主从备份

GITD定义

1.全局事物标识(global transaction identifieds)
2.GTID事物是唯一性一个事务对应一个GTID
3.一个GTID在服务器上只执行一次,避免重复执行,导致数据混乱
4.使用master_auto_postion=1的方式自动匹配GTID断点进行复制
5.mysql-5.6.5开始支持,mysql-5.6.10开始完善

GTID = source_id:transation_id

GTID的组成部分

server_uuid:后面是一个序列号
uuid:每个mysql实例的唯一ID,由于会传递到slave,所以可以理解为源ID
Sequence number:在每台mysql服务器上都是从1开始自增长的序列,一个数值对应一个事务。

GTID比传统复制的优势

1.更容易实现failover,不用一千那样在需要找log_file和log_pos.
2.更简单的搭建主从复制
3.更加安全
4.GTID是连续没有空洞的,因此主从库出现数据冲突时,可以用添加空事物的方式进行跳过。

GTID工作原理

1.master更新数据时,会在事务前产生GTID,一同记录到binlog日志中
2.slave端的I/O线程将变更的binlog,写入到本地的relay log中
3.sql线程从reloy log中获取GTID,然后对比slave端的binlog是否有记录
4.如果有记录,说明该GTID的事务已经执行,slave会忽略
5.如果没有记录,slave就好从relay log中执行该GTID的事务,并记录到binglog
6.在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。

MYSQL基于GTID的主从备份

准备两台虚拟机,分别两台都安装MySQL,步骤略。详情参照MySQL主从配置这篇文章。

数据库角色 IP 应用与系统版本
主数据库 192.168.228.23 centos7
从数据库 192.168.228.30 centos7

在主数据库服务器修改/etc/my.cnf配置文件

[root@yxr ~]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

#GTID
server-id=23  //主服务器id(可以修改成自己的主机,方便区分)
gtid-mode=on    //开启gtid模式
enforce-gtid-consistency=on  //强制gtid一致性,开启后对于特定create table不被支持
#binlog
log_bin=master-binlog
log-slave-updates=1
binlog_format=row  //强烈建议,其他格式可能导致数据不一致
#relay log
skip_slave_start=1

重启mysql服务

[root@yxr ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@yxr ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      128          :::22                       :::*                  
LISTEN     0      100         ::1:25                       :::*                  
LISTEN     0      80           :::3306                     :::*                  

在主数据库上创建一个同步账号授权给从数据库使用

[root@yxr ~]# 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.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> create user 'repl'@'192.168.228.30' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'repl'@'192.168.228.30';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

重新开一个终端,给主数据库上读锁(避免备份途中与其他人在写入导致数据同步的不一致)

[root@yxr ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
(注意:此锁表的终端必须在备份完成以后才能退出)

在从数据库服务器操作如下

修改配置文件

[root@arongya ~]# vim /etc/my.cnf
[root@arongya ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve

#GTID 添加以下内容
gtid_mode=on    //控制开启/关闭GTID模式
enforce_gtid_consistency=on
server_id=30
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog-format=row  //强制建议,其他格式可能导致数据不一致
#relay log
skip-slave-start=1

重启服务

[root@arongya ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL..... SUCCESS! 

配置并启动主从复制,首先先进入mysql数据库

mysql> change master to
    -> master_host='192.168.228.23',
    -> master_user='repl',
    -> master_password='123456',
    -> master_port=3306,
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> start slave;
Query OK, 0 rows affected (0.06 sec)

查看从服务器的状态:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.228.23
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000001
          Read_Master_Log_Pos: 773
               Relay_Log_File: arongya-relay-bin.000002
                Relay_Log_Pos: 994
        Relay_Master_Log_File: master-binlog.000001
             Slave_IO_Running: Yes   //这里必须与我的一致为yes
            Slave_SQL_Running: Yes  //这里必须与我的一致为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: 773
              Relay_Log_Space: 1203
              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: 128
                  Master_UUID: 6be5e744-b3c9-11e8-a134-000c29989243
             Master_Info_File: /opt/data/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: 6be5e744-b3c9-11e8-a134-000c29989243:1-3
            Executed_Gtid_Set: 6be5e744-b3c9-11e8-a134-000c29989243:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

解除主库的锁表状态,直接退出交互式界面即可

mysql> quit 
Bye

验证,在主库创建名为yaoxiaorong的数据库,并添加内容,插入数据

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database yaoxiaorong;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yaoxiaorong        |
+--------------------+
5 rows in set (0.00 sec)

mysql> use yaoxiaorong;
Database changed
mysql> create table student (id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.10 sec)

mysql> show tables;
+-----------------------+
| Tables_in_yaoxiaorong |
+-----------------------+
| student               |
+-----------------------+
1 row in set (0.00 sec)

mysql> insert into student(id,name,age)values(1,'tom',20),(2,'jerry',28),(3,'sean',25);
Query OK, 3 rows affected (0.11 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   28 |
|  3 | sean  |   25 |
+----+-------+------+
3 rows in set (0.00 sec)

在从数据库服务器上查看是否同步存在yaoxiaorong库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yaoxiaorong        |
+--------------------+
5 rows in set (0.04 sec)

mysql> use yaoxiaorong;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | jerry |   28 |
|  3 | sean  |   25 |
+----+-------+------+
3 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/zzs183yxr_zzs/article/details/82556201