Preface

 
    Classic replication is commonly used in previous version of MySQL.It's really tough in managing them when our replications get into failures.Many new features are also depend on GTID.So it's urgent to use GTID replication as soon as possible.I'm gonna to demenstrate how to change classic replication to GTID replication online with two servers.Here we go.
 
Framework
Hostname IP/Port Identity OS Version MySQL Version GTID Mode Binlog Format
zlm2 192.168.1.101/3306 master CentOS 7.0 5.7.21 off row
zlm3 192.168.1.102/3306 slave CentOS 7.0 5.7.21 off row
 
Procedure
 
Check  parameter "gtid_mode" and is "OFF" on both master and slave in the replication group.
 //Master
(root@localhost mysql3306.sock)[(none)]>show variables like 'gtid_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | OFF |
+---------------+-------+
row in set (0.01 sec //Slave
(root@localhost mysql3306.sock)[(none)]>show variables like 'gtid_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode | OFF |
+---------------+-------+
row in set (0.00 sec)
Execute sysbench to generate some transactions continuously on master.
 [root@zlm2 :: ~/sysbench-1.0/src/lua]
#sysbench oltp_insert.lua --mysql-host=192.168.1.101 --mysql-port= --mysql-user=zlm --mysql-password=zlmzlm --mysql-db=sysbench --tables= --table-size= --mysql-storage-engine=innodb prepare
sysbench 1.0. (using bundled LuaJIT 2.1.-beta2) Creating table 'sbtest1'...
Inserting records into 'sbtest1'
Creating a secondary index on 'sbtest1'...
Creating table 'sbtest2'...
Inserting records into 'sbtest2'
Creating a secondary index on 'sbtest2'...
... [root@zlm2 :: ~/sysbench-1.0/src/lua]
#sysbench oltp_insert.lua --mysql-host=192.168.1.101 --mysql-port= --mysql-user=zlm --mysql-password=zlmzlm --mysql-db=sysbench --threads= --time= --report-interval= --rand-type=uniform run
sysbench 1.0. (using bundled LuaJIT 2.1.-beta2) Running the test with following options:
Number of threads:
Report intermediate results every second(s)
Initializing random number generator from current time Initializing worker threads... Threads started! [ 60s ] thds: tps: 1623.71 qps: 1623.71 (r/w/o: 0.00/1623.71/0.00) lat (ms,%): 2.97 err/s: 0.00 reconn/s: 0.00
[ 120s ] thds: tps: 1844.96 qps: 1844.96 (r/w/o: 0.00/1844.96/0.00) lat (ms,%): 2.61 err/s: 0.00 reconn/s: 0.00
[ 180s ] thds: tps: 1894.37 qps: 1894.37 (r/w/o: 0.00/1894.37/0.00) lat (ms,%): 2.61 err/s: 0.00 reconn/s: 0.00
... //Check the output of processlist.
(root@localhost mysql3306.sock)[(none)]>show processlist;
+----+------+------------+----------+-------------+------+---------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+------------+----------+-------------+------+---------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| | root | localhost | NULL | Query | | starting | show processlist |
| | repl | zlm3: | NULL | Binlog Dump | | Master has sent all binlog to slave; waiting for more updates | NULL |
| | zlm | zlm2: | sysbench | Query | | update | INSERT INTO sbtest1 (id, k, c, pad) VALUES (, , '57837919367-24452778030-14591605115-8049012633 |
| | zlm | zlm2: | sysbench | Query | | update | INSERT INTO sbtest1 (id, k, c, pad) VALUES (, , '45087463438-93604980565-67881991526-9944080034 |
| | zlm | zlm2: | sysbench | Query | | update | INSERT INTO sbtest1 (id, k, c, pad) VALUES (, , '01822437471-94427682076-39418270545-9867829936 |
+----+------+------------+----------+-------------+------+---------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
rows in set (0.00 sec)
Make sure that the classic replication is working normally on slave.
 (root@localhost mysql3306.sock)[(none)]>show slave status\G
*************************** . row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: repl
Master_Port:
Connect_Retry:
Master_Log_File: mysql-bin.
Read_Master_Log_Pos:
Relay_Log_File: relay-bin.
Relay_Log_Pos:
Relay_Master_Log_File: mysql-bin.
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:
Last_Error:
Skip_Counter:
Exec_Master_Log_Pos:
Relay_Log_Space:
Until_Condition: None
Until_Log_File:
Until_Log_Pos:
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master:
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:
Last_IO_Error:
Last_SQL_Errno:
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:
Master_UUID: 1b7181ee-6eaf-11e8-998e-080027de0e0e
Master_Info_File: mysql.slave_master_info
SQL_Delay:
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: System lock
Master_Retry_Count:
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0 //This means we are using the classic replication now.
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)
Change the parameter "enforce_gitd_consistency" to "warn" on both master and slave.
 //Master
(root@localhost mysql3306.sock)[(none)]>set @@global.enforce_gtid_consistency=warn;
Query OK, rows affected (0.13 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| WARN |
+-----------------------------------+
row in set (0.06 sec) //Error log of master
--13T07::56.877416+: [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.
--13T07::15.748645+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 8825ms. The settings might not be optimal. (flushed= and evicted=, during the time.) //Slave
(root@localhost mysql3306.sock)[(none)]>set @@global.enforce_gtid_consistency=warn;
Query OK, rows affected (0.49 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| WARN |
+-----------------------------------+
row in set (1.35 sec) //Error log of slave
--13T07::02.556232+: [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN. //Make sure there's no warning messages on both master and slave.
Change the parameter "enforce_gitd_consistency" to "on" on both master and slave.
 //Master
(root@localhost mysql3306.sock)[(none)]>set @@global.enforce_gtid_consistency=on;
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| ON |
+-----------------------------------+
row in set (0.00 sec) //Slave
(root@localhost mysql3306.sock)[(none)]>set @@global.enforce_gtid_consistency=on;
Query OK, rows affected (0.03 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| ON |
+-----------------------------------+
row in set (0.00 sec)
Change  the parameter "gtid_mode" to "off_permissive"  on both master and slave.
 //Master
(root@localhost mysql3306.sock)[(none)]>set @@globa.gtid_mode=off_permissive;
Query OK, rows affected (0.72 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.gtid_mode;
+--------------------+
| @@global.gtid_mode |
+--------------------+
| OFF_PERMISSIVE |
+--------------------+
row in set (0.01 sec) //Error log of master
--13T07::56.877416+: [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.
--13T07::15.748645+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 8825ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::38.472436+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 8569ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::03.886312+: [Note] Changed ENFORCE_GTID_CONSISTENCY from WARN to ON.
--13T07::04.137251+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 5067ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::39.586306+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 5394ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::38.441594+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 4927ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::19.070954+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 4539ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T07::20.930564+: [Note] Changed GTID_MODE from OFF to OFF_PERMISSIVE.
--13T07::36.490470+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 4602ms. The settings might not be optimal. (flushed= and evicted=, during the time.) //Slave
(root@localhost mysql3306.sock)[(none)]>set @@global.gtid_mode=off_permissive;
Query OK, rows affected (3.02 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.gtid_mode;
+--------------------+
| @@global.gtid_mode |
+--------------------+
| OFF_PERMISSIVE |
+--------------------+
row in set (0.00 sec) //Error log of slave
--13T07::02.556232+: [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.
--13T07::22.628014+: [Note] Changed ENFORCE_GTID_CONSISTENCY from WARN to ON.
--13T07::33.136288+: [Note] Aborted connection to db: 'unconnected' user: 'root' host: 'localhost' (Got timeout reading communication packets)
--13T07::27.360767+: [Note] Changed GTID_MODE from OFF to OFF_PERMISSIVE.
--13T07::39.972826+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 10489ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
Change  the parameter "gtid_mode" to "on_permissive"  on both master and slave.
 //Master
(root@localhost mysql3306.sock)[(none)]>set @@global.gtid_mode=on_permissive;
Query OK, rows affected (3.26 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.gtid_mode;
+--------------------+
| @@global.gtid_mode |
+--------------------+
| ON_PERMISSIVE |
+--------------------+
row in set (0.00 sec) //Error log of master
--13T07::16.796632+: [Note] Changed GTID_MODE from OFF_PERMISSIVE to ON_PERMISSIVE.
--13T07::20.034425+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 4954ms. The settings might not be optimal. (flushed= and evicted=, during the time.) //Slave
(root@localhost mysql3306.sock)[(none)]>set @@global.gtid_mode=on_permissive;
Query OK, rows affected (2.22 sec) (root@localhost mysql3306.sock)[(none)]>select @@global.gtid_mode;
+--------------------+
| @@global.gtid_mode |
+--------------------+
| ON_PERMISSIVE |
+--------------------+
row in set (0.06 sec) //Error log of slave
--13T07::57.921081+: [Note] Changed GTID_MODE from OFF_PERMISSIVE to ON_PERMISSIVE.
--13T07::03.109628+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 5853ms. The settings might not be optimal. (flushed= and evicted=, during the time.) //I'm afraid it's better to execut "set gtid_mode=on_permissive;" on slave first for best practice even though sometimes it's not obliged to do that.
Make sure all the binlogs generated by classic replication has been disappeared on both master and slave by checking parameter 'ongoing_anonymous_transaction_count' whether it returns "0".
 //Master
(root@localhost mysql3306.sock)[(none)]>show status like 'ongoing_anonymous_transaction_count';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | |
+-------------------------------------+-------+
row in set (0.66 sec) //Slave
(root@localhost mysql3306.sock)[(none)]>show status like 'ongoing_anonymous_transaction_count';
+-------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | |
+-------------------------------------+-------+
row in set (3.34 sec) //The value of 'ongoing_anonymous_transaction_count' become "0" what means there arn't non-gtid events in binlogs anymore.Therefore,we can do the last step,that is,to change the "gtid_mode" to "on".
Change the parameter "gtid_mode" to "on" on both master and slave.
 //Master
(root@localhost mysql3306.sock)[(none)]>select @@global.gtid_mode;
+--------------------+
| @@global.gtid_mode |
+--------------------+
| ON |
+--------------------+
row in set (0.00 sec) //Error log of master
--13T08::59.853460+: [Note] Changed GTID_MODE from ON_PERMISSIVE to ON.
--13T08::01.804678+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 6035ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T08::56.202081+: [Note] Aborted connection to db: 'unconnected' user: 'repl' host: 'zlm3' (Failed on my_net_write()) //Slave
(root@localhost mysql3306.sock)[(none)]>set @@global.gtid_mode=on;
ERROR (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:
Current database: *** NONE *** //It's stuck here.Oh my!!! //Check the error log of slave see what has happened.
--13T08::49.070915+: [ERROR] Disk is full writing './relay-bin.000044' (Errcode: - No space left on device). Waiting for someone to free space...
--13T08::49.070948+: [ERROR] Retry in secs. Message reprinted in secs
--13T08::49.104353+: [ERROR] Disk is full writing '/data/mysql/mysql3306/logs/mysql-bin.000011' (Errcode: - No space left on device). Waiting for someone to free space...
--13T08::49.104382+: [ERROR] Retry in secs. Message reprinted in secs
--13T08::51.712891+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 4001ms. The settings might not be optimal. (flushed= and evicted=, during the time.)
--13T08::00.346384+: [Note] InnoDB: page_cleaner: 1000ms intended loop took 7634ms. The settings might not be optimal. (flushed= and evicted=, during the time.) //It shows "[ERROR] Disk is full writing ... ".The test tables have been inserted too many data. [root@zlm3 :: ~]
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root .4G .4G 20K % / //The root directory is full.
devtmpfs 488M 488M % /dev
tmpfs 497M 497M % /dev/shm
tmpfs 497M 6.6M 491M % /run
tmpfs 497M 497M % /sys/fs/cgroup
/dev/sda1 497M 118M 379M % /boot
none 87G 80G .1G % /vagrant //Unfortunately,the disk on salve has been writen fully.
    Notwithstanding the demonstrating was interupted accidentally but the porcedure of changing classic replication to GTID replicatioin is correct.Onlyif  the slave has finished to change the "gtid_mode" to "on",the implementing is accomplished.
    One more thing need to do is to modify your "my.cnf" file to make them support GTID replication after restarting your mysqld process.Make sure these three parameters:"enforce_gtid_consistency=on","gtid_mode=on","log_slave_updates=on" are right in your configuration file "my.cnf".
    The last thing to do in this case is to stop slave,set "master_auto_position=1"  and start slave again.I'm not going to do these last steps here('cause the environment has been destroyed.oops!).
 
Some error masseages may occur if you don't implement follow the sequence above.
 //The output of "show salve status\G"
Last_IO_Errno:
Last_IO_Error: The replication receiver thread cannot start because the master has GTID_MODE = ON and this server has GTID_MODE = OFF //You cannot modify "gtid_mode" to "on" directly.
(root@localhost mysql3306.sock)[(none)]>set @@global.gtid_mode=on;
ERROR (HY000): The value of @@GLOBAL.GTID_MODE can only be changed one step at a time: OFF <-> OFF_PERMISSIVE <-> ON_PERMISSIVE <-> ON. Also note that this value must be stepped up or down simultaneously on all servers. See the Manual for instructions.
Summary
  • GTID replication is the best practice in MySQL replicaiton now,especially in 5.7 version above.More and more new good features are relies on GTID,such as "Group Replication","Group Commit","Parallel Replication",etc.
  • We'd better replace all the classic replication to GTID replication in our product environment in order to get more benifits and work efficiently.
  • Chang classic replicaiton to GTID replicaiton online should follow the order of "off -> off_permissive -> on_permissive -> on" and execute them on both master and slaves.
  • Notice that change online is only support on MySQL 5.7.6 and above.
 

MySQL 5.7传统复制到GTID在线切换(一主一从)的更多相关文章

  1. MySQL 5.7 传统复制到GTID在线切换

    来源:http://wubx.net/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究. 前题: 要求MySQL 5.7.6及以后版本. 所有组 ...

  2. (5.12)mysql高可用系列——复制中的在线切换GTID模式/增加节点/删除节点

    目录 [0]需求 前提,已经假设好基于传统异步复制的主库和从库1. [0.1]传统异步切换成基于GTID的无损模式 [0.2]增加特殊要求的从库 [1]操作环境 [2]构建 复制->半同步复制 ...

  3. MySQL 5.7--多源复制(非GTID模式)

    ==================================================== 在MYSQL5.7版本中引入多源复制,一个从库允许复制多个主库的数据,每个主库被配置为一个单独 ...

  4. MySQL5.7不停业务将传统复制变更为GTID复制

      由于GTID的优势,我们需要将传统基于file-pos的复制更改为基于GTID的复制,如何在线变更成为我们关心的一个点,如下为具体的方法: 目前我们有一个传统复制下的M-S结构: port 330 ...

  5. MySQL的GTID复制与传统复制的相互转换

    主库:192.168.225.128:3307从库1:192.168.225.129:3307 Gtid作为5.6版本以来的杀手级特性,却因为不支持拓扑结构内开关而饱受诟病.如果你需要从未开启GTID ...

  6. 在线建立或重做mysql主从复制架构方法(传统模式和GTID模式)【转】

    mysql主从复制架构,是mysql数据库主要特色之一,绝大多数公司都有用到. 而GTID模式是基于事务的复制模式的意思,发展到现在也是越来越多人用. 以前很多文章,介绍搭建mysql主从复制架构,是 ...

  7. MySQL的GTID复制与传统复制的相互切换

    MySQL的GTID复制与传统复制的相互转换 1. GTID复制转换成传统复制 1.1 环境准备 1.2 停止slave 1.3 查看当前主从状态 1.4 change master 1.5 启动主从 ...

  8. 与MySQL传统复制相比,GTID有哪些独特的复制姿势?

    与MySQL传统复制相比,GTID有哪些独特的复制姿势? http://mp.weixin.qq.com/s/IF1Pld-wGW0q2NiBjMXwfg 陈华军,苏宁云商IT总部资深技术经理,从事数 ...

  9. mysql同步复制异常的常见操作-传统复制方式

    mysql同步复制异常的常见操作-传统复制方式 一.传统复制方式是基于非gtid的,常见的错误有1032和1062 1032是主键冲突,1062是从库中没有找到对应的记录. 可以查看出现错误的binl ...

随机推荐

  1. 自己的spring boot starter

    这篇文章说的更加详细具体:https://www.cnblogs.com/hjwublog/p/10332042.html 在刚开始看spring boot的时候,发现这么多starter,不免觉得好 ...

  2. Jersey框架简介

    一 Jersey简介 开发RESTful WebService意味着支持在多种媒体类型以及抽象底层的客户端-服务器通信细节,如果没有一个好的工具包可用,这将是一个困难的任务 为了简化使用JAVA开发R ...

  3. POJ 3020——Antenna Placement——————【 最小路径覆盖、奇偶性建图】

    Antenna Placement Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  4. DotNetty 跨平台的网络通信库(转)

    久以来,.Net开发人员都非常羡慕Java有Netty这样,高效,稳定又易用的网络通信基础框架.终于微软的Azure团队,使用C#实现的Netty的版本发布.不但使用了C#和.Net平台的技术特点,并 ...

  5. java-logic====吃货联盟

    1.系统界面 2.功能一 3.查看餐袋 4.签收订单 5.删除订单 6.我要点赞 主要分在两个类中: 第一个类entity,主要的是一些共有的属性 主要代码: public class entity ...

  6. hibernate课程 初探一对多映射2-3 创建hibernateUtil工具类

    本节主要内容:创建hibernateUtil工具类:demo demo: HibernateUtil.java package hibernate_001; import org.hibernate. ...

  7. JavaScript比较和逻辑运算符

    JavaScript比较和逻辑运算符 JavaScript比较和逻辑运算符 比较和逻辑运算符用于测试true或者false. 比较运算符 比较运算符在逻辑语句中使用,以测定变量或值是否相等 例如设定x ...

  8. textarea存起来的数据把空格也存起来

    textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...

  9. Java 笔记 —— java 和 javac

    Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...

  10. u-boot分析(九)----nand flash初始化|nand flash读写分析

    u-boot分析(九) 上篇博文我们按照210的启动流程,分析到了初始化串口,由于接下来的取消存储保护不是很重要,所以我们今天按照u-boot的启动流程对nand flash初始化进行分析. 今天我们 ...