mysql主从复制 如果主库出现故障 从库升级为主库故障修复后在恢复

本文详细介绍了在CentOS 7环境下进行MySQL主从复制的故障转移过程,包括模拟主库故障、从库升主及数据同步等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mysql主从复制 如果主库出现故障 从库升级为主库故障修复后在

恢复

环境配置:

  • 系统版本
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)
  • 内核版本
 [root@localhost ~]# uname -r
3.10.0-1062.el7.x86_64
  • 角色分配

A :主 – 故障 - 主 192.168.74.190
B :从 – 主 – 从 192.168.74.191

首先确保主从环境
master 主:
模拟主库故障重启

slave 从:
关闭重置slave 重置master

MariaDB [(none)]> stop slave; Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> reset master; Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> reset slave all; Query OK, 0 rows affected (0.00 sec)

重新授权 从–>主 (并查看日志数据位置)

MariaDB [(none)]> grant all on . to’slave’@’%’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00
sec)

MariaDB [(none)]> show master status;
±-----------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±-----------------±---------±-------------±-----------------+
| mysql-bin.000001 | 445 |
±-----------------±---------±-------------±-----------------+

模拟新的数据生成(创建库并插入数据)

MariaDB [(none)]> create database ceshi charset utf8; Query OK, 1 row
affected (0.01 sec)

MariaDB [(none)]> use ceshi; Database changed MariaDB [ceshi]> create
table a(id int(10),name varchar(10)); Query OK, 0 rows affected (0.00
sec)

MariaDB [ceshi]> insert into a values(1,“新增”); Query OK, 1 row
affected (0.00 sec)

MariaDB [ceshi]> select * from a;
±-----±-------+
| id | name |
±-----±-------+
| 1 | 新增 |
±-----±-------+
1 row in set (0.00 sec)

再查看新增数据位置

MariaDB [(none)]> show master status;
±-----------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±-----------------±---------±-------------±-----------------+
| mysql-bin.000001 | 842 |
±-----------------±---------±-------------±-----------------+
1 row in set (0.00 sec)

主恢复 从又恢复从 将从的新增数据打包 发给主

[root@localhost ~]# mysqlbinlog /var/lib/mysql/mysql-bin.000001 --start-position=445 --stop-position=842 > /root/a.sql

[root@localhost ~]# scp /root/a.sql 192.168.74.190:/root/
The authenticity of host ‘192.168.74.190 (192.168.74.190)’ can’t be established. ECDSA key
fingerprint is SHA256:DQjrTMPNwX4iiAGlu9Dn2BFf6LK9v8EIRKZhu0cNRnQ.
ECDSA key fingerprint is MD5:97:2c:78:4e:ea:ba:ad:3f:10:88:4d:1d:d9:b9:a1:ec.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.74.190’ (ECDSA) to the list of known hosts.
root@192.168.74.190’s password:
a.sql 100% 2164 1.5MB/s 00:00

master上接受并查看

MariaDB [(none)]> source /root/a.sql

并查看
MariaDB [ceshi]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
5 rows in set (0.00 sec)

MariaDB [ceshi]> use ceshi
Database changed

MariaDB [ceshi]> select * from a;
±-----±-------+
| id | name |
±-----±-------+
| 1 | 新增 |
±-----±-------+
1 row in set (0.00 sec)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值