基于GTID环境搭建主从复制
1.环境
----------------------------------------------------------|
|mysql版本 | 5.7.14 | 5.7.14 |
|----------|----------------------------------------------|
|ip |192.168.26.233(master) | 192.168.26.75 (slave)|
|port | 3306 | 3306 |
|---------------------------------------------------------|
 
1. 主库上必须启用两个参数
gtid-mode = on
enforce-gtid-consistency = 1
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
server-id = 3306100
binlog_format = row
skip_slave_start=1 数据库起来后不能直接开启复制
 
2. 查看master库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000020 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
Executed_Gtid_Set 是空的
 
mysql> use zw
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> show tables;
+--------------+
| Tables_in_zw |
+--------------+
| mycode |
| t1 |
+--------------+
2 rows in set (0.00 sec)
 
mysql> insert into t1 values(2);
Query OK, 1 row affected (0.00 sec)
 
mysql> show global variables like '%server%';
+---------------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------+
| character_set_server | utf8 |
| collation_server | utf8_general_ci |
| innodb_ft_server_stopword_table | |
| server_id | 3306100 |
| server_id_bits | 32 |
| server_uuid | 7e354a2c-6f5f-11e6-997d-005056a36f08 |
+---------------------------------+--------------------------------------+
6 rows in set (0.00 sec)
server_uuid : 是 instance的唯一标识
 
3.创建一个表,插入测试数据
mysql> create table t2 (id int(6));
Query OK, 0 rows affected (0.20 sec)
 
mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
 
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000020 | 814 | | | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
Executed_Gtid_Set 有gtid的信息了
7e354a2c-6f5f-11e6-997d-005056a36f08
7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 后面多了个1-3,1-3就是gtid的事物信息
 
4.master端备库全库
[root@zw-test-db tmp]# mysqldump -S /tmp/mysql3306.sock -uroot -p --single-transaction --master-data=2 -A >/tmp/full_backup.sql
Enter password:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database.
If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
会有一个警告: 大概意思是说备份里有GTID的信息,如果不需要GTID的信息,可以加上后面提示的参数过滤掉,这个警告不用管
 
备份里有gtid的信息
SET @@GLOBAL.GTID_PURGED='7e354a2c-6f5f-11e6-997d-005056a36f08:1-3';
1-3,做同步后,第4个事物会直接传过去
 
5.将master端的备份到scp到 slave端
[root@zw-test-db tmp]# scp full_backup.sql root@192.168.26.108:/tmp
 
二 、slave操作:
1.slave端的参数文件
gtid-mode = on
enforce-gtid-consistency = 1
log-bin = /data/mysql/mysql_3306/logs/mysql-bin
server-id = 3306101
binlog_format = row
skip_slave_start=1 数据库起来后不能直接开启复制
注意:server-id不能一样
 
2.重启mysql数据库
[root@llmj-svn-26-81 mysql_3306]# mysqladmin -S /tmp/mysql3306.sock -uroot -p shutdown
Enter password:
[root@llmj-svn-26-81 mysql_3306]# ps -ef|grep mysql
root 9756 9701 0 11:31 pts/2 00:00:00 grep mysq
[root@llmj-svn-26-81 mysql_3306]# mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf &
[1] 9762
[root@llmj-svn-26-81 mysql_3306]# mysqld --defaults-file=/data/mysql/mysql_3306/my3306.cnf &
[1] 9762
 
3.恢复到 slave端
[root@llmj-svn-26-81 tmp]# mysql -S /tmp/mysql3306.sock -uroot -p < /tmp/full_backup.sql
Enter password:
[root@llmj-svn-26-81 tmp]# mysql -S /tmp/mysql3306.sock -uroot -p < /tmp/full_backup.sql
Enter password:
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.
注意:多次恢复会报错,因为里面已经有gtid的信息了。
如果你还想继续强制恢复,可以在从库上 reset master ;
 
