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 ...
随机推荐
- Linux命令(二十七) 用户组管理命令
Linux提供了一系列的命令管理用户组.用户组就是具有相同特征的用户集合.每个用户都有一个用户组,系统能对一个用户组中所有用户进行集中管理,通过把相同属性的用户定义到同一用户组,并赋予该用户自一定的操 ...
- Caffe搭建:Ubuntu14.04 + CUDA7.0 + opencv3.0 + Matlab2014A
从Hinton在science上发表深度学习训练开创新的文章以来,深度学习火了整整有3年多,而且随着新的硬件和算法的提出,深度学习正在应用于越来越多的领域,发挥其算法的优势. 实验室并没有赶上第一波深 ...
- 删除或添加最大化、最小化按钮 - 回复 "Tommy the CAT" 的问题
本例效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...
- C# 多线程之Thread类
使用System.Threading.Thread类可以创建和控制线程. 常用的构造函数有: // 摘要: // 初始化 System.Threading.Thread 类的新实例,指定允许对象在 ...
- IOS AES加密之ECB128模式
1.AES加密模式有好几种,网上大多是CBC.256模式,找了好久才找到解决ECB128模式加密. AES需要导入头文件 #import <CommonCrypto/CommonCryptor. ...
- CentOS7下安装Scrapy
更新yum[root@localhost ~]# yum -y update1安装gcc及扩展包[root@localhost ~]# yum install gcc libffi-devel pyt ...
- Java之List使用场景
1.List使用场景 特点: ①在 List集合中允许出现 重复元素 <通过元素的equals方法,来比较是否为重复的元素.> ②所有元素是以一种 线性方式进行存储 ③List集合还有一个 ...
- 知识点【JavaScript模块化】
JavaScript模块化历程 JavaScript发展变迁大概是一下几个步骤: 工具(浏览器兼容) 组件(功能模块) 框架(功能模块组织) 应用(业务模块组织) 但是经过了长长的后天努力过程Java ...
- 异步查询json传日期格式到前台,变成了时间戳的格式
问题: 使用mybatis 查询mysql数据库,其中一个日期格式的字段,由异步查询使用 json传递到前台,变成了时间戳,而不是日期格式了.如何使查询出的日期展示成日期格式呢 解决办法: 1.尝试使 ...
- 【刷题】BZOJ 1823 [JSOI2010]满汉全席
Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...