mysql5.7 主从复制的正常切换【转】
目前环境如下:
master server IP:172.17.61.131
slave server IP:172.17.61.132
mysql version: mysql-5.7.21-linux
目标:计划内的主从复制正常切换
1.切换之前需要检查slave的同步状态,对master进行锁表。
slave server: 确保Slave_IO_Running和Slave_SQL_Running都为YES
- mysql> show slave status \G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.17.61.131
- Master_User: repl
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql_bin.000036
- Read_Master_Log_Pos: 154
- Relay_Log_File: slave_relay_bin.000003
- Relay_Log_Pos: 367
- Relay_Master_Log_File: mysql_bin.000036
- Slave_IO_Running: Yes
- Slave_SQL_Running: Yes
- ...
- 1 row in set (0.00 sec)
master server进行锁表操作,由于我是测试环境没有应用,所以锁表很快。
2.在flush tables with read lock成功获得锁之前,必须等待所有语句执行完成(包括SELECT)。所以如果有个慢查询在执行,或者一个打开的事务,或者其他进程拿着表锁,flush tables with read lock就会被阻塞,直到所有的锁被释放。
- mysql> flush tables with read lock;
- Query OK, 0 rows affected (0.01 sec)
3.接着要确保master所有的binlog已同步到slave,并且slave都已apply了所有的binlog。
maser server:
- mysql> show processlist;
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- | 2 | repl | 172.17.61.132:60079 | NULL | Binlog Dump | 1907 | Master has sent all binlog to slave; waiting for more updates | NULL |
- | 3 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+------+---------------------+------+-------------+------+---------------------------------------------------------------+------------------+
- 2 rows in set (0.00 sec)
slave server:
- mysql> show processlist;
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | 1 | system user | | NULL | Connect | 1918 | Slave has read all relay log; waiting for more updates | NULL |
- | 2 | system user | | NULL | Connect | 1918 | Waiting for master to send event | NULL |
- | 4 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- 3 rows in set (0.00 sec)
4.停止slave的进程
slave sever:
- mysql> STOP SLAVE IO_THREAD;
- Query OK, 0 rows affected (0.02 sec)
- mysql> show processlist;
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | Id | User | Host | db | Command | Time | State | Info |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- | 1 | system user | | NULL | Connect | 2039 | Slave has read all relay log; waiting for more updates | NULL |
- | 4 | root | localhost | NULL | Query | 0 | starting | show processlist |
- +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
- 2 rows in set (0.00 sec)
5.修改slave的my.cnf,重启mysql
- [mysqld]
- socket = /usr/local/mysql/mysql.sock
- character_set_server= utf8
- init_connect= 'SET NAMES utf8'
- basedir= /usr/local/mysql
- datadir= /u01/mysql
- socket = /u01/mysql/mysql.sock
- log-error= /u01/log/mysql/mysql_3306.err
- pid-file= /u01/mysql/mysqld.pid
- lower_case_table_names = 1
- sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- secure-file-priv = /u01/backup
- server-id=10001
- log_bin = /u01/mysql/mysql_bin
- #skip-grant-tables
- innodb_flush_log_at_trx_commit=1
- sync_binlog=1
- #relay-log=/u01/mysql/slave_relay_bin
- expire_logs_days=10
- read_only=0
- #relay_log_recovery=on
- #relay_log_info_repository=TABLE
- max_binlog_size=1073741824
- #autocommit=off
- #long_query_time=15
- #slow_query_log=on
- [root@qht132 ~]# service mysql restart
- Shutting down MySQL.. [ OK ]
- Starting MySQL. [ OK ]
需要开启原slave的bin_log,关闭relay_log*,关闭read_only
5.提升slave为master
- mysql> stop slave;
- Query OK, 0 rows affected (0.00 sec)
- mysql> reset master;
- Query OK, 0 rows affected (0.00 sec)
- mysql> reset slave all;
- Query OK, 0 rows affected (0.01 sec)
- mysql> show variables like 'read_only';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | read_only | OFF |
- +---------------+-------+
- 1 row in set (0.00 sec)
- mysql> show master status \G
- *************************** 1. row ***************************
- File: mysql_bin.000001
- Position: 154
- Binlog_Do_DB:
- Binlog_Ignore_DB:
- Executed_Gtid_Set:
- 1 row in set (0.00 sec)
在新的master上建立同步帐户并赋予同步权限(由于我为了省事,之前主从配置的时候将61网段的权限都赋予给了repl,所以这一步我也可以不做)
- mysql> grant replication slave on *.* to 'repl'@'172.17.61.%';
- Query OK, 0 rows affected (0.01 sec)
6.修改原master的my.cnf,重启mysql
- [mysqld]
- socket = /usr/local/mysql/mysql.sock
- character_set_server= utf8
- init_connect= 'SET NAMES utf8'
- basedir= /usr/local/mysql
- datadir= /u01/mysql
- socket = /u01/mysql/mysql.sock
- log-error= /u01/log/mysql/mysql_3306.err
- pid-file= /u01/mysql/mysqld.pid
- lower_case_table_names = 1
- sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- secure-file-priv = /u01/backup
- server-id=10000
- #log_bin = /u01/mysql/mysql_bin
- #skip-grant-tables
- #innodb_flush_log_at_trx_commit=1
- #sync_binlog=1
- expire_logs_days=10
- read_only=1
- relay_log_recovery=on
- relay_log_info_repository=TABLE
- #max_binlog_size=1073741824
- #autocommit=off
- #long_query_time=15
- #slow_query_log=on
关闭原master的binlog,开启relay_log*,打开read_only状态
- [root@qht131 ~]# service mysql restart
- Shutting down MySQL.. [ OK ]
- Starting MySQL. [ OK ]
7.将master切换为slave
- mysql> reset master;
- Query OK, 0 rows affected (0.00 sec)
- mysql> change master to
- -> master_host='172.17.61.132',
- -> master_user='repl',
- -> master_password='repl',
- -> master_log_file='mysql_bin.000001',
- -> master_log_pos=154;
- Query OK, 0 rows affected, 2 warnings (0.03 sec)
- mysql> start slave;
- Query OK, 0 rows affected (0.01 sec)
master_log_file和master_log_pos需指定为新master切换后显示的起始位置
检查一下新slave的状态,确保slave_io_running和slave_sql_running都为YES。
- mysql> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 172.17.61.132
- Master_User: repl
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql_bin.000001
- Read_Master_Log_Pos: 360
- Relay_Log_File: qht131-relay-bin.000002
- Relay_Log_Pos: 526
- Relay_Master_Log_File: mysql_bin.000001
- 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: 360
- Relay_Log_Space: 734
- 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: 10001
- Master_UUID: 744cfcde-3a9b-11e8-b299-000c2900d025
- Master_Info_File: /u01/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:
- Executed_Gtid_Set:
- Auto_Position: 0
- Replicate_Rewrite_DB:
- Channel_Name:
- Master_TLS_Version:
- 1 row in set (0.00 sec)
8.最后进行测试
新master:
- mysql> create table t3 ( c1 int);
- Query OK, 0 rows affected (0.05 sec)
新slave:
- mysql> use l5m
- Database changed
- mysql> show tables;
- +---------------+
- | Tables_in_l5m |
- +---------------+
- | t1 |
- | t2 |
- | t3 |
- | test_emp |
- | tt |
- +---------------+
- 5 rows in set (0.00 sec)
- mysql> show create table t3\G
- *************************** 1. row ***************************
- Table: t3
- Create Table: CREATE TABLE `t3` (
- `c1` int(11) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8
- 1 row in set (0.00 sec)
至此测试完毕!
转自
mysql5.7 主从复制的正常切换 - CSDN博客
https://blog.csdn.net/jolly10/article/details/79877564
mysql5.7 主从复制的正常切换【转】的更多相关文章
- mysql5.7主从复制--在线变更复制类型【转】
这里说一下关于如何在线变更复制类型(日志复制到全局事物复制),参考课程:mysql5.7复制实战 先决条件 (1)集群中所有的服务器版本均高于5.7.6(2)集群中所有的服务器gtid_mod ...
- redis - 主从复制与主从切换
redis2.8之前本身是不支持分布式管理的,一般建议使用redis3.0及以后版本 redis主从切换的方法 keepalive 或者 使用sentinel线程管理 说明如何使用sentinel实 ...
- MySQL5.6主从复制最佳实践
MySQL5.6 主从复制的配置 环境 操作系统:CentOS-6.6-x86_64 MySQL 版本:mysql-5.6.26.tar.gz 主节点 IP:192.168.31.57 ...
- MySQL5.6主从复制方案
MySQL5.6主从复制方案 1.主备服务器操作 环境:CentOS 6.3/6.4 最小化缺省安装,配置好网卡. 安装MySQL前,确认Internet连接正常,以便下载安装文件. # 新增用户组 ...
- MYSQL主从复制搭建及切换操作(GTID与传统)
结构如下: MYSQL主从复制方式有默认的复制方式异步复制,5.5版本之后半同步复制,5.6版本之后新增GTID复制,包括5.7版本的多源复制. MYSQL版本:5.7.20 操作系统版本:linux ...
- mysql5.7主从复制
1.简介 1.1主从复制 是指建立一个和主数据库完全一样的数据库环境,称为从数据库:主数据库一般是实时的业务数据库,从数据库的作用和使用场合一般有几个: 一是作为后备数据库,主数据库服务器故障后,可切 ...
- 利用mycat实现基于mysql5.5主从复制的读写分离
整体步骤: 1.准备好两台服务器,一台作为主数据库服务器,一台作为从服务器,并安装好mysql数据库,此处略 2.配置好主从同步 3.下载JDK配置mycat依赖的JAVA环境,mycat采用java ...
- MySQL5.7Gtid主从复制总是遇到日志被清等出现无法正常主从复制
最近最是在MySQL5.7上的的gtid主从复制问题总是遇上下面问题: Last_Error: Coordinator stopped because there were error(s) in t ...
- Mysql5.6主从复制-基于binlog
MySQL5.6开始主从复制有两种方式:基于日志(binlog):基于GTID(全局事务标示符). 此文章是基于日志方式的配置步骤 环境: master数据库IP:192.168.247.128sla ...
随机推荐
- python数据分析所需要了解的操作。
import pandas as pd data_forest_fires = pd.read_csv("data/forestfires.csv", encoding='gbk' ...
- C语言入门:03.关键字、标识符、注释
一.学习语法之前的提醒 (1)C语言属于一门高级语言,其实,所有高级语言的基本语法组成部分都是一样的,只是表现形式不太一样 (2)就好像亚洲人和非洲人,大家都有人类的结构:2只 手.2只脚.1个头,只 ...
- 获取select的 text
JS获取select的value和text值的简单实例 感谢 3lian8 的投递 时间:2014-02-27 来源:三联教程 本篇文章主要是对JS获取select的value和text值的简单实例进 ...
- Hbase之JAVA API不能远程访问问题解决
1.配置Linux的hostname2.配置Linux的hosts,映射ip的hostname的关系3.配置访问windows的hosts 参考文档:http://blog.csdn.net/ty49 ...
- Ubuntu17安装maven3.5.2
1.下载maven 源码文件.tar.gz 2.解压源文件sudo tar -zxvf .tar.gz文件 3.配置/etc/profile文件 export MAVEN_HOME=/app/java ...
- github如何添加新的分支
需求:甲建立分支分给乙步骤:在甲创建的项目仓库里边点右上角的按钮 就可以通过名字搜索了. 在乙的右上角 再选中Repositories就可以看到所有的 存储库 了
- 【BZOJ1025】[SCOI2009]游戏(动态规划)
[BZOJ1025][SCOI2009]游戏(动态规划) 题面 BZOJ 洛谷 题解 显然就是一个个的置换,那么所谓的行数就是所有循环的大小的\(lcm+1\). 问题等价于把\(n\)拆分成若干个数 ...
- HGOI20180813 (NOIP2018 提高组 Day2 模拟试题)
省常中省选提高Day2 继续 第一题就考了贪心,正解95pts的贪心策略第一印象是想到的,但是被自己否定掉了qwq,然后打了 不是正解的贪心,样例5没过(可怜)思路如下:先找出每个门对应可以通过的人数 ...
- bzoj1178/luogu3626 会议中心 (倍增+STL::set)
贪心地,可以建出一棵树,每个区间对应一个点,它的父亲是它右边的.与它不相交的.右端点最小的区间. 为了方便,再加入一个[0,0]区间 于是就可以倍增来做出从某个区间开始,一直到某个右界,这之中最多能选 ...
- Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)
Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...