4.在slave端查看master状态
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000004 | 154 | | | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+------------------------------------------+
| Variable_name | Value |
+----------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
| session_track_gtids | OFF |
+----------------------------------+------------------------------------------+
8 rows in set (0.00 sec)
 
5.reset master信息
mysql> reset master;
Query OK, 0 rows affected (0.11 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+-------+
| Variable_name | Value |
+----------------------------------+-------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------+
8 rows in set (0.01 sec)
可以看到 gtid_executed 和 gtid_purged 已经没有gtid的信息了
 
6.然后再重新恢复:
[root@llmj-svn-26-81 tmp]# mysql -S /tmp/mysql3306.sock -uroot -p < /tmp/full_backup.sql
Enter password:
然后就没有报错了,恢复成功!
 
7.然后 change master to ,这时候跟 binlog+position 有点变化
mysql> change master to master_host='192.168.26.233', master_port=3306, master_user='repl',master_password='repl', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.23 sec)
master_log_file='mysql-bin.000016',master_log_pos=154 这两个参数替换成了 master_auto_position=1
 
8.启动slave
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.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000020
Read_Master_Log_Pos: 814
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000020
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: 814
Relay_Log_Space: 655
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
Master_Info_File: /data/mysql/mysql_3306/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:
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 状态正常!
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
状态正常!
 
三、测试
1. 从库操作:
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 154 | | | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
 
2. 从库上操作,create table ,然后 show master status;
mysql> create table t3(id int(6));
Query OK, 0 rows affected (0.26 sec)
mysql> insert into t3 values (1);
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+-----------------+--------+-------------+----------------+---------------------------------------------------------------------------------+
| File |Position|Binlog_Do_DB |Binlog_Ignore_DB|Executed_Gtid_Set |
+-----------------+--------+-------------+----------------+---------------------------------------------------------------------------------+
| mysql-bin.000001| 564| | |7e354a2c-6f5f-11e6-997d-005056a36f08:1-3,ba0d5587-74d6-11e6-ab5c-005056a3f46e:1-2|
+-----------------+--------+-------------+----------------+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
可以看到从库的gtid的信息,也就是说从库的操作可以查出来;1-3是是主库操作的gtid,后面1-2是从库的操作的gtid
 
四、测试同步情况
1.主库操作:
mysql> use zw;
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 mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 7 | ggg |
+----+---------+
6 rows in set (0.00 sec)
 
2.从库查看mycode表
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 7 | ggg |
+----+---------+
6 rows in set (0.00 sec)
 
3.主库insert 一条数据
mysql> insert into mycode values(6,'fff');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | fff |
| 7 | ggg |
+----+---------+
7 rows in set (0.00 sec)
 
4.查看从库
mysql> select * from mycode;
+----+---------+
| id | content |
+----+---------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
| 4 | ddd |
| 5 | eee |
| 6 | fff |
| 7 | ggg |
+----+---------+
7 rows in set (0.00 sec)
从库数据也同步过来啦!

mysql gtid 主从复制的更多相关文章

  1. MySQL GTID 主从复制错误修复方法

    https://yq.aliyun.com/articles/155827?spm=5176.8067842.tagmain.6.RFPTAL MySQL 传统的主从复制方式使用 master_log ...

  2. mysql GTID主从复制故障后不停机恢复同步流程

    GTID实现主从复制数据同步 GTID是一个基于原始mysql服务器生成的一个已经被成功执行的全局事务ID,它由服务器ID以及事务ID组成,这个全局事务ID不仅仅在原始服务器上唯一,在所有主从关系的m ...

  3. mysql GTID主从复制(主库在线,添加新丛库)

    要求: 1.         主库上线,主库不停止服务的前提下做主从复制 2.         新添加一个丛库 操作: 1.         在主库导出数据(主库正常运行): 2.         将 ...

  4. MySQL GTID 错误处理汇总

    MySQL GTID是在传统的mysql主从复制的基础之上演化而来的产物,即通过UUID加上事务ID的方式来确保每一个事物的唯一性.这样的操作方式使得我们不再需要关心所谓的log_file和log_P ...

  5. 企业级-Mysql双主互备高可用负载均衡架构(基于GTID主从复制模式)(原创)

    前言:          原理与思想        这里选用GTID主从复制模式Mysql主从复制模式,是为了更加确保主从复制的正确性.健康性与易配性.这里做的是两服务器A,B各有Mysql实例331 ...

  6. 实现mysql的读写分离(mysql-proxy)____1(mysql的主从复制,基于gtid的主从复制,半同步复制,组复制)

    主从复制原理: 从库生成两个线程,一个I/O线程,一个SQL线程: i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中:主库会生成一个 log ...

  7. MySQL 5.7 基于GTID主从复制+并行复制+半同步复制

    环境准备 IP HOSTNAME SERVICE SYSTEM 192.168.131.129 mysql-master1 mysql CentOS7.6 192.168.131.130 mysql- ...

  8. Centos7.5部署MySQL5.7基于GTID主从复制+并行复制+半同步复制+读写分离(ProxySQL) 环境- 运维笔记 (完整版)

    之前已经详细介绍了Mysql基于GTID主从复制的概念,原理和配置,下面整体记录下MySQL5.7基于GTID主从复制+并行复制+增强半同步复制+读写分离环境的实现过程,以便加深对mysql新特性GT ...

  9. MySQL GTID (一)

    MySQL GTID 系列之一 一.GTID相关概念 GTID:全局事务标识符,MySQL5.6版本开始在主从复制中推出的重量级特性. 每提交一个事务,当前执行线程都会拿到一个给定复制环境中唯一的GT ...

随机推荐

  1. NetBeans运行项目报错

    1.错误描述 严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start co ...

  2. hdu5820 Lights

    主席树 但是能够想到题解的做法很难 #include <stdio.h> #include <string.h> #include <vector> #includ ...

  3. java实现全排列问题

    1.问题描述: 一组字符串的全排列,按照全排列的顺序输出,并且每行结尾无空格. 2.输入: 输入一个字符串 3.输入示例: 请输入全排列的字符串: abc 4.输出示例: a b c a c b b ...

  4. NOIWC2018游记

    NOIWC2018游记 接着PKUWC就是NOIWC了.感觉时间很紧呀,但越是紧张呢,就越让人心里觉得充实. 能够去NOIWC,应该是一次非常充实的体验吧. 这一篇游记是接着上一篇写的,时间点上完全都 ...

  5. [CQOI2015]任务查询系统

    把一个任务拆成两个,在s时加入,在e+1时减去即可 直接离散化后上主席树 # include <bits/stdc++.h> # define IL inline # define RG ...

  6. [.NET Core] 简单读取 json 配置文件

    简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...

  7. eclipse 精确查询

    ---恢复内容开始--- ctrl+H(一般都是这个,如果无效看你的自定义快捷键) 输入\b 查询的字符串 \b   后面的正则表达式选框必须选

  8. 云计算之路-阿里云上:服务器CPU 100%问题是memcached的连接数限制引起的

    非常抱歉,昨天的服务器CPU 100%问题是达到 memcached 的连接数限制引起的,不是阿里云服务器的问题. 之前我们用的是阿里云“云数据库 memcached 版”,上个周末我们换成了自己搭建 ...

  9. C# QQ邮箱注册,以及数秒

    一. 这是前台需要的东西 <asp:TextBox ID="Textemail" runat="server" CssClass="nonein ...

  10. elfinder源码浏览-Volume文件系统操作类(1)

    今天看了一个文件管理的java后台源码,elfinder 发现这个东东比我写的代码效率告到不知道哪去了,苦思冥想后还是抽点时间看看吧.. 它实现了我们电脑上的所以关于文件操作的动作,并生成了api开放 ...